FILE* lame_fopen(char const* file, char const* mode) { FILE* fh = 0; wchar_t* wfile = utf8ToUnicode(file); wchar_t* wmode = utf8ToUnicode(mode); if (wfile != 0 && wmode != 0) { fh = _wfopen(wfile, wmode); } else { fh = fopen(file, mode); } free(wfile); free(wmode); return fh; }
void Path::remove(const std::string & path) { NTA_CHECK(!path.empty()) << "Can't remove an empty path"; // Just return if it doesn't exist already if (!Path::exists(path)) return; if (isDirectory(path)) { Directory::removeTree(path); return; } #ifdef WIN32 std::wstring wpath(utf8ToUnicode(path)); BOOL res = ::DeleteFile(/*(LPCTSTR)*/wpath.c_str()); if (res == FALSE) NTA_THROW << "Path::remove() -- unable to delete '" << path << "' error message: " << OS::getErrorMessage(); #else int res = ::remove(path.c_str()); if (res != 0) NTA_THROW << "Path::remove() -- unable to delete '" << path << "' error message: " << OS::getErrorMessage(); #endif }
int_r KCharset::countChar(const char* str, size_t len) const { int_r count = 0; unsigned int uchar; if(g_encoding == encode_utf8) { while(len > 0) { int n = utf8ToUnicode(str, &uchar); if(!n) return count; str += n; len -= n; count++; } return count; } else { while(len > 0) { int n = this->to_uchar(str, (int)len, &uchar); if(!n) return count; str += n; len -= n; count++; } return count; } }
inline bool decode(const string& str, Unicode& res) { #ifdef CPPJIEBA_GBK return gbkTrans(str, res); #else return utf8ToUnicode(str, res); #endif }
inline bool decode(const string& str, vector<uint16_t>& vec) { #ifdef CPPJIEBA_GBK return gbkTrans(str, vec); #else return utf8ToUnicode(str, vec); #endif }
/* ** Convert a UTF-8 filename into whatever form the underlying ** operating system wants filenames in. Space to hold the result ** is obtained from sqliteMalloc and must be freed by the calling ** function. */ static void *convertUtf8Filename(const char *zFilename){ void *zConverted = 0; if( isNT() ){ zConverted = utf8ToUnicode(zFilename); }else{ zConverted = utf8ToMbcs(zFilename); } /* caller will handle out of memory */ return zConverted; }
void Path::rename(const std::string & oldPath, const std::string & newPath) { NTA_CHECK(!oldPath.empty() && !newPath.empty()) << "Can't rename to/from empty path"; #ifdef WIN32 std::wstring wOldPath(utf8ToUnicode(oldPath)); std::wstring wNewPath(utf8ToUnicode(newPath)); BOOL res = ::MoveFile(/*(LPCTSTR)*/wOldPath.c_str(), /*(LPCTSTR)*/wNewPath.c_str()); if (res == FALSE) NTA_THROW << "Path::rename() -- unable to rename '" << oldPath << "' to '" << newPath << "' error message: " << OS::getErrorMessage(); #else int res = ::rename(oldPath.c_str(), newPath.c_str()); if (res == -1) NTA_THROW << "Path::rename() -- unable to rename '" << oldPath << "' to '" << newPath << "' error message: " << OS::getErrorMessage(); #endif }
void Path::setPermissions(const std::string &path, bool userRead, bool userWrite, bool groupRead, bool groupWrite, bool otherRead, bool otherWrite ) { if(Path::isDirectory(path)) { Directory::Iterator iter(path); Directory::Entry e; while(iter.next(e)) { std::string sub = Path::join(path, e.path); setPermissions(sub, userRead, userWrite, groupRead, groupWrite, otherRead, otherWrite); } } #if WIN32 int countFailure = 0; std::wstring wpath(utf8ToUnicode(path)); DWORD attr = GetFileAttributes(wpath.c_str()); if(attr != INVALID_FILE_ATTRIBUTES) { if(userWrite) attr &= ~FILE_ATTRIBUTE_READONLY; BOOL res = SetFileAttributes(wpath.c_str(), attr); if(!res) { NTA_WARN << "Path::setPermissions: Failed to set attributes for " << path; ++countFailure; } } else { NTA_WARN << "Path::setPermissions: Failed to get attributes for " << path; ++countFailure; } if(countFailure > 0) { NTA_THROW << "Path::setPermissions failed for " << path; } #else mode_t mode = 0; if (userRead) mode |= S_IRUSR; if (userWrite) mode |= S_IRUSR; if (groupRead) mode |= S_IRGRP; if (groupWrite) mode |= S_IWGRP; if (otherRead) mode |= S_IROTH; if (otherWrite) mode |= S_IWOTH; chmod(path.c_str(), mode); #endif }
/* ** Convert UTF-8 to multibyte character string. Space to hold the ** returned string is obtained from sqliteMalloc(). */ static char *utf8ToMbcs(const char *zFilename){ char *zFilenameMbcs; WCHAR *zTmpWide; zTmpWide = utf8ToUnicode(zFilename); if( zTmpWide==0 ){ return 0; } zFilenameMbcs = unicodeToMbcs(zTmpWide); sqliteFree(zTmpWide); return zFilenameMbcs; }
char* lame_getenv(char const* var) { char* str = 0; wchar_t* wvar = utf8ToUnicode(var); wchar_t* wstr = 0; if (wvar != 0) { wstr = _wgetenv(wvar); str = unicodeToUtf8(wstr); } free(wvar); free(wstr); return str; }
int KCharset::ReadOneChar(const void* p, int len, int& chr) const { int n; if(g_encoding == encode_utf8) { n = utf8ToUnicode((char*)p, (charset::uchar32*)&chr); } else { n = this->to_uchar((char*)p, len, (charset::uchar32*)&chr); } return n > 0 ? n : 0; }
int KCharset::from_utf8(const char** srcP, int* srclenP, char** dstP, int* dstlenP) const { char* src = (char*)*srcP; char* dst = *dstP; size_t srclen = *srclenP, dstlen = *dstlenP; #if defined(WIN32) unsigned int uchar = 0; char ctmp[8]; while(src[0] && srclen > 0) { int n = utf8ToUnicode(src, &uchar); if(n < 0) { *srcP = src; *srclenP = (int)srclen; *dstP = dst; *dstlenP = (int)dstlen; return -1; } int n2 = this->from_uchar(uchar, ctmp, sizeof(ctmp)); if(n2 < 0) { *srcP = src; *srclenP = (int)srclen; *dstP = dst; *dstlenP = (int)dstlen; return -1; } if((int)dstlen < n2) break; memcpy(dst, ctmp, n2); src += n; srclen -= n; dst += n2; dstlen -= n2; } int wbytes = (int)(dst - *dstP); if(dstlen > 0) *dst = '\0'; *srcP = src; *srclenP = (int)srclen; *dstP = dst; *dstlenP = (int)dstlen; return wbytes; #else size_t n = iconv(m_iconv_utf8_r, &src, &srclen, dstP, &dstlen); if(n == -1) { *srcP = src; *srclenP = srclen; *dstlenP = dstlen; return -1; } if(dstlen > 0) (*dstP)[0] = '\0'; *srcP = src; *srclenP = srclen; *dstlenP = dstlen; return (int)(*dstP - dst); #endif }
wchar_t* getFileContent() { ListHead head; init(&head); FILE *stream; errno_t err; char a; err = fopen_s(&stream, "a.txt", "rb"); if (err != 0) { printf("not opened.\n"); } a = fgetc(stream); while (a != EOF) { add(&head, a); a = fgetc(stream); } add(&head, '\0'); char* utf8Str = list2Chars(&head); wchar_t* unicodeStr = utf8ToUnicode(utf8Str); return unicodeStr; }
/*! Matches the string against the given wildcard pattern. Returns either MATCH_OK, or NO_MATCH when everything went fine, or values < 0 (see enum at the top of Query.cpp) if an error occurs. */ status_t matchString(char* pattern, char* string) { while (*pattern) { // end of string == valid end of pattern? if (!string[0]) { while (pattern[0] == '*') pattern++; return !pattern[0] ? MATCH_OK : NO_MATCH; } switch (*pattern++) { case '?': { // match exactly one UTF-8 character; we are // not interested in the result utf8ToUnicode(&string); break; } case '*': { // compact pattern while (true) { if (pattern[0] == '?') { if (!*++string) return NO_MATCH; } else if (pattern[0] != '*') break; pattern++; } // if the pattern is done, we have matched the string if (!pattern[0]) return MATCH_OK; while(true) { // we have removed all occurences of '*' and '?' if (pattern[0] == string[0] || pattern[0] == '[' || pattern[0] == '\\') { status_t status = matchString(pattern, string); if (status < B_OK || status == MATCH_OK) return status; } // we could be nice here and just jump to the next // UTF-8 character - but we wouldn't gain that much // and it'd be slower (since we're checking for // equality before entering the recursion) if (!*++string) return NO_MATCH; } break; } case '[': { bool invert = false; if (pattern[0] == '^' || pattern[0] == '!') { invert = true; pattern++; } if (!pattern[0] || pattern[0] == ']') return MATCH_BAD_PATTERN; uint32 c = utf8ToUnicode(&string); bool matched = false; while (pattern[0] != ']') { if (!pattern[0]) return MATCH_BAD_PATTERN; if (pattern[0] == '\\') pattern++; uint32 first = utf8ToUnicode(&pattern); // Does this character match, or is this a range? if (first == c) { matched = true; break; } else if (pattern[0] == '-' && pattern[1] != ']' && pattern[1]) { pattern++; if (pattern[0] == '\\') { pattern++; if (!pattern[0]) return MATCH_BAD_PATTERN; } uint32 last = utf8ToUnicode(&pattern); if (c >= first && c <= last) { matched = true; break; } } } if (invert) matched = !matched; if (matched) { while (pattern[0] != ']') { if (!pattern[0]) return MATCH_BAD_PATTERN; pattern++; } pattern++; break; } return NO_MATCH; } case '\\': if (!pattern[0]) return MATCH_BAD_PATTERN; // supposed to fall through default: if (pattern[-1] != string[0]) return NO_MATCH; string++; break; } } if (string[0]) return NO_MATCH; return MATCH_OK; }
/* ** Create the mutex and shared memory used for locking in the file ** descriptor pFile */ static BOOL winceCreateLock(const char *zFilename, winFile *pFile){ WCHAR *zTok; WCHAR *zName = utf8ToUnicode(zFilename); BOOL bInit = TRUE; /* Initialize the local lockdata */ ZeroMemory(&pFile->local, sizeof(pFile->local)); /* Replace the backslashes from the filename and lowercase it ** to derive a mutex name. */ zTok = CharLowerW(zName); for (;*zTok;zTok++){ if (*zTok == '\\') *zTok = '_'; } /* Create/open the named mutex */ pFile->hMutex = CreateMutexW(NULL, FALSE, zName); if (!pFile->hMutex){ sqliteFree(zName); return FALSE; } /* Acquire the mutex before continuing */ winceMutexAcquire(pFile->hMutex); /* Since the names of named mutexes, semaphores, file mappings etc are ** case-sensitive, take advantage of that by uppercasing the mutex name ** and using that as the shared filemapping name. */ CharUpperW(zName); pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(winceLock), zName); /* Set a flag that indicates we're the first to create the memory so it ** must be zero-initialized */ if (GetLastError() == ERROR_ALREADY_EXISTS){ bInit = FALSE; } sqliteFree(zName); /* If we succeeded in making the shared memory handle, map it. */ if (pFile->hShared){ pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); /* If mapping failed, close the shared memory handle and erase it */ if (!pFile->shared){ CloseHandle(pFile->hShared); pFile->hShared = NULL; } } /* If shared memory could not be created, then close the mutex and fail */ if (pFile->hShared == NULL){ winceMutexRelease(pFile->hMutex); CloseHandle(pFile->hMutex); pFile->hMutex = NULL; return FALSE; } /* Initialize the shared memory if we're supposed to */ if (bInit) { ZeroMemory(pFile->shared, sizeof(winceLock)); } winceMutexRelease(pFile->hMutex); return TRUE; }
void FontLoader::loadFont(const std::string &fileName, bool exportToFile) { Ogre::DataStreamPtr file = Ogre::ResourceGroupManager::getSingleton().openResource(fileName); float fontSize; int one; file->read(&fontSize, sizeof(fontSize)); file->read(&one, sizeof(int)); assert(one == 1); file->read(&one, sizeof(int)); assert(one == 1); char name_[284]; file->read(name_, sizeof(name_)); std::string name(name_); GlyphInfo data[256]; file->read(data, sizeof(data)); file->close(); // Create the font texture std::string bitmapFilename = "Fonts/" + std::string(name) + ".tex"; Ogre::DataStreamPtr bitmapFile = Ogre::ResourceGroupManager::getSingleton().openResource(bitmapFilename); int width, height; bitmapFile->read(&width, sizeof(int)); bitmapFile->read(&height, sizeof(int)); std::vector<Ogre::uchar> textureData; textureData.resize(width*height*4); bitmapFile->read(&textureData[0], width*height*4); bitmapFile->close(); std::string resourceName; if (name.size() >= 5 && Misc::StringUtils::ciEqual(name.substr(0, 5), "magic")) resourceName = "Magic Cards"; else if (name.size() >= 7 && Misc::StringUtils::ciEqual(name.substr(0, 7), "century")) resourceName = "Century Gothic"; else if (name.size() >= 7 && Misc::StringUtils::ciEqual(name.substr(0, 7), "daedric")) resourceName = "Daedric"; else return; // no point in loading it, since there is no way of using additional fonts std::string textureName = name; Ogre::Image image; image.loadDynamicImage(&textureData[0], width, height, Ogre::PF_BYTE_RGBA); Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().createManual(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, width, height, 0, Ogre::PF_BYTE_RGBA); texture->loadImage(image); if (exportToFile) image.save(resourceName + ".png"); // Register the font with MyGUI MyGUI::ResourceManualFont* font = static_cast<MyGUI::ResourceManualFont*>( MyGUI::FactoryManager::getInstance().createObject("Resource", "ResourceManualFont")); // We need to emulate loading from XML because the data members are private as of mygui 3.2.0 MyGUI::xml::Document xmlDocument; MyGUI::xml::ElementPtr root = xmlDocument.createRoot("ResourceManualFont"); root->addAttribute("name", resourceName); MyGUI::xml::ElementPtr defaultHeight = root->createChild("Property"); defaultHeight->addAttribute("key", "DefaultHeight"); defaultHeight->addAttribute("value", fontSize); MyGUI::xml::ElementPtr source = root->createChild("Property"); source->addAttribute("key", "Source"); source->addAttribute("value", std::string(textureName)); MyGUI::xml::ElementPtr codes = root->createChild("Codes"); for(int i = 0; i < 256; i++) { float x1 = data[i].top_left.x*width; float y1 = data[i].top_left.y*height; float w = data[i].top_right.x*width - x1; float h = data[i].bottom_left.y*height - y1; ToUTF8::Utf8Encoder encoder(mEncoding); unsigned long unicodeVal = utf8ToUnicode(getUtf8(i, encoder, mEncoding)); MyGUI::xml::ElementPtr code = codes->createChild("Code"); code->addAttribute("index", unicodeVal); code->addAttribute("coord", MyGUI::utility::toString(x1) + " " + MyGUI::utility::toString(y1) + " " + MyGUI::utility::toString(w) + " " + MyGUI::utility::toString(h)); code->addAttribute("advance", data[i].width); code->addAttribute("bearing", MyGUI::utility::toString(data[i].kerning) + " " + MyGUI::utility::toString((fontSize-data[i].ascent))); code->addAttribute("size", MyGUI::IntSize(data[i].width, data[i].height)); // More hacks! The french game uses several win1252 characters that are not included // in the cp437 encoding of the font. Fall back to similar available characters. if (mEncoding == ToUTF8::CP437) { std::multimap<int, int> additional; // <cp437, unicode> additional.insert(std::make_pair(39, 0x2019)); // apostrophe additional.insert(std::make_pair(45, 0x2013)); // dash additional.insert(std::make_pair(45, 0x2014)); // dash additional.insert(std::make_pair(34, 0x201D)); // right double quotation mark additional.insert(std::make_pair(34, 0x201C)); // left double quotation mark additional.insert(std::make_pair(44, 0x201A)); additional.insert(std::make_pair(44, 0x201E)); additional.insert(std::make_pair(43, 0x2020)); additional.insert(std::make_pair(94, 0x02C6)); additional.insert(std::make_pair(37, 0x2030)); additional.insert(std::make_pair(83, 0x0160)); additional.insert(std::make_pair(60, 0x2039)); additional.insert(std::make_pair(79, 0x0152)); additional.insert(std::make_pair(90, 0x017D)); additional.insert(std::make_pair(39, 0x2019)); additional.insert(std::make_pair(126, 0x02DC)); additional.insert(std::make_pair(84, 0x2122)); additional.insert(std::make_pair(83, 0x0161)); additional.insert(std::make_pair(62, 0x203A)); additional.insert(std::make_pair(111, 0x0153)); additional.insert(std::make_pair(122, 0x017E)); additional.insert(std::make_pair(89, 0x0178)); additional.insert(std::make_pair(156, 0x00A2)); additional.insert(std::make_pair(46, 0x2026)); for (std::multimap<int, int>::iterator it = additional.begin(); it != additional.end(); ++it) { if (it->first != i) continue; MyGUI::xml::ElementPtr code = codes->createChild("Code"); code->addAttribute("index", it->second); code->addAttribute("coord", MyGUI::utility::toString(x1) + " " + MyGUI::utility::toString(y1) + " " + MyGUI::utility::toString(w) + " " + MyGUI::utility::toString(h)); code->addAttribute("advance", data[i].width); code->addAttribute("bearing", MyGUI::utility::toString(data[i].kerning) + " " + MyGUI::utility::toString((fontSize-data[i].ascent))); code->addAttribute("size", MyGUI::IntSize(data[i].width, data[i].height)); } } // ASCII vertical bar, use this as text input cursor if (i == 124) { MyGUI::xml::ElementPtr cursorCode = codes->createChild("Code"); cursorCode->addAttribute("index", MyGUI::FontCodeType::Cursor); cursorCode->addAttribute("coord", MyGUI::utility::toString(x1) + " " + MyGUI::utility::toString(y1) + " " + MyGUI::utility::toString(w) + " " + MyGUI::utility::toString(h)); cursorCode->addAttribute("advance", data[i].width); cursorCode->addAttribute("bearing", MyGUI::utility::toString(data[i].kerning) + " " + MyGUI::utility::toString((fontSize-data[i].ascent))); cursorCode->addAttribute("size", MyGUI::IntSize(data[i].width, data[i].height)); } // Question mark, use for NotDefined marker (used for glyphs not existing in the font) if (i == 63) { MyGUI::xml::ElementPtr cursorCode = codes->createChild("Code"); cursorCode->addAttribute("index", MyGUI::FontCodeType::NotDefined); cursorCode->addAttribute("coord", MyGUI::utility::toString(x1) + " " + MyGUI::utility::toString(y1) + " " + MyGUI::utility::toString(w) + " " + MyGUI::utility::toString(h)); cursorCode->addAttribute("advance", data[i].width); cursorCode->addAttribute("bearing", MyGUI::utility::toString(data[i].kerning) + " " + MyGUI::utility::toString((fontSize-data[i].ascent))); cursorCode->addAttribute("size", MyGUI::IntSize(data[i].width, data[i].height)); } } // These are required as well, but the fonts don't provide them for (int i=0; i<2; ++i) { MyGUI::FontCodeType::Enum type; if(i == 0) type = MyGUI::FontCodeType::Selected; else if (i == 1) type = MyGUI::FontCodeType::SelectedBack; MyGUI::xml::ElementPtr cursorCode = codes->createChild("Code"); cursorCode->addAttribute("index", type); cursorCode->addAttribute("coord", "0 0 0 0"); cursorCode->addAttribute("advance", "0"); cursorCode->addAttribute("bearing", "0 0"); cursorCode->addAttribute("size", "0 0"); } if (exportToFile) { xmlDocument.createDeclaration(); xmlDocument.save(resourceName + ".xml"); } font->deserialization(root, MyGUI::Version(3,2,0)); MyGUI::ResourceManager::getInstance().removeByName(font->getResourceName()); MyGUI::ResourceManager::getInstance().addResource(font); }
void Path::copy(const std::string & source, const std::string & destination) { NTA_CHECK(!source.empty()) << "Can't copy from an empty source"; NTA_CHECK(!destination.empty()) << "Can't copy to an empty destination"; NTA_CHECK(source != destination) << "Source and destination must be different"; if (isDirectory(source)) { Directory::copyTree(source, destination); return; } // The target is always a filename. The input destination // Can be either a directory or a filename. If the destination // doesn't exist it is treated as a filename. std::string target(destination); if (Path::exists(destination) && isDirectory(destination)) target = Path::normalize(Path::join(destination, Path::getBasename(source))); bool success = true; #ifdef WIN32 // Must remove read-only or hidden files before copy // because they cannot be overwritten. For simplicity // I just always remove if it exists. if (Path::exists(target)) Path::remove(target); // This will quietly overwrite the destination file if it exists std::wstring wsource(utf8ToUnicode(source)); std::wstring wtarget(utf8ToUnicode(target)); BOOL res = ::CopyFile(/*(LPCTSTR)*/wsource.c_str(), /*(LPCTSTR)*/wtarget.c_str(), FALSE); success = res != FALSE; #else try { OFStream out(target.c_str()); out.exceptions(std::ofstream::failbit | std::ofstream::badbit); UInt64 size = Path::getFileSize(source); if(size) { IFStream in(source.c_str()); if(out.fail()) { std::cout << OS::getErrorMessage() << std::endl; } in.exceptions(std::ifstream::failbit | std::ifstream::badbit); out << in.rdbuf(); } } catch(std::exception &e) { std::cerr << "Path::copy('" << source << "', '" << target << "'): " << e.what() << std::endl; } catch (...) { success = false; } #endif if (!success) NTA_THROW << "Path::copy() - failed copying file " << source << " to " << destination << " os error: " << OS::getErrorMessage(); }
size_t KCharset::_to_uchar(const char* str, size_t len, charset::uchar32* uchr) { return g_encoding == encode_utf8 ? utf8ToUnicode(str, uchr) : this->to_uchar(str, (int)len, uchr); }