Changeset 96c1461 in nscp for include/strEx.h
- Timestamp:
- 11/27/11 22:04:35 (19 months ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 9853bc3
- Parents:
- 16198e3
- File:
-
- 1 edited
-
include/strEx.h (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/strEx.h
r40fca56 r96c1461 82 82 } 83 83 84 template<> 85 inline std::string cvt(std::wstring const & str) { 84 inline std::wstring to_unicode(std::string const & str) { 85 #ifdef WIN32 86 int len = static_cast<int>(str.length()); 87 int nChars = MultiByteToWideChar(CP_ACP, 0, str.c_str(), len, NULL, 0); 88 if (nChars == 0) 89 return L""; 90 wchar_t *buffer = new wchar_t[nChars+1]; 91 if (buffer == NULL) 92 return L""; 93 MultiByteToWideChar(CP_ACP, 0, str.c_str(), len, buffer, nChars); 94 buffer[nChars] = 0; 95 std::wstring buf(buffer, nChars); 96 delete [] buffer; 97 return buf; 98 #else 99 size_t utf8Length = str.length(); 100 size_t outbytesLeft = utf8Length*sizeof(wchar_t); 101 102 //Copy the instring 103 char *inString = new char[str.length()+1]; 104 strcpy(inString, str.c_str()); 105 106 //Create buffer for output 107 char *outString = (char*)new wchar_t[utf8Length+1]; 108 memset(outString, 0, sizeof(wchar_t)*(utf8Length+1)); 109 110 char *inPointer = inString; 111 char *outPointer = outString; 112 113 iconv_t convDesc = iconv_open("WCHAR_T", ""); 114 iconv(convDesc, &inPointer, &utf8Length, &outPointer, &outbytesLeft); 115 iconv_close(convDesc); 116 117 std::wstring retval( (wchar_t *)outString ); 118 119 //Cleanup 120 delete[] inString; 121 delete[] outString; 122 123 return retval; 124 #endif 125 } 126 /* 127 inline std::wstring to_unicode(std::wstring const & str) { 86 128 #ifdef WIN32 87 129 // figure out how many narrow characters we are going to get … … 97 139 return buf; 98 140 #else 141 return str; 142 #endif 143 } 144 */ 145 146 template<> 147 inline std::string cvt(std::wstring const & str) { 148 #ifdef WIN32 149 // figure out how many narrow characters we are going to get 150 int nChars = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), NULL, 0, NULL, NULL); 151 if (nChars == 0) 152 return ""; 153 154 // convert the wide string to a narrow string 155 // nb: slightly naughty to write directly into the string like this 156 std::string buf; 157 buf.resize(nChars); 158 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), const_cast<char*>(buf.c_str()), nChars, NULL, NULL); 159 return buf; 160 #else 99 161 size_t wideSize = sizeof(wchar_t)*str.length(); 100 162 size_t outbytesLeft = wideSize+sizeof(char); //We cannot know how many wide character there is yet … … 128 190 inline std::wstring cvt(std::string const & str) { 129 191 #ifdef WIN32 130 int len = st r.length();131 int nChars = MultiByteToWideChar(CP_ OEMCP, 0, str.c_str(), len, NULL, 0);192 int len = static_cast<int>(str.length()); 193 int nChars = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), len, NULL, 0); 132 194 if (nChars == 0) 133 195 return L""; … … 135 197 if (buffer == NULL) 136 198 return L""; 137 MultiByteToWideChar(CP_ OEMCP, 0, str.c_str(), len, buffer, nChars);199 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), len, buffer, nChars); 138 200 buffer[nChars] = 0; 139 201 std::wstring buf(buffer, nChars); … … 396 458 string = string.substr(0,pos)+with+string.substr(pos+len); 397 459 if (with.find(replace) != std::wstring::npos) // If the replace containes the key look after the replace! 460 pos = string.find(replace, pos+with.length()); 461 else 462 pos = string.find(replace, pos+1); 463 } 464 } 465 inline void replace(std::string &string, const std::string replace, const std::string with) { 466 std::string::size_type pos = string.find(replace); 467 std::string::size_type len = replace.length(); 468 while (pos != std::string::npos) { 469 string = string.substr(0,pos)+with+string.substr(pos+len); 470 if (with.find(replace) != std::string::npos) // If the replace containes the key look after the replace! 398 471 pos = string.find(replace, pos+with.length()); 399 472 else … … 505 578 if (p == std::wstring::npos) 506 579 return value * smallest_unit; 580 else if ( (time[p] == L's') || (time[p] == L'S') ) 581 return value; 582 else if ( (time[p] == L'm') || (time[p] == L'M') ) 583 return value * 60; 584 else if ( (time[p] == L'h') || (time[p] == L'H') ) 585 return value * 60 * 60; 586 else if ( (time[p] == L'd') || (time[p] == L'D') ) 587 return value * 24 * 60 * 60; 588 else if ( (time[p] == L'w') || (time[p] == L'W') ) 589 return value * 7 * 24 * 60 * 60; 590 return value * smallest_unit; 591 } 592 inline unsigned stoui_as_time_sec(std::string time, unsigned int smallest_unit = 1) { 593 std::string::size_type p = time.find_first_of("sSmMhHdDwW"); 594 std::string::size_type pend = time.find_first_not_of("0123456789"); 595 unsigned int value = boost::lexical_cast<unsigned int>(pend==std::string::npos?time:time.substr(0,pend).c_str()); 596 if (p == std::string::npos) 597 return value * smallest_unit; 507 598 else if ( (time[p] == 's') || (time[p] == 'S') ) 508 599 return value; … … 520 611 long neg = 1; 521 612 if (time.length() > 1 && time[0] == L'-') 613 return -(long)stoui_as_time_sec(time.substr(1), smallest_unit); 614 return stoui_as_time_sec(time, smallest_unit); 615 } 616 inline long stol_as_time_sec(std::string time, unsigned int smallest_unit = 1) { 617 long neg = 1; 618 if (time.length() > 1 && time[0] == '-') 522 619 return -(long)stoui_as_time_sec(time.substr(1), smallest_unit); 523 620 return stoui_as_time_sec(time, smallest_unit);
Note: See TracChangeset
for help on using the changeset viewer.








