LPCSTR CMainApp::UnFormat(CString strTmp) { m_strBuffer2 = strTmp; int i = m_strBuffer2.GetLength(); char * pStr = m_strBuffer2.GetBuffer(0); char * pNext; while(*pStr) { if(*pStr=='\\' && !IsDBCSLeadByteEx(m_uiCodePage, *pStr)) { pNext = CharNextExA(m_uiCodePage, pStr, 0); switch(*pNext) { case '\\': *pStr = '\\'; break; case 't': *pStr = '\t'; break; case 'n': *pStr = '\n'; break; case 'r': *pStr = '\r'; break; default: break; } pStr = pNext; pNext = CharNextExA(m_uiCodePage, pNext, 0); memmove(pStr, pNext, --i); } else { pStr = CharNextExA(m_uiCodePage, pStr, 0); i--; } } m_strBuffer2.ReleaseBuffer(-1); return m_strBuffer2; }
WCHAR *WCMD_fgets(WCHAR *buf, DWORD noChars, HANDLE h) { DWORD charsRead; BOOL status; DWORD i; /* We can't use the native f* functions because of the filename syntax differences between DOS and Unix. Also need to lose the LF (or CRLF) from the line. */ if (!WCMD_is_console_handle(h)) { LARGE_INTEGER filepos; char *bufA; UINT cp; const char *p; cp = GetConsoleCP(); bufA = heap_alloc(noChars); /* Save current file position */ filepos.QuadPart = 0; SetFilePointerEx(h, filepos, &filepos, FILE_CURRENT); status = ReadFile(h, bufA, noChars, &charsRead, NULL); if (!status || charsRead == 0) { heap_free(bufA); return NULL; } /* Find first EOL */ for (p = bufA; p < (bufA + charsRead); p = CharNextExA(cp, p, 0)) { if (*p == '\n' || *p == '\r') break; } /* Sets file pointer to the start of the next line, if any */ filepos.QuadPart += p - bufA + 1 + (*p == '\r' ? 1 : 0); SetFilePointerEx(h, filepos, NULL, FILE_BEGIN); i = MultiByteToWideChar(cp, 0, bufA, p - bufA, buf, noChars); heap_free(bufA); } else { status = WCMD_ReadFile(h, buf, noChars, &charsRead); if (!status || charsRead == 0) return NULL; /* Find first EOL */ for (i = 0; i < charsRead; i++) { if (buf[i] == '\n' || buf[i] == '\r') break; } } /* Truncate at EOL (or end of buffer) */ if (i == noChars) i--; buf[i] = '\0'; return buf; }
QString QWindowsLocalCodec::convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const { if (!chars || !length) return QString(); int copyLocation = 0; int extra = 2; if (state && state->remainingChars) { copyLocation = state->remainingChars; extra += copyLocation; } int newLength = length + extra; char *mbcs = new char[newLength]; //ensure that we have a NULL terminated string mbcs[newLength-1] = 0; mbcs[newLength-2] = 0; memcpy(&(mbcs[copyLocation]), chars, length); if (copyLocation) { //copy the last character from the state mbcs[0] = (char)state->state_data[0]; state->remainingChars = 0; } const char *mb = mbcs; #ifndef Q_OS_WINCE const char *next = 0; QString s; while((next = CharNextExA(CP_ACP, mb, 0)) != mb) { wchar_t wc[2] = {0}; int charlength = next - mb; int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2); if (len>0) { s.append(QChar(wc[0])); } else { int r = GetLastError(); //check if the character being dropped is the last character if (r == ERROR_NO_UNICODE_TRANSLATION && mb == (mbcs+newLength -3) && state) { state->remainingChars = 1; state->state_data[0] = (char)*mb; } } mb = next; } #else QString s; int size = mbstowcs(NULL, mb, length); if (size < 0) { Q_ASSERT("Error in CE TextCodec"); return QString(); } wchar_t* ws = new wchar_t[size + 2]; ws[size +1] = 0; ws[size] = 0; size = mbstowcs(ws, mb, length); for (int i=0; i< size; i++) s.append(QChar(ws[i])); delete [] ws; #endif delete mbcs; return s; }
/*********************************************************************** * CharPrevExA (USER32.@) */ LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags ) { while (*start && (start < ptr)) { LPCSTR next = CharNextExA( codepage, start, flags ); if (next >= ptr) break; start = next; } return (LPSTR)start; }
void ConvertBackslashes(char *str, UINT fileCp) { char *pstr; for ( pstr = str; *pstr; pstr = CharNextExA( fileCp, pstr, 0 )) { if( *pstr == '\\' ) { switch( pstr[1] ) { case 'n': *pstr = '\n'; break; case 't': *pstr = '\t'; break; case 'r': *pstr = '\r'; break; default: *pstr = pstr[1]; break; } memmove(pstr+1, pstr+2, strlen(pstr+2) + 1); } } }
LPCSTR CMainApp::Format(CString strTmp) { char * pStr = strTmp.GetBuffer(0); char * pDest = m_strBuffer2.GetBuffer(MAX_STR_SIZE); char * pNext; while(*pStr) { if(!IsDBCSLeadByteEx(m_uiCodePage, *pStr)) { switch(*pStr) { case '\\': *pDest++ = '\\'; *pDest++ = '\\'; break; case '\t': *pDest++ = '\\'; *pDest++ = 't'; break; case '\r': *pDest++ = '\\'; *pDest++ = 'r'; break; case '\n': *pDest++ = '\\'; *pDest++ = 'n'; break; default: *pDest++ = *pStr; break; } } else { memcpy( pDest, pStr, 2 ); pDest += 2; } pStr = CharNextExA(m_uiCodePage, pStr, 0); } *pDest = '\0'; m_strBuffer2.ReleaseBuffer(-1); return m_strBuffer2; }
void testString(LPSTR input, LPSTR expected) { LPTSTR pReturned = NULL; pReturned = CharNextExA(0,input,0); /* Compare the Returned String to what it should be */ if(strcmp(expected,pReturned) != 0) { Fail("ERROR: CharNextExA Failed: [%s] and [%s] are not equal, " "they should be after calling CharNextExA.\n", pReturned,expected); } }
static int ConvertBackslashes(char *str, UINT fileCp) { int shift = 0; char *pstr; for (pstr = str; *pstr; pstr = CharNextExA(fileCp, pstr, 0)) { if (*pstr == '\\') { shift++; switch (pstr[1]) { case 'n': *pstr = '\n'; break; case 't': *pstr = '\t'; break; case 'r': *pstr = '\r'; break; case 's': *pstr = ' '; break; default: *pstr = pstr[1]; break; } memmove(pstr + 1, pstr + 2, strlen(pstr + 2) + 1); } } return shift; }
int __cdecl main(int argc, char *argv[]) { char * AnExampleString = "this is the string"; char * StringPointer = AnExampleString; int count = 0; /* * Initialize the PAL and return FAILURE if this fails */ if(0 != (PAL_Initialize(argc, argv))) { return FAIL; } /* Use CharNext to move through an entire string. Ensure the pointer * that is returned points to the correct character, by comparing it with * 'stringPointer' which isn't touched by CharNext. */ while(*AnExampleString != 0) { /* Fail if any characters are different. This is comparing the * addresses of both characters, not the characters themselves. */ if(AnExampleString != &StringPointer[count]) { Fail("ERROR: %#x and %#x are different. These should be the " "same address.\n",AnExampleString,&StringPointer[count]); } AnExampleString = CharNextExA(0,AnExampleString,0); ++count; } PAL_Terminate(); return PASS; }
//================================================================================================ //-------------+++--> Format T-Clock's OutPut String From Current Date, Time, & System Information: unsigned MakeFormat(char buf[FORMAT_MAX_SIZE], const char* fmt, SYSTEMTIME* pt, int beat100) //------------------+++--> { const char* bufend = buf+FORMAT_MAX_SIZE; const char* pos; char* out = buf; ULONGLONG TickCount = 0; while(*fmt) { if(*fmt == '"') { for(++fmt; *fmt&&*fmt!='"'; ) *out++ = *fmt++; if(*fmt) ++fmt; continue; } if(*fmt=='\\' && fmt[1]=='n') { fmt+=2; *out++='\n'; } /// for testing else if(*fmt == 'S' && fmt[1] == 'S' && (fmt[2] == 'S' || fmt[2] == 's')) { fmt += 3; out += api.WriteFormatNum(out, (int)pt->wSecond, 2, 0); *out++ = '.'; out += api.WriteFormatNum(out, (int)pt->wMilliseconds, 3, 0); } else if(*fmt == 'y' && fmt[1] == 'y') { int len; len = 2; if(*(fmt + 2) == 'y' && *(fmt + 3) == 'y') len = 4; out += api.WriteFormatNum(out, (len==2)?(int)pt->wYear%100:(int)pt->wYear, len, 0); fmt += len; } else if(*fmt == 'm') { if(*(fmt + 1) == 'm' && *(fmt + 2) == 'e') { *out++ = m_MonthEng[pt->wMonth-1][0]; *out++ = m_MonthEng[pt->wMonth-1][1]; *out++ = m_MonthEng[pt->wMonth-1][2]; fmt += 3; } else if(fmt[1] == 'm' && fmt[2] == 'm') { if(*(fmt + 3) == 'm') { fmt += 4; for(pos=m_MonthLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_MonthShort; *pos; ) *out++=*pos++; } } else { if(fmt[1] == 'm') { fmt += 2; *out++ = (char)((int)pt->wMonth / 10) + '0'; } else { ++fmt; if(pt->wMonth > 9) *out++ = (char)((int)pt->wMonth / 10) + '0'; } *out++ = (char)((int)pt->wMonth % 10) + '0'; } } else if(*fmt == 'a' && fmt[1] == 'a' && fmt[2] == 'a') { if(*(fmt + 3) == 'a') { fmt += 4; for(pos=m_DayOfWeekLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_DayOfWeekShort; *pos; ) *out++=*pos++; } } else if(*fmt=='d') { if(fmt[1]=='d' && fmt[2]=='e'){ fmt+=3; for(pos=m_DayOfWeekEng[pt->wDayOfWeek]; *pos; ) *out++=*pos++; }else if(fmt[1]=='d' && fmt[2]=='d') { fmt+=3; if(*fmt=='d'){ ++fmt; pos=m_DayOfWeekLong; }else{ pos=m_DayOfWeekShort; } for(; *pos; ) *out++=*pos++; }else{ if(fmt[1]=='d') { fmt+=2; *out++ = (char)((int)pt->wDay / 10) + '0'; }else{ ++fmt; if(pt->wDay > 9) *out++ = (char)((int)pt->wDay / 10) + '0'; } *out++ = (char)((int)pt->wDay % 10) + '0'; } } else if(*fmt=='h') { int hour = pt->wHour; while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; if(fmt[1] == 'h') { fmt += 2; *out++ = (char)(hour / 10) + '0'; } else { ++fmt; if(hour > 9) *out++ = (char)(hour / 10) + '0'; } *out++ = (char)(hour % 10) + '0'; } else if(*fmt=='H') { if(fmt[1] == 'H') { fmt += 2; *out++ = (char)(pt->wHour / 10) + '0'; } else { ++fmt; if(pt->wHour > 9) *out++ = (char)(pt->wHour / 10) + '0'; } *out++ = (char)(pt->wHour % 10) + '0'; } else if((*fmt=='w'||*fmt=='W') && (fmt[1]=='+'||fmt[1]=='-')) { char is_12h = (*fmt == 'w'); char is_negative = (*++fmt == '-'); int hour = 0; for(; *++fmt<='9'&&*fmt>='0'; ){ hour *= 10; hour += *fmt-'0'; } if(is_negative) hour = -hour; hour = (pt->wHour + hour)%24; if(hour < 0) hour += 24; if(is_12h){ while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; } *out++ = (char)(hour / 10) + '0'; *out++ = (char)(hour % 10) + '0'; } else if(*fmt == 'n') { if(fmt[1] == 'n') { fmt += 2; *out++ = (char)((int)pt->wMinute / 10) + '0'; } else { ++fmt; if(pt->wMinute > 9) *out++ = (char)((int)pt->wMinute / 10) + '0'; } *out++ = (char)((int)pt->wMinute % 10) + '0'; } else if(*fmt == 's') { if(fmt[1] == 's') { fmt += 2; *out++ = (char)((int)pt->wSecond / 10) + '0'; } else { ++fmt; if(pt->wSecond > 9) *out++ = (char)((int)pt->wSecond / 10) + '0'; } *out++ = (char)((int)pt->wSecond % 10) + '0'; } else if(*fmt == 't' && fmt[1] == 't') { fmt += 2; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else if(*fmt == 'A' && fmt[1] == 'M') { if(fmt[2] == '/' && fmt[3] == 'P' && fmt[4] == 'M') { if(pt->wHour < 12) *out++ = 'A'; //--+++--> 2010 - Noon / MidNight Decided Here! else *out++ = 'P'; *out++ = 'M'; fmt += 5; } else if(fmt[2] == 'P' && fmt[3] == 'M') { fmt += 4; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else *out++ = *fmt++; } else if(*fmt == 'a' && fmt[1] == 'm' && fmt[2] == '/' && fmt[3] == 'p' && fmt[4] == 'm') { if(pt->wHour < 12) *out++ = 'a'; else *out++ = 'p'; *out++ = 'm'; fmt += 5; } // internet time else if(*fmt == '@' && fmt[1] == '@' && fmt[2] == '@') { fmt += 3; *out++ = '@'; *out++ = (char)(beat100 / 10000) + '0'; *out++ = (char)((beat100 % 10000) / 1000) + '0'; *out++ = (char)((beat100 % 1000) / 100) + '0'; if(*fmt=='.' && fmt[1]=='@') { fmt += 2; *out++ = '.'; *out++ = (char)((beat100 % 100) / 10) + '0'; if(*fmt=='@'){ ++fmt; *out++ = (char)((beat100 % 10)) + '0'; } } } // alternate calendar else if(*fmt == 'Y' && m_AltYear > -1) { int n = 1; while(*fmt == 'Y') { n *= 10; ++fmt; } if(n < m_AltYear) { n = 1; while(n < m_AltYear) n *= 10; } for(;;) { *out++ = (char)((m_AltYear % n) / (n/10)) + '0'; if(n == 10) break; n /= 10; } } else if(*fmt == 'g') { for(pos=m_EraStr; *pos&&*fmt=='g'; ){ char* p2 = CharNextExA(m_codepage, pos, 0); while(pos != p2) *out++ = *pos++; ++fmt; } while(*fmt == 'g') fmt++; } else if(*fmt == 'L' && strncmp(fmt, "LDATE", 5) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 5; } else if(*fmt == 'D' && strncmp(fmt, "DATE", 4) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'T' && strncmp(fmt, "TIME", 4) == 0) { GetTimeFormat(LOCALE_USER_DEFAULT, 0, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'S') { // uptime int width, padding, num; const char* old_fmt = ++fmt; char specifier = api.GetFormat(&fmt, &width, &padding); if(!TickCount) TickCount = api.GetTickCount64(); switch(specifier){ case 'd'://days num = (int)(TickCount/86400000); break; case 'a'://hours total num = (int)(TickCount/3600000); break; case 'h'://hours (max 24) num = (TickCount/3600000)%24; break; case 'n'://minutes num = (TickCount/60000)%60; break; case 's'://seconds num = (TickCount/1000)%60; break; case 'T':{// ST, uptime as h:mm:ss ULONGLONG past = TickCount/1000; int hour, minute; num = past%60; past /= 60; minute = past%60; past /= 60; hour = (int)past; out += api.WriteFormatNum(out, hour, width, padding); *out++ = ':'; out += api.WriteFormatNum(out, minute, 2, 0); *out++ = ':'; width = 2; padding = 0; break;} default: specifier = '\0'; fmt = old_fmt; *out++ = 'S'; } if(specifier) out += api.WriteFormatNum(out, num, width, padding); } else if(*fmt == 'W') { // Week-of-Year struct tm tmnow; time_t ts = time(NULL); localtime_r(&ts, &tmnow); ++fmt; if(*fmt == 's') { // Week-Of-Year Starts Sunday out += strftime(out, bufend-out, "%U", &tmnow); ++fmt; } else if(*fmt == 'm') { // Week-Of-Year Starts Monday out += strftime(out, bufend-out, "%W", &tmnow); ++fmt; } else if(*fmt == 'i') { // ISO-8601 week (1st version by henriko.se, 2nd by White-Tiger) int wday,borderdays,week; for(;;){ wday = (!tmnow.tm_wday?6:tmnow.tm_wday-1); // convert from Sun-Sat to Mon-Sun (0-5) borderdays = (tmnow.tm_yday + 7 - wday) % 7; // +7 to prevent it from going negative week = (tmnow.tm_yday + 6 - wday) / 7; if(borderdays >= 4){ // year starts with at least 4 days ++week; } else if(!week){ // we're still in last year's week --tmnow.tm_year; tmnow.tm_mon = 11; tmnow.tm_mday = 31; tmnow.tm_isdst = -1; if(mktime(&tmnow)==-1){ // mktime magically updates tm_yday, tm_wday week = 1; break; // fail safe } tmnow.tm_mon = 0; // just to speed up the "if" below, since we know that it can't be week 1 continue; // repeat (once) } if(tmnow.tm_mon==11 && tmnow.tm_mday>=29){ // end of year, could be week 1 borderdays = 31 - tmnow.tm_mday + wday; if(borderdays < 3) week = 1; } break; } out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'u') { int week = 1 + (tmnow.tm_yday + 6 - tmnow.tm_wday) / 7; out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'w') { // SWN (Simple Week Number) out += wsprintf(out,"%d",1 + tmnow.tm_yday / 7); ++fmt; } } //================================================================================================ //======================================= JULIAN DATE Code ======================================== else if(*fmt == 'J' && *(fmt + 1) == 'D') { double y, M, d, h, m, s, bc, JD; struct tm Julian; int id, is, i=0; char* szJulian; time_t UTC = time(NULL); gmtime_r(&UTC, &Julian); y = Julian.tm_year +1900; // Year M = Julian.tm_mon +1; // Month d = Julian.tm_mday; // Day h = Julian.tm_hour; // Hours m = Julian.tm_min; // Minutes s = Julian.tm_sec; // Seconds // This Handles the January 1, 4713 B.C up to bc = 100.0 * y + M - 190002.5; // Year 0 Part. JD = 367.0 * y; JD -= floor(7.0*(y + floor((M+9.0)/12.0))/4.0); JD += floor(275.0*M/9.0); JD += d; JD += (h + (m + s/60.0)/60.0)/24.0; JD += 1721013.5; // BCE 2 November 18 00:00:00.0 UT - Tuesday JD -= 0.5*bc/fabs(bc); JD += 0.5; szJulian = _fcvt(JD, 4, &id, &is); // Make it a String while(*szJulian) { if(i == id) { //--//-++-> id = Decimal Point Precision/Position *out++ = '.'; // ReInsert the Decimal Point Where it Belongs. } else { *out++ = *szJulian++; //--+++--> Done! } i++; } fmt +=2; } //================================================================================================ //======================================= ORDINAL DATE Code ======================================= else if(*fmt == 'O' && *(fmt + 1) == 'D') { //--------+++--> Ordinal Date UTC: char szOD[16] = {0}; struct tm today; time_t UTC = time(NULL); char* od; gmtime_r(&UTC, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'O' && *(fmt + 1) == 'd') { //------+++--> Ordinal Date Local: char szOD[16] = {0}; struct tm today; time_t ts = time(NULL); char* od; localtime_r(&ts, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'D' && strncmp(fmt, "DOY", 3) == 0) { //--+++--> Day-Of-Year: char szDoy[8] = {0}; struct tm today; time_t ts = time(NULL); char* doy; localtime_r(&ts, &today); strftime(szDoy, 8, "%j", &today); doy = szDoy; while(*doy) *out++ = *doy++; fmt +=3; } //========================================================================== else if(*fmt == 'P' && strncmp(fmt, "POSIX", 5) == 0) { //-> Posix/Unix Time: char szPosix[16] = {0}; // This will Give the Number of Seconds That Have char* posix; //--+++--> Elapsed Since the Unix Epoch: 1970-01-01 00:00:00 wsprintf(szPosix, "%ld", time(NULL)); posix = szPosix; while(*posix) *out++ = *posix++; fmt +=5; } //========================================================================== else if(*fmt == 'T' && strncmp(fmt, "TZN", 3) == 0) { //--++-> TimeZone Name: #ifndef __GNUC__ /* forces us to link with msvcr100 */ char szTZName[TZNAME_MAX] = {0}; size_t lRet; char* tzn; int iDST; _get_daylight(&iDST); if(iDST) { _get_tzname(&lRet, szTZName, TZNAME_MAX, 1); } else { _get_tzname(&lRet, szTZName, TZNAME_MAX, 0); } tzn = szTZName; while(*tzn) *out++ = *tzn++; #endif fmt +=3; } //================================================================================================= else { for(pos=CharNext(fmt); fmt!=pos; ) *out++=*fmt++; } } *out='\0'; return (unsigned)(out-buf); }