123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #include "kwsysPrivate.h"
- #include KWSYS_HEADER(Configure.hxx)
- #include <iostream>
- #include KWSYS_HEADER(IOStream.hxx)
- #if 0
- #include "Configure.hxx.in"
- #include "IOStream.hxx.in"
- #endif
- #if KWSYS_IOS_NEED_OPERATORS_LL
- #include <stdio.h> // sscanf, sprintf
- #include <string.h> // memchr
- #if defined(_MAX_INT_DIG)
- #define KWSYS_IOS_INT64_MAX_DIG _MAX_INT_DIG
- #else
- #define KWSYS_IOS_INT64_MAX_DIG 32
- #endif
- namespace KWSYS_NAMESPACE {
- static int IOStreamScanStream(std::istream& is, char* buffer)
- {
-
- char* out = buffer;
- char* end = buffer + KWSYS_IOS_INT64_MAX_DIG - 1;
-
- if (is.peek() == '+') {
- *out++ = '+';
- is.ignore();
- } else if (is.peek() == '-') {
- *out++ = '-';
- is.ignore();
- }
-
-
-
- int base = 0;
- int flags = is.flags() & std::ios_base::basefield;
- if (flags == std::ios_base::oct) {
- base = 8;
- } else if (flags == std::ios_base::dec) {
- base = 10;
- } else if (flags == std::ios_base::hex) {
- base = 16;
- }
- bool foundDigit = false;
- bool foundNonZero = false;
- if (is.peek() == '0') {
- foundDigit = true;
- is.ignore();
- if ((is.peek() == 'x' || is.peek() == 'X') && (base == 0 || base == 16)) {
- base = 16;
- foundDigit = false;
- is.ignore();
- } else if (base == 0) {
- base = 8;
- }
- }
-
- const char* digits = "0123456789abcdefABCDEF";
- int maxDigitIndex = 10;
- if (base == 8) {
- maxDigitIndex = 8;
- } else if (base == 16) {
- maxDigitIndex = 10 + 6 + 6;
- }
-
- for (; is.peek() != EOF; is.ignore()) {
- if (memchr(digits, *out = (char)is.peek(), maxDigitIndex) != 0) {
- if ((foundNonZero || *out != '0') && out < end) {
- ++out;
- foundNonZero = true;
- }
- foundDigit = true;
- } else {
- break;
- }
- }
-
- if (foundDigit && !foundNonZero) {
- *out++ = '0';
- } else if (!foundDigit) {
- out = buffer;
- }
-
- *out = '\0';
- return base;
- }
- template <class T>
- std::istream& IOStreamScanTemplate(std::istream& is, T& value, char type)
- {
- int state = std::ios_base::goodbit;
-
- std::istream::sentry okay(is);
- if (okay) {
- try {
-
- char buffer[KWSYS_IOS_INT64_MAX_DIG];
- #if defined(_MSC_VER)
- char format[] = "%I64_";
- const int typeIndex = 4;
- #else
- char format[] = "%ll_";
- const int typeIndex = 3;
- #endif
- switch (IOStreamScanStream(is, buffer)) {
- case 8:
- format[typeIndex] = 'o';
- break;
- case 0:
- case 10:
- format[typeIndex] = type;
- break;
- case 16:
- format[typeIndex] = 'x';
- break;
- };
-
- T result;
- int success = (sscanf(buffer, format, &result) == 1) ? 1 : 0;
-
- if (is.peek() == EOF) {
- state |= std::ios_base::eofbit;
- }
- if (!success) {
- state |= std::ios_base::failbit;
- } else {
- value = result;
- }
- } catch (...) {
- state |= std::ios_base::badbit;
- }
- }
- is.setstate(std::ios_base::iostate(state));
- return is;
- }
- template <class T>
- std::ostream& IOStreamPrintTemplate(std::ostream& os, T value, char type)
- {
- std::ostream::sentry okay(os);
- if (okay) {
- try {
-
- char format[8];
- char* f = format;
- *f++ = '%';
- if (os.flags() & std::ios_base::showpos) {
- *f++ = '+';
- }
- if (os.flags() & std::ios_base::showbase) {
- *f++ = '#';
- }
- #if defined(_MSC_VER)
- *f++ = 'I';
- *f++ = '6';
- *f++ = '4';
- #else
- *f++ = 'l';
- *f++ = 'l';
- #endif
- long bflags = os.flags() & std::ios_base::basefield;
- if (bflags == std::ios_base::oct) {
- *f++ = 'o';
- } else if (bflags != std::ios_base::hex) {
- *f++ = type;
- } else if (os.flags() & std::ios_base::uppercase) {
- *f++ = 'X';
- } else {
- *f++ = 'x';
- }
- *f = '\0';
-
-
- char buffer[2 * KWSYS_IOS_INT64_MAX_DIG];
- sprintf(buffer, format, value);
- os << buffer;
- } catch (...) {
- os.clear(os.rdstate() | std::ios_base::badbit);
- }
- }
- return os;
- }
- #if !KWSYS_IOS_HAS_ISTREAM_LONG_LONG
- std::istream& IOStreamScan(std::istream& is, IOStreamSLL& value)
- {
- return IOStreamScanTemplate(is, value, 'd');
- }
- std::istream& IOStreamScan(std::istream& is, IOStreamULL& value)
- {
- return IOStreamScanTemplate(is, value, 'u');
- }
- #endif
- #if !KWSYS_IOS_HAS_OSTREAM_LONG_LONG
- std::ostream& IOStreamPrint(std::ostream& os, IOStreamSLL value)
- {
- return IOStreamPrintTemplate(os, value, 'd');
- }
- std::ostream& IOStreamPrint(std::ostream& os, IOStreamULL value)
- {
- return IOStreamPrintTemplate(os, value, 'u');
- }
- #endif
- }
- #else
- namespace KWSYS_NAMESPACE {
- void IOStreamSymbolToAvoidWarning();
- void IOStreamSymbolToAvoidWarning()
- {
- }
- }
- #endif
|