static err_t Open(urlpart* p, const tchar_t* URL, int Flags) { err_t Result; const tchar_t *String, *Equal; tchar_t Value[MAXPATHFULL]; datetime_t Date = INVALID_DATETIME_T; String = tcsrchr(URL,URLPART_SEP_CHAR); if (!String) return ERR_INVALID_DATA; Clear(p); Node_SetData((node*)p,STREAM_URL,TYPE_STRING,URL); Equal = GetProtocol(URL,NULL,0,NULL); tcsncpy_s(Value,TSIZEOF(Value),Equal,String-Equal); tcsreplace(Value,TSIZEOF(Value),URLPART_SEP_ESCAPE,URLPART_SEPARATOR); Node_SetData((node*)p,URLPART_URL,TYPE_STRING,Value); while (String++ && *String) { Equal = tcschr(String,T('=')); if (Equal) { tchar_t *Sep = tcschr(Equal,T('#')); if (Sep) tcsncpy_s(Value,TSIZEOF(Value),Equal+1,Sep-Equal-1); else tcscpy_s(Value,TSIZEOF(Value),Equal+1); if (tcsncmp(String,T("ofs"),Equal-String)==0) p->Pos = StringToInt(Value,0); else if (tcsncmp(String,T("len"),Equal-String)==0) p->Length = StringToInt(Value,0); else if (tcsncmp(String,T("mime"),Equal-String)==0) Node_SetData((node*)p,URLPART_MIME,TYPE_STRING,Value); else if (tcsncmp(String,T("date"),Equal-String)==0) Date = StringToInt(Value,0); } String = tcschr(String,'#'); } if (Date!=INVALID_DATETIME_T && Date != FileDateTime(Node_Context(p),Node_GetDataStr((node*)p,URLPART_URL))) return ERR_INVALID_DATA; p->Input = GetStream(p,Node_GetDataStr((node*)p,URLPART_URL),Flags); if (!p->Input) return ERR_NOT_SUPPORTED; Stream_Blocking(p->Input,p->Blocking); Result = Stream_Open(p->Input,Node_GetDataStr((node*)p,URLPART_URL),Flags); if (Result == ERR_NONE && p->Pos!=INVALID_FILEPOS_T) // TODO: support asynchronous stream opening { if (Stream_Seek(p->Input,p->Pos,SEEK_SET)!=p->Pos) return ERR_INVALID_DATA; } return Result; }
int GetCodePage(const tchar_t* ContentType) { const tchar_t* p = tcsstr(ContentType,T("CHARSET=")); if (p) { p += 8; if (tcsncmp(p,T("UTF-8"),5)==0) return CP_UTF8; } return Context()->CodePage; }
int TextTest(int verbose, struct cfg *cfg, char *args[]) { tchar buf[1024]; tchar *hello = TEXT("hello"); tchar *a = TEXT("a"); tchar *e = TEXT(""); tchar *cpy; cfg = NULL; *args = NULL; verbose = 1; /* text_length */ T(text_length(NULL, NULL) == 0); T(text_length(hello, NULL) == 0); T(text_length(hello + 3, hello) == 0); T(text_length(hello, hello) == 0); T(text_length(hello, hello + 1) == 0); T(text_length(hello, hello + 4) == 0); T(text_length(a, a + 1) == 0); T(text_length(a, a + 2) == 1); T(text_length(a + 1, a + 2) == 0); T(text_length(hello, hello + 100) == 5); /* text_size */ T(text_size(NULL, NULL) == 0); T(text_size(hello, NULL) == 0); T(text_size(hello + 3, hello) == 0); T(text_size(hello, hello) == 0); T(text_size(hello, hello + 1) == 0); T(text_size(hello, hello + 4) == 0); T(text_size(a, a + 1) == 0); T(text_size(a, a + 2) == (2 * sizeof *a)); T(text_size(a + 1, a + 2) == (1 * sizeof *a)); T(text_size(hello, hello + 100) == (6 * sizeof *hello)); /* text_copy */ T(text_copy(hello, hello + 100, NULL, buf + 1024, 0) == 0); T(text_copy(hello, hello + 100, buf + 3, buf, 0) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(NULL, hello + 100, buf, buf + 1024, 0) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello + 3, hello, buf, buf + 1024, 0) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 100, 1) == 1); T(tcsncmp(TEXT("h"), buf, 100) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 100, 5) == 5); T(tcsncmp(hello, buf, 100) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 1024, 0) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(e, e + 100, buf, buf + 1024, 100) == 0); T(tcsncmp(e, buf, 100) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 1024, 100) == 5); T(tcsncmp(hello, buf, 100) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 1, buf, buf + 1024, 100) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 5, buf, buf + 1024, 100) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 6, buf, buf + 1024, 100) == 5); T(tcsncmp(hello, buf, 100) == 0); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 1, 100) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 5, 100) == 0); T(buf[0] == TEXT('\0')); memset(buf, 'x', 1024 * sizeof(tchar)); T(text_copy(hello, hello + 100, buf, buf + 6, 100) == 5); T(tcsncmp(hello, buf, 100) == 0); /* text_copy_new */ T(text_copy_new(hello, hello + 100, &cpy, 100, NULL) == 5); T(tcsncmp(hello, cpy, 100) == 0); allocator_free(NULL, cpy); T(text_copy_new(hello, hello + 100, NULL, 100, NULL) == 0); T(text_copy_new(NULL, hello + 100, &cpy, 100, NULL) == 0); T(cpy == NULL); T(text_copy_new(hello + 3, hello, &cpy, 100, NULL) == 0); T(cpy == NULL); T(text_copy_new(hello, hello + 100, &cpy, 0, NULL) == 0); T(tcsncmp(e, cpy, 100) == 0); allocator_free(NULL, cpy); T(text_copy_new(hello, hello + 100, &cpy, 4, NULL) == 4); T(tcsncmp(TEXT("hell"), cpy, 100) == 0); allocator_free(NULL, cpy); T(text_copy_new(hello, hello + 100, &cpy, 5, NULL) == 5); T(tcsncmp(hello, cpy, 100) == 0); allocator_free(NULL, cpy); return 0; }
/******************************************************************** * [函数名]: insertCurrentTime * [描述]: 日志当前时间 (eg: YYYY-MM-DD hh:mm:ss) * y: year M: month d: day * h: hour m: minute s: second * [修改记录]: * 2015-05-20,littledj: create * 2015-05-20,littledj: 增加自定义格式 ********************************************************************/ void GLogger::insertCurrentTime(tstring format) { if (format.empty()) format = TEXT("** yyyy-MM-dd hh:mm:ss **\n"); time_t tt = time(0); struct tm *lt = localtime(&tt); tchar* ct = new tchar[format.length() + 4]; tchar tmp[5]; tcscpy(ct, format.c_str()); for (size_t i = 0; i < format.size(); i++) { if (tcsncmp(ct + i, TEXT("yyyy"), 4 ) == 0) { stprintf(tmp, TEXT("%04d"), lt->tm_year + 1900); tcsncpy(ct + i, tmp, 4); i += 3; continue; } if (tcsncmp(ct + i, TEXT("MM"), 2) == 0) { stprintf(tmp, TEXT("%02d"), lt->tm_mon + 1); tcsncpy(ct + i, tmp, 2); i++; continue; } if (tcsncmp(ct + i, TEXT("dd"), 2) == 0) { stprintf(tmp, TEXT("%02d"), lt->tm_mday); tcsncpy(ct + i, tmp, 2); i++; continue; } if (tcsncmp(ct + i, TEXT("hh"), 2) == 0 || tcsncmp(ct + i, TEXT("HH"), 2) == 0) { stprintf(tmp, TEXT("%02d"), lt->tm_hour); tcsncpy(ct + i, tmp, 2); i++; continue; } if (tcsncmp(ct + i, TEXT("mm"), 2) == 0) { stprintf(tmp, TEXT("%02d"), lt->tm_min); tcsncpy(ct + i, tmp, 2); i++; continue; } if (tcsncmp(ct + i, TEXT("ss"), 2) == 0) { stprintf(tmp, TEXT("%02d"), lt->tm_sec); tcsncpy(ct + i, tmp, 2); i++; continue; } } tstring msg = formatMsg(PRINT_TYPE::RAW, TEXT("%s"), ct); delete[] ct; if (m_enableColor) output(msg, m_defaultColor); else output(msg); // 保存当前消息 saveToMessagePool(PRINT_TYPE::RAW, msg); }