// returns TRUE on success BOOL CopyFile(const WCHAR* lpExistingFileName, const WCHAR* lpNewFileName, BOOL bFailIfExists) { std::string existingFileName; std::string newFileName; if (!convertToUtf8(lpExistingFileName, existingFileName)) return FALSE; if (!convertToUtf8(lpNewFileName, newFileName)) return FALSE; return CopyFile(existingFileName.c_str(), newFileName.c_str(), bFailIfExists); }
// returns TRUE on success BOOL MoveFile(const WCHAR* lpExistingFileName, const WCHAR* lpNewFileName) { std::string existingFileName; std::string newFileName; if (!convertToUtf8(lpExistingFileName, existingFileName)) return FALSE; if (!convertToUtf8(lpNewFileName, newFileName)) return FALSE; return MoveFile(existingFileName.c_str(), newFileName.c_str()); }
// returns TRUE if the function succeeds BOOL DeleteFile(const WCHAR* lpFileName) { std::string fileName; if (!convertToUtf8(lpFileName, fileName)) return FALSE; return DeleteFile(fileName.c_str()); }
// return TRUE if the function succeeds BOOL CreateDirectory(const WCHAR* lpPathName, SECURITY_ATTRIBUTES* secAttrib) { std::string pathName; if (!convertToUtf8(lpPathName, pathName)) return FALSE; return CreateDirectory(pathName.c_str(), secAttrib); }
BOOL SetFileAttributes(const WCHAR* lpFileName, DWORD dwFileAttributes) { std::string fileName; if (!convertToUtf8(lpFileName, fileName)) return FALSE; return SetFileAttributes(fileName.c_str(), dwFileAttributes); }
DWORD GetFileAttributes(const WCHAR* lpFileName) { std::string fileName; if (!convertToUtf8(lpFileName, fileName)) return INVALID_FILE_ATTRIBUTES; DWORD rv = GetFileAttributes(fileName.c_str()); return rv; }
void EclObjectParameter::ensureUtf8Content() { //If looks like utf16 then convert it to utf8 const void * zero = memchr(mb.bufferBase(), 0, mb.length()); if (zero) { MemoryBuffer translated; if (convertToUtf8(translated, mb.length(), mb.bufferBase())) mb.swapWith(translated); else throw MakeStringException(1, "%s content doesn't appear to be UTF8", value.get()); } }
XeTeXFontMgr::NameCollection* XeTeXFontMgr_FC::readNames(FcPattern* pat) { NameCollection* names = new NameCollection; char* pathname; if (FcPatternGetString(pat, FC_FILE, 0, (FcChar8**)&pathname) != FcResultMatch) return names; int index; if (FcPatternGetInteger(pat, FC_INDEX, 0, &index) != FcResultMatch) return names; FT_Face face; if (FT_New_Face(gFreeTypeLibrary, pathname, index, &face) != 0) return names; const char* name = FT_Get_Postscript_Name(face); if (name == NULL) return names; names->psName = name; // for sfnt containers, we'll read the name table ourselves, not rely on Fontconfig if (FT_IS_SFNT(face)) { std::list<std::string> familyNames; std::list<std::string> subFamilyNames; FT_SfntName nameRec; for (index = 0; index < FT_Get_Sfnt_Name_Count(face); ++index) { char* utf8name = NULL; if (FT_Get_Sfnt_Name(face, index, &nameRec) != 0) continue; switch (nameRec.name_id) { case kFontFullName: case kFontFamilyName: case kFontStyleName: case kPreferredFamilyName: case kPreferredSubfamilyName: { bool preferredName = false; if (nameRec.platform_id == TT_PLATFORM_MACINTOSH && nameRec.encoding_id == TT_MAC_ID_ROMAN && nameRec.language_id == 0) { utf8name = convertToUtf8(macRomanConv, nameRec.string, nameRec.string_len); preferredName = true; } else if ((nameRec.platform_id == TT_PLATFORM_APPLE_UNICODE) || (nameRec.platform_id == TT_PLATFORM_MICROSOFT)) utf8name = convertToUtf8(utf16beConv, nameRec.string, nameRec.string_len); if (utf8name != NULL) { std::list<std::string>* nameList = NULL; switch (nameRec.name_id) { case kFontFullName: nameList = &names->fullNames; break; case kFontFamilyName: nameList = &names->familyNames; break; case kFontStyleName: nameList = &names->styleNames; break; case kPreferredFamilyName: nameList = &familyNames; break; case kPreferredSubfamilyName: nameList = &subFamilyNames; break; } if (preferredName) prependToList(nameList, utf8name); else appendToList(nameList, utf8name); } } break; } } if (familyNames.size() > 0) names->familyNames = familyNames; if (subFamilyNames.size() > 0) names->styleNames = subFamilyNames; } else { index = 0; while (FcPatternGetString(pat, FC_FULLNAME, index++, (FcChar8**)&name) == FcResultMatch) appendToList(&names->fullNames, name); index = 0; while (FcPatternGetString(pat, FC_FAMILY, index++, (FcChar8**)&name) == FcResultMatch) appendToList(&names->familyNames, name); index = 0; while (FcPatternGetString(pat, FC_STYLE, index++, (FcChar8**)&name) == FcResultMatch) appendToList(&names->styleNames, name); if (names->fullNames.size() == 0) { std::string fullName(names->familyNames.front()); if (names->styleNames.size() > 0) { fullName += " "; fullName += names->styleNames.front(); } names->fullNames.push_back(fullName); } } FT_Done_Face(face); return names; }
bgl_entry Babylon::readEntry() { bgl_entry entry; if( file == NULL ) { entry.headword = ""; return entry; } bgl_block block; unsigned int len, pos; std::string headword; std::string definition; std::string temp; std::vector<std::string> alternates; std::string alternate; while( readBlock( block ) ) { switch( block.type ) { case 2: { pos = 0; len = (unsigned char)block.data[pos++]; std::string filename(block.data+pos, len); if (filename != "8EAF66FD.bmp" && filename != "C2EEF3F6.html") { filename = "res/" + filename; pos += len; FILE *ifile = fopen(filename.c_str(), "w"); fwrite(block.data + pos, 1, block.length -pos, ifile); fclose(ifile); } break; } case 1: case 10: alternate.clear(); headword.clear(); definition.clear(); temp.clear(); pos = 0; // Headword len = 0; len = (unsigned char)block.data[pos++]; headword.reserve( len ); for(unsigned int a=0;a<len;a++) headword += block.data[pos++]; convertToUtf8( headword, SOURCE_CHARSET ); // Definition len = 0; len = (unsigned char)block.data[pos++] << 8; len |= (unsigned char)block.data[pos++]; definition.reserve( len ); for(unsigned int a=0;a<len;a++) { if( (unsigned char)block.data[pos] == 0x0a ) { definition += "<br>"; pos++; }else if( (unsigned char)block.data[pos] < 0x20 ) { if( a <= len - 3 && block.data[pos] == 0x14 && block.data[pos+1] == 0x02 ) { int index = (unsigned char)block.data[pos+2] - 0x30; if (index <= 10) { definition = "<font color=\"blue\">" + partOfSpeech[index] + "</font> " + definition; } pos += len - a; break; } else if (block.data[pos] == 0x14) { pos++; } else { definition += block.data[pos++]; } }else definition += block.data[pos++]; } convertToUtf8( definition, TARGET_CHARSET ); // Alternate forms while( pos != block.length ) { len = (unsigned char)block.data[pos++]; alternate.reserve( len ); for(unsigned int a=0;a<len;a++) alternate += block.data[pos++]; convertToUtf8( alternate, SOURCE_CHARSET ); alternates.push_back( alternate ); alternate.clear(); } entry.headword = headword; entry.definition = definition; entry.alternates = alternates; return entry; break; default: ; } if( block.length ) free( block.data ); } entry.headword = ""; return entry; }
bool Babylon::read(std::string &source_charset, std::string &target_charset) { if( file == NULL ) return false; bgl_block block; unsigned int pos; unsigned int type; std::string headword; std::string definition; m_sourceCharset = source_charset; m_targetCharset = target_charset; m_numEntries = 0; while( readBlock( block ) ) { headword.clear(); definition.clear(); switch( block.type ) { case 0: switch( block.data[0] ) { case 8: type = (unsigned int)block.data[2]; if( type > 64 ) type -= 65; m_defaultCharset = bgl_charset[type]; break; default: break; } break; case 1: case 10: // Only count entries m_numEntries++; break; case 3: pos = 2; switch( block.data[1] ) { case 1: headword.reserve( block.length - 2 ); for(unsigned int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_title = headword; break; case 2: headword.reserve( block.length - 2 ); for(unsigned int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_author = headword; break; case 3: headword.reserve( block.length - 2 ); for(unsigned int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_email = headword; break; case 4: headword.reserve( block.length - 2 ); for(unsigned int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_copyright = headword; break; case 7: headword = bgl_language[(unsigned char)(block.data[5])]; m_sourceLang = headword; break; case 8: headword = bgl_language[(unsigned char)(block.data[5])]; m_targetLang = headword; break; case 9: headword.reserve( block.length - 2 ); for(unsigned int a=0;a<block.length-2;a++) { if (block.data[pos] == '\r') { } else if (block.data[pos] == '\n') { headword += "<br>"; } else { headword += block.data[pos]; } pos++; } m_description = headword; break; case 26: type = (unsigned int)block.data[2]; if( type > 64 ) type -= 65; if (m_sourceCharset.empty()) m_sourceCharset = bgl_charset[type]; break; case 27: type = (unsigned int)block.data[2]; if( type > 64 ) type -= 65; if (m_targetCharset.empty()) m_targetCharset = bgl_charset[type]; break; default: break; } break; default: ; } if( block.length ) free( block.data ); } gzseek( file, 0, SEEK_SET ); convertToUtf8( m_title, TARGET_CHARSET ); convertToUtf8( m_author, TARGET_CHARSET ); convertToUtf8( m_email, TARGET_CHARSET ); convertToUtf8( m_copyright, TARGET_CHARSET ); convertToUtf8( m_description, TARGET_CHARSET ); printf("Default charset: %s\nSource Charset: %s\nTargetCharset: %s\n", m_defaultCharset.c_str(), m_sourceCharset.c_str(), m_targetCharset.c_str()); return true; }
bgl_entry Babylon::readEntry() { bgl_entry entry; if( file == NULL ) { entry.headword = ""; return entry; } bgl_block block; int len, pos; std::string headword; std::string definition; std::string temp; std::vector<std::string> alternates; std::string alternate; while( readBlock( block ) ) { switch( block.type ) { case 1: case 10: alternate.clear(); headword.clear(); definition.clear(); temp.clear(); pos = 0; // Headword len = 0; len = (unsigned char)block.data[pos++]; headword.reserve( len ); for(int a=0;a<len;a++) headword += block.data[pos++]; convertToUtf8( headword, SOURCE_CHARSET ); // Definition len = 0; len = (unsigned char)block.data[pos++] << 8; len |= (unsigned char)block.data[pos++]; definition.reserve( len ); for(int a=0;a<len;a++) { if( (unsigned char)block.data[pos] == 0x0a ) { definition += '\n'; pos++; }else if( (unsigned char)block.data[pos] < 0x20 ) { if( a < len - 3 && block.data[pos] == 0x14 && block.data[pos+1] == 0x02 ) definition = partOfSpeech[(unsigned char)block.data[pos+2] - 0x30] + " " + definition; pos += len - a; break; }else definition += block.data[pos++]; } convertToUtf8( definition, TARGET_CHARSET ); // Alternate forms while( pos != block.length ) { len = (unsigned char)block.data[pos++]; alternate.reserve( len ); for(int a=0;a<len;a++) alternate += block.data[pos++]; convertToUtf8( alternate, SOURCE_CHARSET ); alternates.push_back( alternate ); alternate.clear(); } entry.headword = headword; entry.definition = definition; entry.alternates = alternates; return entry; break; default: ; } if( block.length ) free( block.data ); } entry.headword = ""; return entry; }
bool Babylon::read() { if( file == NULL ) return false; bgl_block block; int pos; int type; std::string headword; std::string definition; m_numEntries = 0; while( readBlock( block ) ) { headword.clear(); definition.clear(); switch( block.type ) { case 0: switch( block.data[0] ) { case 8: type = (int)block.data[2]; if( type > 64 ) type -= 65; m_defaultCharset = bgl_charset[type]; break; default: break; } break; case 1: case 10: // Only count entries m_numEntries++; break; case 3: pos = 2; switch( block.data[1] ) { case 1: headword.reserve( block.length - 2 ); for(int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_title = headword; break; case 2: headword.reserve( block.length - 2 ); for(int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_author = headword; break; case 3: headword.reserve( block.length - 2 ); for(int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_email = headword; break; case 4: headword.reserve( block.length - 2 ); for(int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_copyright = headword; break; case 7: headword = bgl_language[block.data[5]]; m_sourceLang = headword; break; case 8: headword = bgl_language[block.data[5]]; m_targetLang = headword; break; case 9: headword.reserve( block.length - 2 ); for(int a=0;a<block.length-2;a++) headword += block.data[pos++]; m_description = headword; break; case 26: type = (int)block.data[2]; if( type > 64 ) type -= 65; m_sourceCharset = bgl_charset[type]; break; case 27: type = (int)block.data[2]; if( type > 64 ) type -= 65; m_targetCharset = bgl_charset[type]; break; default: break; } break; default: ; } if( block.length ) free( block.data ); } gzseek( file, 0, SEEK_SET ); convertToUtf8( m_title, DEFAULT_CHARSET ); convertToUtf8( m_author, DEFAULT_CHARSET ); convertToUtf8( m_email, DEFAULT_CHARSET ); convertToUtf8( m_copyright, DEFAULT_CHARSET ); convertToUtf8( m_description, DEFAULT_CHARSET ); return true; //return m_numEntries; }