9class CLASS_DECL_ACME text_reader
14 const_char_pointer m_psz;
18 text_reader(const_char_pointer psz) : m_psz(psz) { }
19 text_reader(
const text_reader & textreader) :
20 m_psz(textreader.m_psz)
24 text_reader(text_reader && textreader) :
25 m_psz(textreader.m_psz)
27 textreader.m_psz =
nullptr;
35 text_reader & operator >>(
bool & b)
38 skip_whitespace(m_psz);
40 auto word = read_word(m_psz);
42 paired_trim(word,
"\"'([{",
"\"')]}");
56 else if (word.case_insensitive_order(
"false") == 0)
62 else if (word.case_insensitive_order(
"true") == 0)
68 else if (word.case_insensitive_order(
"no") == 0)
74 else if (word.case_insensitive_order(
"yes") == 0)
83 throw parsing_exception(
"text_reader::operator >> (bool &) invalid value for boolean : \"" +
string(word) +
"\"");
92 text_reader & operator >>(::i8 & ch)
102 text_reader & operator >>(::u8 & uch)
105 uch = *((::u8 *)m_psz);
117 text_reader & operator >>(unichar & wch)
120 wch = ::consume_unicode_index(m_psz);
142 text_reader & operator >>(::i16 & sh)
145 read_integer(sh, m_psz);
152 text_reader & operator >>(::u16 & u)
155 read_natural(u, m_psz);
162 text_reader & operator >>(::i32 & i)
165 read_integer(i, m_psz);
172 text_reader & operator >>(::u32 & u)
175 read_natural(u, m_psz);
182 text_reader & operator >>(::i64 & i)
185 read_integer(i, m_psz);
192 text_reader & operator >>(::u64 & u)
195 read_natural(u, m_psz);
202 text_reader & operator >>(::f32 & f)
212 text_reader & operator >>(::f64 & d)
222 text_reader & operator >>(
string & str)
225 str = read_line(m_psz);
235 set.parse_network_payload(m_psz);
242 static string read_line(const_char_pointer &psz)
261 while (*psz !=
'\r' && *psz !=
'\n' && *psz !=
'\0')
270 if (*psz ==
'\n' && *(psz - 1) ==
'\r')
277 return { pszStart, pszEnd - pszStart };
282 static const_ansi_range read_word(const_char_pointer &psz)
301 while (!character_isspace(*psz) && *psz !=
'\0')
310 if (*psz ==
'\n' && *(psz - 1) ==
'\r')
317 return { pszStart, pszEnd - pszStart };
322 template <
typename TYPE >
323 void read_natural(TYPE & t, const_char_pointer &psz)
326 skip_whitespace(psz);
330 auto l = strtol(psz, (char_pointer *) & psz, 10);
332 if (psz == pszStart || l < 0)
335 throw parsing_exception(
"cannot text_reader::read_natural from \"" + ellipsis(pszStart, 20) +
"\"");
344 template <
typename TYPE >
345 void read_integer(TYPE & t, const_char_pointer &psz)
348 skip_whitespace(psz);
352 t = (TYPE)strtol(psz, (char_pointer *) & psz, 10);
357 throw parsing_exception(
"cannot text_reader::read_integer from \"" + ellipsis(pszStart, 20) +
"\"");
364 template <
typename TYPE >
365 void read_f32(TYPE & t, const_char_pointer &psz)
368 skip_whitespace(psz);
372 t = (TYPE)strtod(psz, (char_pointer *) & psz);
377 throw parsing_exception(
"cannot text_reader::read_f32 from \"" + ellipsis(pszStart, 32) +
"\"");
384 static void skip_whitespace(const_char_pointer &psz)
387 while (unicode_is_whitespace(*psz))
397 static void paired_trim(const_ansi_range &
block, const_char_pointer pszBeg, const_char_pointer pszEnd)
400 const_char_pointer pszBegChar = pszBeg;
401 const_char_pointer pszEndChar = pszEnd;
404 *pszBegChar !=
'\0' &&
411 if (*
block.begin() == *pszBegChar && *(
block.end() - 1) == *pszEndChar)
427 static string ellipsis(const_char_pointer psz, character_count maximum_length)
430 if (maximum_length < 3)
433 throw ::exception(error_bad_argument);
439 character_count i = 0;
441 for (; i < maximum_length; i++, psz++)
455 auto pszBuffer = str.get_buffer(i);
457 if (i == maximum_length && *psz !=
'\0')
460 memory_copy(pszBuffer, pszStart, maximum_length - 3);
462 ansi_copy(pszBuffer + maximum_length - 3,
"...");
468 memory_copy(pszBuffer, pszStart, i);
472 str.release_buffer(i);
474 return ::transfer(str);