comparison src/stdlib/SDL_string.c @ 1456:84de7511f79f

Fixed a bunch of 64-bit compatibility problems
author Sam Lantinga <slouken@libsdl.org>
date Wed, 01 Mar 2006 09:43:47 +0000
parents d910939febfa
children 73dc5d39bbf8
comparison
equal deleted inserted replaced
1455:f487bb150acc 1456:84de7511f79f
67 } 67 }
68 return (text - textstart); 68 return (text - textstart);
69 } 69 }
70 #endif 70 #endif
71 71
72 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD) 72 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
73 static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep) 73 static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
74 { 74 {
75 const char *textstart = text; 75 const char *textstart = text;
76 unsigned long value = 0; 76 unsigned long value = 0;
77 77
98 } 98 }
99 return (text - textstart); 99 return (text - textstart);
100 } 100 }
101 #endif 101 #endif
102 102
103 #ifdef SDL_HAS_64BIT_TYPE 103 #ifndef HAVE_SSCANF
104 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL) 104 static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
105 static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
106 { 105 {
107 const char *textstart = text; 106 const char *textstart = text;
108 Sint64 value = 0; 107 uintptr_t value = 0;
109 SDL_bool negative = SDL_FALSE; 108
110
111 if ( *text == '-' ) {
112 negative = SDL_TRUE;
113 ++text;
114 }
115 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 109 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
116 text += 2; 110 text += 2;
117 } 111 }
118 for ( ; ; ) { 112 for ( ; ; ) {
119 int v; 113 int v;
129 value *= radix; 123 value *= radix;
130 value += v; 124 value += v;
131 ++text; 125 ++text;
132 } 126 }
133 if ( valuep ) { 127 if ( valuep ) {
134 if ( negative && value ) { 128 *valuep = value;
135 *valuep = -value;
136 } else {
137 *valuep = value;
138 }
139 } 129 }
140 return (text - textstart); 130 return (text - textstart);
141 } 131 }
142 #endif 132 #endif
143 133
144 #ifndef HAVE_SSCANF 134 #ifdef SDL_HAS_64BIT_TYPE
145 static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep) 135 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
136 static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
146 { 137 {
147 const char *textstart = text; 138 const char *textstart = text;
148 Uint64 value = 0; 139 Sint64 value = 0;
149 140 SDL_bool negative = SDL_FALSE;
141
142 if ( *text == '-' ) {
143 negative = SDL_TRUE;
144 ++text;
145 }
150 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 146 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
151 text += 2; 147 text += 2;
152 } 148 }
153 for ( ; ; ) { 149 for ( ; ; ) {
154 int v; 150 int v;
164 value *= radix; 160 value *= radix;
165 value += v; 161 value += v;
166 ++text; 162 ++text;
167 } 163 }
168 if ( valuep ) { 164 if ( valuep ) {
165 if ( negative && value ) {
166 *valuep = -value;
167 } else {
168 *valuep = value;
169 }
170 }
171 return (text - textstart);
172 }
173 #endif
174
175 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
176 static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
177 {
178 const char *textstart = text;
179 Uint64 value = 0;
180
181 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
182 text += 2;
183 }
184 for ( ; ; ) {
185 int v;
186 if ( SDL_isdigit(*text) ) {
187 v = *text - '0';
188 } else if ( radix == 16 && SDL_isupperhex(*text) ) {
189 v = 10 + (*text - 'A');
190 } else if ( radix == 16 && SDL_islowerhex(*text) ) {
191 v = 10 + (*text - 'a');
192 } else {
193 break;
194 }
195 value *= radix;
196 value += v;
197 ++text;
198 }
199 if ( valuep ) {
169 *valuep = value; 200 *valuep = value;
170 } 201 }
171 return (text - textstart); 202 return (text - textstart);
172 } 203 }
173 #endif 204 #endif
486 } 517 }
487 return value; 518 return value;
488 } 519 }
489 #endif 520 #endif
490 521
522 #ifndef HAVE_STRTOUL
523 unsigned long SDL_strtoul(const char *string, char **endp, int base)
524 {
525 size_t len;
526 unsigned long value;
527
528 len = SDL_ScanUnsignedLong(string, base ? base : 10, &value);
529 if ( endp ) {
530 *endp = (char *)string + len;
531 }
532 return value;
533 }
534 #endif
535
491 #ifdef SDL_HAS_64BIT_TYPE 536 #ifdef SDL_HAS_64BIT_TYPE
492 537
493 #ifndef HAVE__I64TOA 538 #ifndef HAVE__I64TOA
494 char *SDL_lltoa(Sint64 value, char *string, int radix) 539 char *SDL_lltoa(Sint64 value, char *string, int radix)
495 { 540 {
547 { 592 {
548 size_t len; 593 size_t len;
549 Sint64 value; 594 Sint64 value;
550 595
551 len = SDL_ScanLongLong(string, base ? base : 10, &value); 596 len = SDL_ScanLongLong(string, base ? base : 10, &value);
597 if ( endp ) {
598 *endp = (char *)string + len;
599 }
600 return value;
601 }
602 #endif
603
604 #ifndef HAVE_STRTOULL
605 Uint64 SDL_strtoull(const char *string, char **endp, int base)
606 {
607 size_t len;
608 Uint64 value;
609
610 len = SDL_ScanUnsignedLongLong(string, base ? base : 10, &value);
552 if ( endp ) { 611 if ( endp ) {
553 *endp = (char *)string + len; 612 *endp = (char *)string + len;
554 } 613 }
555 return value; 614 return value;
556 } 615 }
815 } 874 }
816 done = SDL_TRUE; 875 done = SDL_TRUE;
817 break; 876 break;
818 case 'p': 877 case 'p':
819 { 878 {
820 unsigned long value; 879 uintptr_t value;
821 text += SDL_ScanUnsignedLong(text, 16, &value); 880 text += SDL_ScanUintPtrT(text, 16, &value);
822 if ( ! suppress ) { 881 if ( ! suppress ) {
823 void** valuep = va_arg(ap, void**); 882 void** valuep = va_arg(ap, void**);
824 *valuep = (void*)value; 883 *valuep = (void*)value;
825 ++retval; 884 ++retval;
826 } 885 }
1001 while ( *string && maxlen-- ) { 1060 while ( *string && maxlen-- ) {
1002 *text++ = *string++; 1061 *text++ = *string++;
1003 } 1062 }
1004 return (text - textstart); 1063 return (text - textstart);
1005 } 1064 }
1006 int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) 1065 int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
1007 { 1066 {
1008 char *textstart = text; 1067 char *textstart = text;
1009 if ( maxlen <= 0 ) { 1068 if ( maxlen <= 0 ) {
1010 return 0; 1069 return 0;
1011 } 1070 }