SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Itoa.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #include "slikenet/EmptyHeader.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 // Fast itoa from http://www.jb.man.ac.uk/~slowe/cpp/itoa.html for Linux since it seems like Linux doesn't support this function.
23 // I modified it to remove the std dependencies.
24 char* Itoa( int value, char* result, int base )
25  {
26  // check that the base if valid
27  if (base < 2 || base > 16) { *result = 0; return result; }
28  char* out = result;
29  int quotient = value;
30 
31  int absQModB;
32 
33  do {
34  // KevinJ - get rid of this dependency
35  //*out = "0123456789abcdef"[ std::abs( quotient % base ) ];
36  absQModB=quotient % base;
37  if (absQModB < 0)
38  absQModB=-absQModB;
39  *out = "0123456789abcdef"[ absQModB ];
40  ++out;
41  quotient /= base;
42  } while ( quotient );
43 
44  // Only apply negative sign for base 10
45  if ( value < 0 && base == 10) *out++ = '-';
46 
47  // KevinJ - get rid of this dependency
48  // std::reverse( result, out );
49  *out = 0;
50 
51  // KevinJ - My own reverse code
52  char *start = result;
53  char temp;
54  out--;
55  while (start < out)
56  {
57  temp=*start;
58  *start=*out;
59  *out=temp;
60  start++;
61  out--;
62  }
63 
64  return result;
65 }
66 
67 #ifdef __cplusplus
68 }
69 #endif