/*---------------------------------------------------------------------- | NPT_String::Format +---------------------------------------------------------------------*/ NPT_String NPT_String::Format(const char* format, ...) { NPT_String result; NPT_Size buffer_size = NPT_STRING_FORMAT_BUFFER_DEFAULT_SIZE; // default value va_list args; for(;;) { /* try to format (it might not fit) */ result.Reserve(buffer_size); char* buffer = result.UseChars(); va_start(args, format); int f_result = NPT_FormatStringVN(buffer, buffer_size, format, args); va_end(args); if (f_result >= (int)(buffer_size)) f_result = -1; if (f_result >= 0) { result.SetLength(f_result); break; } /* the buffer was too small, try something bigger */ /* (we don't trust the return value of NPT_FormatStringVN */ /* for the actual size needed) */ buffer_size *= 2; if (buffer_size > NPT_STRING_FORMAT_BUFFER_MAX_SIZE) break; } return result; }
/*---------------------------------------------------------------------- | AppendNumber +---------------------------------------------------------------------*/ static void AppendNumber(NPT_String& output, NPT_UInt32 number, unsigned int digit_count) { NPT_Size new_length = output.GetLength()+digit_count; output.SetLength(new_length); char* dest = output.UseChars()+new_length; while (digit_count--) { *--dest = '0'+(number%10); number /= 10; } }
/*---------------------------------------------------------------------- | NPT_BufferedInputStream::ReadLine +---------------------------------------------------------------------*/ NPT_Result NPT_BufferedInputStream::ReadLine(NPT_String& line, NPT_Size max_chars, bool break_on_cr) { // clear the line line.SetLength(0); // reserve space for the chars line.Reserve(max_chars); // read the line NPT_Size chars_read = 0; NPT_CHECK_NOLOGTIMEOUT(ReadLine(line.UseChars(), max_chars, &chars_read, break_on_cr)); // adjust the length of the string object line.SetLength(chars_read); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_DateTime::ToString +---------------------------------------------------------------------*/ NPT_String NPT_DateTime::ToString(Format format, NPT_Flags flags) const { NPT_String result; if (NPT_FAILED(CheckDate(*this))) return result; switch (format) { case FORMAT_W3C: AppendNumber(result, m_Year, 4); result += '-'; AppendNumber(result, m_Month, 2); result += '-'; AppendNumber(result, m_Day, 2); result += 'T'; AppendNumber(result, m_Hours, 2); result += ':'; AppendNumber(result, m_Minutes, 2); result += ':'; AppendNumber(result, m_Seconds, 2); if (flags & FLAG_EMIT_FRACTION) { result += '.'; if (flags & FLAG_EXTENDED_PRECISION) { // nanoseconds precision AppendNumber(result, m_NanoSeconds, 9); } else { // only miliseconds precision AppendNumber(result, m_NanoSeconds/1000000, 3); } } if (m_TimeZone) { NPT_UInt32 tz; if (m_TimeZone > 0) { result += '+'; tz = m_TimeZone; } else { result += '-'; tz = -m_TimeZone; } AppendNumber(result, tz/60, 2); result += ':'; AppendNumber(result, tz%60, 2); } else { result += 'Z'; } break; case FORMAT_ANSI: { // compute the number of days elapsed since 1900 NPT_UInt32 days = ElapsedDaysSince1900(*this); // format the result result.SetLength(24); NPT_FormatString(result.UseChars(), result.GetLength()+1, "%.3s %.3s%3d %.2d:%.2d:%.2d %d", NPT_TIME_DAYS_SHORT[(days+1)%7], NPT_TIME_MONTHS[m_Month-1], m_Day, m_Hours, m_Minutes, m_Seconds, m_Year); break; } case FORMAT_RFC_1036: case FORMAT_RFC_1123: { // compute the number of days elapsed since 1900 NPT_UInt32 days = ElapsedDaysSince1900(*this); if (format == FORMAT_RFC_1036) { result += NPT_TIME_DAYS_LONG[(days+1)%7]; result += ", "; AppendNumber(result, m_Day, 2); result += '-'; result += NPT_TIME_MONTHS[m_Month-1]; result += '-'; AppendNumber(result, m_Year%100, 2); } else { result += NPT_TIME_DAYS_SHORT[(days+1)%7]; result += ", "; AppendNumber(result, m_Day, 2); result += ' '; result += NPT_TIME_MONTHS[m_Month-1]; result += ' '; AppendNumber(result, m_Year, 4); } result += ' '; AppendNumber(result, m_Hours, 2); result += ':'; AppendNumber(result, m_Minutes, 2); result += ':'; AppendNumber(result, m_Seconds, 2); if (m_TimeZone) { if (m_TimeZone > 0) { result += " +"; AppendNumber(result, m_TimeZone/60, 2); AppendNumber(result, m_TimeZone%60, 2); } else { result += " -"; AppendNumber(result, -m_TimeZone/60, 2); AppendNumber(result, -m_TimeZone%60, 2); } } else { result += " GMT"; } break; } } return result; }