static int ValidTS(const char *str)/*is timestamp of event valid*/ { if(True==IsSecond(str))return True; if(True==IsTime(str))return True; if(True==IsDateTime(str))return True; return False; }
void ValueGet(Time& x, const Value& v) { if(IsString(v)) { x = IsoTime(v); return; } ValueCheck(IsNull(v) || IsDateTime(v)); x = v; }
void CheckDateTime() { Time tm = GetSysTime(); Date dt = tm; Value c; Value v = tm; RDUMP(v); ASSERT((Date)v == dt); Date xx = v; ASSERT(xx == dt); c = v; RDUMP(c); ASSERT((Date)c == dt); Value cv = v; RDUMP(cv); ASSERT((Date)cv == dt); Value v2 = tm; RDUMP(v2); ASSERT(v2 == v); c = v; RDUMP(c); ASSERT((Date)c == dt); ASSERT(c == tm); v = dt; tm = ToTime(dt); v2 = tm; ASSERT(v == v2); ASSERT(GetHashValue(v) == GetHashValue(v2)); ASSERT(IsDateTime(v)); ASSERT(IsDateTime(c)); ASSERT(tm == v); ASSERT(tm == v2); ASSERT(dt == v); ASSERT(dt == v2); }
static int FormatMatch(const char *str,int format)/*does format of timestamp match format*/ { switch(format) { case TSF_Undefined:return False; case TSF_Seconds:if(False==IsSecond(str))return False;else return True; case TSF_Time:if(False==IsTime(str))return False;else return True; case TSF_DateTime:if(False==IsDateTime(str))return False; else return True; default:return False; } }
String FormatXmlRpcValue(const Value& _v) { String r; Value v = _v; if(v.GetType() == INT64_V) { int64 x = v; if((int)x == x) v = (int)x; } if(IsNull(v) && !IsString(v) && !IsValueArray(v)) r = XmlTag("nil")(); else if(v.GetType() == INT_V) r = XmlTag("int")(Format("%d", (int)v)); else if(v.GetType() == BOOL_V) r = XmlTag("boolean")(AsString((int)(bool)v)); else if(IsNumber(v)) r = XmlTag("double")(Format("%.16g", (double)v)); else if(IsDateTime(v)) { Time t = v; r = XmlTag("dateTime.iso8601") (Format("%04.4d%02.2d%02.2d`T%02.2d`:%02.2d`:%02.2d", t.year, t.month, t.day, t.hour, t.minute, t.second)); } else if(v.GetType() == VALUEMAP_V) { r = "<struct>"; ValueMap vm = v; const Index<Value>& k = vm.GetKeys(); ValueArray va = vm.GetValues(); for(int i = 0; i < k.GetCount(); i++) r << XmlTag("member")(XmlTag("name")(k[i]) + FormatXmlRpcValue(va[i])); r << "</struct>"; } else if(v.GetType() == VALUEARRAY_V) { r = "<array><data>"; ValueArray va = v; for(int i = 0; i < va.GetCount(); i++) r << FormatXmlRpcValue(va[i]); r << "</data></array>"; } else if(v.Is<RawJsonText>()) r = XmlTag("string").Text(v.To<RawJsonText>().json); else r = XmlTag("string").Text(v); return XmlTag("value")(r); }
/* formats: DateTime: yyyy/Mon/dd hh:mm:ss.us Time: hh:mm:ss.us Second: s.us */ static enum TimeStampFormats GetTimeStampFormat(const char *filename) { /*get timestamp format used in file*/ enum TimeStampFormats ret_val = TSF_Undefined; char str[MAXTIMESTAMPLENGTH + 1]; FILE *fp = fopen(filename, FOPEN_READ); if (fp == NULL) { fprintf(stderr, "%s: warning: cannot open %s: %s\n", progname, filename, strerror(errno)); return TSF_Undefined; } if (fgets(str, sizeof(str), fp) != NULL) { if (IsSecond(str)) ret_val = TSF_Seconds; else if (IsTime(str)) ret_val = TSF_Time; else if (IsDateTime(str)) ret_val = TSF_DateTime; } fclose(fp); return ret_val; }
String AsJSON(const Value& v, const String& sep, bool pretty) { String r; if(v.GetType() == VALUEMAP_V) { r << "{"; String sep1; if(pretty) { r << "\r\n"; sep1 = sep + '\t'; } ValueMap m = v; ValueArray va = m.GetValues(); for(int i = 0; i < m.GetCount(); i++) { if(i) { r << ","; if(pretty) r << "\r\n"; } if(pretty) r << sep1; r << AsJSON((String)m.GetKey(i)) << (pretty ? ": " : ":") << AsJSON(va[i], sep1, pretty); } if(pretty) r << "\r\n" << sep; r << "}"; return r; } if(v.GetType() == VALUEARRAY_V) { r << "["; String sep1; if(pretty) { r << "\r\n"; sep1 = sep + '\t'; } ValueArray va = v; for(int i = 0; i < va.GetCount(); i++) { if(i) { r << ","; if(pretty) r << "\r\n"; } if(pretty) r << sep1; r << AsJSON(va[i], sep1, pretty); } if(pretty) r << "\r\n" << sep; r << "]"; return r; } if(IsNumber(v) && IsNull(v)) return "null"; if(v.GetType() == INT_V) return Format("%d", (int)v); if(v.GetType() == BOOL_V) return (bool)v ? "true" : "false"; if(IsNumber(v)) return Format("%.16g", (double)v); if(IsString(v)) return AsCString((String)v, INT_MAX, NULL, ASCSTRING_JSON); if(IsDateTime(v)) return AsJSON((Time)v); if(IsNull(v)) return "null"; NEVER_("Non-JSON value in AsJSON: " + v.GetTypeName()); return "null"; }
Value ConvertDate::Format(const Value& q) const { if(IsDateTime(q)) return Convert::Format((Date)q); return Convert::Format(q); }
void VfkStream::ScanFile(int fx) { RTIMING("VfkStream::ScanFile"); Stream& strm = streams[fx]; int64 last_line = strm.GetSize(); while(last_line > 0) { strm.Seek(last_line - 1); if(strm.Get() == '\n') break; last_line--; } strm.Seek(0); try { int c; int64 rowpos = strm.GetPos(); while((c = strm.Get()) == '&' && ((c = strm.Get()) == 'H' || c == 'D') && IsAlpha(strm.Term())) { char type = c; int64 begin = strm.GetPos(); SkipRow(strm); rowpos = strm.GetPos(); int len = (int)(strm.GetPos() - begin); StringBuffer linebuf(len + 1); strm.Seek(begin); strm.Get(linebuf, len); linebuf[len] = 0; const char *b = linebuf; const char *id = b; while(IsIdent(*++b)) ; String ident(id, b); if(*b++ != ';') throw Exc(NFormat("';' expected after '%s' (found: '%c', %2:02x)", ident, *b)); if(type == 'D') { String fident = "X_" + ident; int f = tables.Find(fident); if(f < 0) throw Exc(NFormat("unexpected data for filter table '%s'", ident)); // b = ScanRow(b, tables[f]); } else if(IsAlpha(*b)) { String fident = "X_" + ident; Table& tbl = tables.GetAdd(fident); tbl.name = tbl.rawname = fident; tbl.row_count = 0; ScanHeader(b, tbl); } else { do { Vector<Value> row; row.SetCount(HDR_COUNT); if(*b == '\"') { WString text = ReadString(b, &b); if(IsDateTime(ident) && !IsNull(text)) { Time dt = VfkReadTime(text.ToString(), NULL); if(IsNull(dt)) throw Exc(NFormat("invalid date/time value %s", AsCString(text.ToString()))); row[HDR_DTM] = dt; } else { row[HDR_STR] = text; if(ident == "CODEPAGE") if(text == WString("EE8MSWIN1250")) charset = CHARSET_WIN1250; } } else { double num = ScanDouble(b, &b); if(IsNull(num)) throw Exc("invalid numeric value"); row[HDR_NUM] = num; } int l = header.FindLast(ident); row[HDR_ID] = ident; row[HDR_ORD] = (l >= 0 ? (int)header[l][HDR_ORD] + 1 : 0); header.Add(ident) = row; } while(*b++ == ';'); b--; } } strm.Seek(rowpos); while(strm.Get() == '&' && strm.Get() == 'B' && IsAlpha(strm.Term())) { int64 header_offset = strm.GetPos(); SkipRow(strm); int64 begin_offset = strm.GetPos(); int len = (int)(begin_offset - header_offset); Buffer<char> linebuf(len + 1); strm.Seek(header_offset); strm.Get(linebuf, len); linebuf[len] = 0; const char *b = linebuf; const char *id = b; while(IsIdent(*++b)) ; int idlen = b - id; String ident(id, b); if(*b++ != ';') throw Exc(NFormat("';' expected after '%s' (found: '%c', %2:02x)", ident, *b)); String name = ident; for(const VFKLongName *ln = vfk_long_names; ln->shortname; ln++) if(name == ln->shortname) { name = ln->longname; break; } Table& tbl = tables.GetAdd(name); tbl.name = name; tbl.rawname = ident; ScanHeader(b, tbl); int64 p = begin_offset, e = last_line; Buffer<char> idbuf(idlen + 3); while(p < e) { int64 m = (p + e) >> 1; while(m > p) { char part[100]; int partsize = (int)min<int64>(m - p, sizeof(part)); strm.Seek(m - partsize); strm.Get(part, partsize); const char *x = &part[partsize]; while(x > part && x[-1] != '\n') x--; int lfpos = x - part; if(x > part && --x > part && x[-1] == '\r') x--; m -= partsize - lfpos; if(x <= part) continue; if(*--x != '\xA4') break; m -= lfpos - (x - part); } strm.Seek(m); if(strm.Get(idbuf, idlen + 3) != idlen + 3 || idbuf[0] != '&' || idbuf[1] != 'D' || memcmp(~idbuf + 2, id, idlen) || idbuf[idlen + 2] != ';') e = m; else { SkipRow(strm); p = strm.GetPos(); } } int xgrp = file_groups.GetKey(fx); int f; for(f = 0; f < tbl.file_index.GetCount(); f++) if(file_groups.GetKey(tbl.file_index[f]) == xgrp) break; if(f >= tbl.file_index.GetCount()) { tbl.file_index.Add(fx); tbl.begin_offset.Add(begin_offset); tbl.end_offset.Add(p); } strm.Seek(p); } } catch(Exc e) { throw Exc(NFormat("%s (offset %n): %s", file_groups[fx], strm.GetPos(), e)); } }
Value JsonRpcData(const Value& v) { if(IsDateTime(v) && !IsNull(v)) return FormatIso8601(v); return v; }