static Q_INT64 read_int_ascii( QDataStream *s ) { register int n = 0; char buf[40]; for ( ;; ) { buf[n] = s->device()->getch(); if ( buf[n] == '\n' || n > 38 ) // $-terminator break; n++; } buf[n] = '\0'; #if defined(__LP64__) || defined(Q_OS_OSF) // sizeof(long) == 8 return strtol(buf, (char **)0, 10); #else # if defined(Q_OS_TEMP) return strtol( buf, (char**)0, 10 ); # elif defined(Q_OS_WIN) return _atoi64( buf ); # elif defined(Q_OS_HPUX) return __strtoll( buf, (char**)0, 10 ); # elif defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION < 0x1020 return strtoq( buf, (char**)0, 10 ); # else return strtoll( buf, (char**)0, 10 ); // C99 function # endif #endif }
// Convert string to signed int FXint __strtol(const FXchar *beg,const FXchar** end,FXint base,FXbool* ok){ register FXlong value=__strtoll(beg,end,base,ok); if(__unlikely(value<INT_MIN)){ if(ok) *ok=false; return INT_MIN; } if(__unlikely(value>INT_MAX)){ if(ok) *ok=false; return INT_MAX; } return (FXint)value; }
long long int strtoll( const char* nptr, char** endptr, int base ) { #if defined(HAVE___STRTOLL) return __strtoll(nptr, endptr, base); #else #error strtoll support is not available #endif }
long long int LwStrtoll( const char* nptr, char** endptr, int base ) { #if defined(HAVE_STRTOLL) return strtoll(nptr, endptr, base); #elif defined(HAVE___STRTOLL) return __strtoll(nptr, endptr, base); #elif SIZEOF_LONG_LONG_INT == SIZEOF_LONG_INT && defined(HAVE_STRTOL) return (long long) strtol(nptr, endptr, base); #else #error strtoll support is not available #endif }
Int64 strToInt64(const char* s, char** endptr, int base) { #if defined(_WIN32) # ifdef __MINGW32__ return strToInt64Impl(s, endptr, base); # else return _strtoi64(s, endptr, base); # endif #elif defined(ICE_64) return strtol(s, endptr, base); #elif defined(__hpux) return __strtoll(s, endptr, base); #else return strtoll(s, endptr, base); #endif }
long strtol(const char *nptr, char **endptr, int base) { struct st_buf_ptr buf; buf.p = (char*)nptr; intmax_t ret; remove_spaces(&buf); ret = __strtoll(endptr, base, 0, get_from_buf, inc_buf, (void*)&buf); if (errno == ERANGE) { ret = LONG_MAX; } else if (ret > LONG_MAX) { ret = LONG_MAX; errno = ERANGE; } return (long int)ret; }
static void strtoll_func() { /* Without integner prefix(e.g. 0x, 0b...) */ char text[1024] = "12345670qwert\0"; char* non_intger; long long int rtn = 0; int base = 0; /* base 0 */ rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 8, strlen(text + 8)); /* With prefix 0(octet)*/ memset(text, 0, 1024); strncpy(text, "012345670qwert", strlen("012345670qwert")); /* base 0 */ base = 0; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 012345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 12345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 9, strlen(text + 9)); /* With prefix 0x(hex)*/ memset(text, 0, 1024); strncpy(text, "0x12345670qwert", strlen("0x12345670qwert")); /* base 0 */ base = 0; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 10, strlen(text + 10)); /* base 8 */ base = 8; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0); assert_memory_equal(non_intger, text + 1, strlen(text + 1)); /* base 10 */ base = 10; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0); assert_memory_equal(non_intger, text + 1, strlen(text + 1)); /* base 16 */ base = 16; rtn = __strtoll(text, &non_intger, base); assert_int_equal(rtn, 0x12345670); assert_memory_equal(non_intger, text + 10, strlen(text + 10)); }
/* * parse scanf format string */ int __scanf(const char *input, FILE *stream, bool stream_or_memory, const char *fmt, va_list ap) { unsigned int assign_count = 0; va_list ap_copy, ap_tmp; int width, base = 0; char *p, *s; long long num_res = 0; unsigned long long num_ures = 0; bool num_unsigned = false; bool suppress = false; bool use_width = false; bool using_nth = false; /* length modifiers */ bool lm_h, lm_hh, lm_l, lm_ll, lm_j, lm_z, lm_t, lm_L; int arg_nth = 0; int i = 0; bool list_not; char *list_start, *list_end; unsigned char *user_us; unsigned long *user_ul; unsigned long long *user_ull; unsigned short *user_ush; unsigned int *user_ui; uintmax_t *user_uj; char *user_s = NULL; long *user_l; long long *user_ll; short *user_sh; int *user_i; intmax_t *user_j; size_t *user_z; ptrdiff_t *user_t; void **user_pp; #ifndef CONFIG_WITHOUT_FLOATING #ifndef CONFIG_WITHOUT_LONG_DOUBLE long double *user_ld; #endif /* CONFIG_WITHOUT_LONG_DOUBLE */ double *user_d; float *user_f; #endif /* CONFIG_WITHOUT_FLOATING */ struct st_mem_stream mem_stream; mem_stream.stream_or_memory = stream_or_memory; mem_stream.mem = (char**)&input; mem_stream.stream = stream; mem_stream.last_stream_char = '\0'; mem_stream.read_bytes = 0; __va_copy(ap_copy, ap); if (stream != NULL) lock_stream(stream); for (p = (char*)fmt; *p != '\0'; p++) { use_width = false; width = 1; num_unsigned = false; suppress = false; num_res = 0; num_ures = 0; lm_h = false; lm_hh = false; lm_l = false; lm_ll = false; lm_j = false; lm_z = false; lm_t = false; lm_L = false; if (*p != '%') { char_comp: s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } if (isspace(*p)) { while (isspace(*s)) { __inc_next_char(&mem_stream); s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } } } else { if (*p == *s) { __inc_next_char(&mem_stream); } else { goto matching_failure; } } continue; } /* *p == '%' */ again_inc: p++; again: switch(*p) { case '%': goto char_comp; break; case '*': suppress = true; goto again_inc; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': i = *p - '0'; for (p++; p && (unsigned int)(*p - '0') < 10; p++) { i = i * 10 + (*p - '0'); } if (*p == '$') { using_nth = true; if (i == 0) { goto matching_failure; } else { arg_nth = i; } p++; } else { width = i; if (width != 0) { use_width = true; } } goto again; /* Length modifiers */ case 'h': if (lm_h) { lm_hh = true; lm_h = false; } else { lm_h = true; } goto again_inc; case 'l': if (lm_l) { lm_ll = true; lm_l = false; } else { lm_l = true; } goto again_inc; case 'j': lm_j = true; goto again_inc; case 'z': lm_z = true; goto again_inc; case 't': lm_t = true; goto again_inc; #ifndef CONFIG_WITHOUT_LONG_DOUBLE case 'L': lm_L = true; goto again_inc; #endif /* CONFIG_WITHOUT_LONG_DOUBLE */ /* Numbers */ case 'd': base = 10; goto number; case 'i': base = 0; goto number; case 'o': base = 8; num_unsigned = true; goto number; case 'u': base = 10; num_unsigned = true; goto number; case 'x': case 'X': base = 16; num_unsigned = true; number: eat_spaces(&mem_stream); if (!use_width) { width = 0; } errno = 0; /* Check for strto* errors */ if (suppress) { (void)__strtoll(NULL, base, width, get_next_char, inc_next_char, (void*)&mem_stream); } else { if (num_unsigned) { uintmax_t num_tmp; errno = 0; num_tmp = __strtoull(NULL, base, width, get_next_char, inc_next_char, (void*)&mem_stream); if(errno == EINVAL) { goto matching_failure; } assign_count++; if (lm_ll) { GET_ARG(user_ull, unsigned long long*); *user_ull = (unsigned long long)num_tmp; } else if (lm_l) { GET_ARG(user_ul, unsigned long*); *user_ul = (unsigned long)num_tmp; } else if (lm_hh) { GET_ARG(user_us, unsigned char*); *user_us = (unsigned char)num_tmp; } else if (lm_h) { GET_ARG(user_ush, unsigned short*); *user_ush = (unsigned short)num_tmp; } else if (lm_j) { GET_ARG(user_uj, uintmax_t*); *user_uj = (uintmax_t)num_tmp; } else if (lm_z) { GET_ARG(user_z, size_t*); *user_z = (size_t)num_tmp; } else if (lm_t) { GET_ARG(user_t, ptrdiff_t*); *user_t = (unsigned long)num_tmp; } else { GET_ARG(user_ui, unsigned int*); *user_ui = (unsigned int)num_tmp; } } else { intmax_t num_tmp; errno = 0; num_tmp = __strtoll(NULL, base, width, get_next_char, inc_next_char, (void*)&mem_stream); if(errno == EINVAL) { goto matching_failure; } assign_count++; if (lm_ll) { GET_ARG(user_ll, long long*); *user_ll = (long long)num_tmp; } else if (lm_l) { GET_ARG(user_l, long*); *user_l = (long int)num_tmp; } else if (lm_hh) { GET_ARG(user_s, char*); *user_s = (char)num_tmp; } else if (lm_h) { GET_ARG(user_sh, short*); *user_sh = (short)num_tmp; } else if (lm_j) { GET_ARG(user_j, intmax_t*); *user_j = (intmax_t)num_tmp; } else if (lm_z) { GET_ARG(user_z, size_t*); *user_z = (signed long)num_tmp; } else if (lm_t) { GET_ARG(user_t, ptrdiff_t*); *user_t = (ptrdiff_t)num_tmp; } else { GET_ARG(user_i, int*); *user_i = (int)num_tmp; } } } /* if (errno == EINVAL) { goto matching_failure; } */ break; #ifndef CONFIG_WITHOUT_FLOATING case 'A': case 'a': case 'E': case 'e': case 'F': case 'f': case 'G': case 'g': eat_spaces(&mem_stream); if (!use_width) { width = 0; } s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } if (*s == 'i' || *s == 'I') { if (!eat_infinity(&mem_stream)) { s = __get_next_char(&mem_stream); if (*s == '\0') { goto input_failure; } else { goto matching_failure; } } /* if (!suppress) { assign_count++; if (lm_l) { GET_ARG(user_d, double*); *user_d = INFINITY; #ifndef CONFIG_WITHOUT_LONG_DOUBLE } else if (lm_L) { GET_ARG(user_ld, long double*); *user_ld = INFINITY; #endif } else { GET_ARG(user_f, float*); *user_f = INFINITY; } } */ } else if (*s == 'n' || *s == 'N') { if (!eat_nan(&mem_stream)) { goto matching_failure; } /* if (!suppress) { assign_count++; if (lm_l) { GET_ARG(user_d, double*); *user_d = NAN; #ifndef CONFIG_WITHOUT_LONG_DOUBLE } else if (lm_L) { GET_ARG(user_ld, long double*); *user_ld = NAN; #endif } else { GET_ARG(user_f, float*); *user_f = NAN; } } */ } else { if (suppress) { (void)__strtold(NULL, width, get_next_char, inc_next_char, (void*)&mem_stream); } else { assign_count++; if (lm_l) { GET_ARG(user_d, double*); *user_d = (double)__strtold(NULL, width, get_next_char, inc_next_char, (void*)&mem_stream); #ifndef CONFIG_WITHOUT_LONG_DOUBLE } else if (lm_L) { GET_ARG(user_ld, long double*); *user_ld = (long double)__strtold(NULL, width, get_next_char, inc_next_char, (void*)&mem_stream); #endif /* CONFIG_WITHOUT_LONG_DOUBLE */ } else { GET_ARG(user_f, float*); *user_f = (float)__strtold(NULL, width, get_next_char, inc_next_char, (void*)&mem_stream); } } } break; #endif /* CONFIG_WITHOUT_FLOATING */ case 'S': lm_l = true; case 's': eat_spaces(&mem_stream); if (!suppress) { GET_ARG(user_s, char*); i = 0; } s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } while (s != NULL && *s != '\0' && !isspace(*s)) { if (use_width) { if (width > 0) { width--; } else { break; } } if (!suppress) { user_s[i] = *s; i++; } __inc_next_char(&mem_stream); s = __get_next_char(&mem_stream); } if (!suppress) { assign_count++; user_s[i] = '\0'; } break; case '[': list_not = false; p++; if (*p == '^') { list_not = true; p++; } if (*p == '\0') { goto matching_failure; } list_start = p; p++; while (*p != ']') { if (*p == '\0') { goto matching_failure; } p++; } list_end = p; if (!suppress) { GET_ARG(user_s, char*); i = 0; } s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } while (s != NULL && *s != '\0' && check_list(*s, list_start, list_end, list_not)) { if (use_width) { if (width > 0) { width--; } else { break; } } if (!suppress) { user_s[i] = *s; i++; } __inc_next_char(&mem_stream); s = __get_next_char(&mem_stream); } if (!suppress) { assign_count++; user_s[i] = '\0'; } break; case 'C': lm_l = true; case 'c': use_width = true; if (!suppress) { GET_ARG(user_s, char*); i = 0; } s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } for (; width > 0 && s != NULL && *s != '\0'; width--) { if (!suppress) { user_s[i] = *s; i++; } __inc_next_char(&mem_stream); s = __get_next_char(&mem_stream); } if (__is_eof(s) && width > 0) { goto eof_failure; } if (!suppress) { assign_count++; user_s[i] = '\0'; } break; case 'p': eat_spaces(&mem_stream); if (!use_width) { width = 0; } else if (width < 2) { goto matching_failure; } s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } if (*s != '0') { goto matching_failure; } __inc_next_char(&mem_stream); s = __get_next_char(&mem_stream); if (__is_eof(s)) { goto eof_failure; } if (*s != 'x' && *s != 'X') { goto matching_failure; } __inc_next_char(&mem_stream); width -= 2; if (suppress) { (void)__strtoll(NULL, 16, width, get_next_char, inc_next_char, (void*)&mem_stream); } else { assign_count++; GET_ARG(user_pp, void**); *user_pp = (void*)(long)__strtoll(NULL, 16, width, get_next_char, inc_next_char, (void*)&mem_stream); } break; case 'n': if (lm_ll) { GET_ARG(user_ll, long long*); *user_ll = (long long)mem_stream.read_bytes; } else if (lm_l) { GET_ARG(user_l, long*); *user_l = (long)mem_stream.read_bytes; } else if (lm_hh) { GET_ARG(user_s, char*); *user_s = (char)mem_stream.read_bytes; } else if (lm_h) { GET_ARG(user_sh, short*); *user_sh = (short)mem_stream.read_bytes; } else if (lm_j) { GET_ARG(user_j, intmax_t*); *user_j = (intmax_t)mem_stream.read_bytes; } else if (lm_z) { GET_ARG(user_i, int*); *user_i = (int)mem_stream.read_bytes; } else if (lm_t) { GET_ARG(user_t, ptrdiff_t*); *user_t = (ptrdiff_t)mem_stream.read_bytes; } else { GET_ARG(user_i, int*); *user_i = (int)mem_stream.read_bytes; } break; default: if (*p == '\0') { break; } } } matching_failure: if (stream != NULL) { unlock_stream(stream); } return assign_count; /* XXX: va_end */ input_failure: if (stream != NULL) { unlock_stream(stream); } return EOF; eof_failure: if (assign_count > 0) { goto matching_failure; } else { goto input_failure; } }