void out3(FILE* fout3, int n, double* *keyvalue, double *tscore, int d, int **readin, int n_tuples,option_t *opt) { int total_cnt = 0; double avg3 = 0; int k = opt->k, k2 = opt->k; char *ans; ans = (char*)malloc(sizeof(char)*(2 * k + 1)*d); ans[0] = 0; int index; //max side for (int i = 0; i < n_tuples; i++) { if (!k) break; index = keyvalue[i] - &tscore[0]; for (int j = 0; j < d; j++) if (readin[index][j] == k) { k--; total_cnt++; break; }//find wbuf(ans, readin[index], d); avg3 += *keyvalue[i]; } //min side for (int i = n_tuples - 1; i >= 0; --i) { if (!k2) break; index = keyvalue[i] - &tscore[0]; for (int j = 0; j < d; j++) if (readin[index][j] == k) { k--; total_cnt++; break; }//find wbuf(ans, readin[index], d); avg3 += *keyvalue[i]; } avg3 /= total_cnt; char first_line[64]; sprintf(first_line, "%d\t%f", k, avg3); strcat(first_line, ans); fwrite(first_line, sizeof(char), strlen(first_line), fout3); }
/* read a wstring from the UnMarshal object */ void UnMarshal::Read(wstring& ws) { MarshalDataType dt = readDataType(); // check for stream read error CHECK_READ_ERROR(m_strm); if (dt != MTYPE_WSTRING) { throw SCXMarshalFormatException(MTYPE_WSTRING, dt, SCXSRCLOCATION); } int strSize = readInteger(); CHECK_READ_ERROR(m_strm); // Read in the wstring as a list of bytes vector<char> buf(strSize, '\0'); m_strm.read(&buf[0], strSize); CHECK_READ_ERROR(m_strm); // Create a real wstring of the appropriate size size_t nr = strSize / sizeof(wchar_t); vector<wchar_t> wbuf(nr + 1 /* Null byte */, L'\0'); memcpy(&wbuf[0], (void*) &buf[0], strSize); // Return the final wstring ws = wstring(&wbuf[0]); }
void FileTestCase::DoRoundTripTest(const wxMBConv& conv) { TestFile tf; const wxString data = "Hello\0UTF"; { wxFile fout(tf.GetName(), wxFile::write); CPPUNIT_ASSERT( fout.IsOpened() ); CPPUNIT_ASSERT( fout.Write(data, conv) ); } { wxFile fin(tf.GetName(), wxFile::read); CPPUNIT_ASSERT( fin.IsOpened() ); const ssize_t len = fin.Length(); wxCharBuffer buf(len); CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) ); wxWCharBuffer wbuf(conv.cMB2WC(buf)); #if wxUSE_UNICODE CPPUNIT_ASSERT_EQUAL( data, wbuf ); #else // !wxUSE_UNICODE CPPUNIT_ASSERT ( memcmp(wbuf, L"Hello\0UTF", data.length()*sizeof(wchar_t)) == 0 ); #endif // wxUSE_UNICODE/!wxUSE_UNICODE } }
BOOL TBrowseDirDlg::SetFileBuf(LPARAM list) { Wstr wbuf(MAX_PATH); BOOL ret = ::SHGetPathFromIDListW((LPITEMIDLIST)list, wbuf.Buf()); if (ret) { WtoU8(wbuf.s(), fileBuf, MAX_PATH_U8); } return ret; }
UINT TWin::GetDlgItemTextU8(int ctlId, char *buf, int len) { Wstr wbuf(len); *buf = 0; GetDlgItemTextW(ctlId, wbuf.Buf(), len); return WtoU8(wbuf.s(), buf, len); }
int TWin::GetWindowTextLengthU8(void) { int len = ::GetWindowTextLengthW(hWnd); Wstr wbuf(len + 1); if (::GetWindowTextW(hWnd, wbuf.Buf(), len + 1) <= 0) return 0; return WtoU8(wbuf.s(), NULL, 0); }
int TWin::GetWindowTextU8(char *text, int len) { Wstr wbuf(len); wbuf[0] = 0; if (::GetWindowTextW(hWnd, wbuf.Buf(), len) < 0) return -1; return WtoU8(wbuf.s(), text, len); }
UINT DragQueryFileU8(HDROP hDrop, UINT iFile, char *buf, UINT cb) { Wstr wbuf(cb); UINT ret = ::DragQueryFileW(hDrop, iFile, wbuf.Buf(), cb); if (ret > 0 && buf) { ret = WtoU8(wbuf.s(), buf, cb); } return ret; }
int main(int argc, char **argv) { callbackBuffer wbuf(0, write_callback_test); // ostream wstr(&wbuf); cerr = &wbuf; cerr << "Hello world!\n" << endl; wbuf.set_callback(0, 0); cerr << "Hello world!\n"; return 0; }
static wxFontEncoding ConvertToMB(wxString *strUtf, const wxMBConv& conv) { wxFontEncoding enc; CHECK( strUtf, wxFONTENCODING_SYSTEM, _T("NULL string in ConvertUTF8ToMB") ); if ( !strUtf->empty() ) { // try to determine which multibyte encoding is best suited for this // Unicode string wxWCharBuffer wbuf(strUtf->wc_str(conv)); if ( !wbuf ) { // invalid UTF-8 data, leave it as is enc = wxFONTENCODING_SYSTEM; } else // try to find a multibyte encoding we can show this in { enc = GuessUnicodeCharset(wbuf); // finally convert to multibyte wxString str; if ( enc == wxFONTENCODING_SYSTEM ) { str = wxString(wbuf); } else { wxCSConv conv(enc); str = wxString(wbuf, conv); } if ( str.empty() ) { // conversion failed - use original text (and display incorrectly, // unfortunately) wxLogDebug(_T("conversion from UTF-8 to default encoding failed")); enc = wxFONTENCODING_SYSTEM; } else { *strUtf = str; } } } else // doesn't really matter what we return from here { enc = wxFONTENCODING_SYSTEM; } return enc; }
static void handle_connection(acl::socket_stream& conn) { acl::string wbuf(__len + 1); char* buf = (char*) malloc(__len + 1); int i; for (i = 0; i < __len; i++) wbuf += 'X'; struct timeval begin; gettimeofday(&begin, NULL); for (i = 0; i < __max; i++) { if (conn.write(wbuf) == -1) { printf("write to server error\r\n"); break; } if (conn.read(buf, __len, true) == -1) { printf("readline from server error\r\n"); break; } if (i <= 1) { buf[__len] = 0; printf("buf: %s\r\n", buf); } if (i % 1000 == 0) { char tmp[64]; snprintf(tmp, sizeof(tmp), "total: %d, curr: %d, len: %d", __max, i, __len); ACL_METER_TIME(tmp); } } free(buf); struct timeval end; gettimeofday(&end, NULL); double n = util::stamp_sub(&end, &begin); printf("total get: %d, spent: %0.2f ms, speed: %0.2f\r\n", __max, n, (i * 1000) /(n > 0 ? n : 1)); }
DWORD GetFullPathNameU8(const char *path, DWORD size, char *buf, char **fname) { Wstr wpath(path), wbuf(size); WCHAR *wfname=NULL; DWORD ret = ::GetFullPathNameW(wpath, size, wbuf.Buf(), &wfname); if (ret == 0 || ret > size) return ret; int fname_len = wfname ? WtoU8(wfname, buf, size) : 0; int path_len = WtoU8(wbuf, buf, size); *fname = wfname ? (buf + path_len - fname_len) : NULL; return ret; }
static void test_it() { bpd::pipe p; bp::pistream rend(p.rend()); bpd::systembuf wbuf(p.wend().get()); std::ostream wend(&wbuf); // This assumes that the pipe's buffer is big enough to accept // the data written without blocking! wend << "1Test 1message" << std::endl; std::string tmp; rend >> tmp; BOOST_CHECK_EQUAL(tmp, "1Test"); rend >> tmp; BOOST_CHECK_EQUAL(tmp, "1message"); }
ase_string ase_getcwd() { DWORD wblen = GetCurrentDirectoryW(0, NULL); if (wblen == 0) { ase_throw_io_error("GetCurrentDirectoryW(len) failed"); } std::vector<WCHAR> wbuf(wblen); WCHAR *const wstr = &wbuf[0]; if (GetCurrentDirectoryW(wblen, wstr) == 0) { ase_throw_io_error("GetCurrentDirectoryW(buf) failed"); } DWORD wstr_len = wblen - 1; ase_string r(wstr, wstr_len); return r; }
void *run_test(void *arg) { RunContext *ctxt = (RunContext *)arg; Buffer wbuf(BUFFER_SIZE); while (ctxt->current_file < FILES_TO_WRITE) { std::string fname = get_current_file_name(ctxt); int fd; fd = open(fname.c_str(), O_WRONLY | O_CREAT, 0666); while (fd < 0 && errno == ENOSPC) { context_gc(ctxt); fd = open(fname.c_str(), O_WRONLY | O_CREAT, 0666); } if (fd < 0) { std::cout << "Failed to create file: " << errno << std::endl; return NULL; } wbuf.fillAt(ctxt->current_pos); char *buf = wbuf.get(); size_t count = BUFFER_SIZE; off_t pos = ctxt->current_pos; while (count > 0) { ssize_t ret = pwrite(fd, buf, count, pos); if (ret > 0) { buf += ret; pos += ret; count -= ret; } else if (ret == 0 || errno == ENOSPC) { context_gc(ctxt); } else { std::cout << "Write ERROR: " << errno << std::endl; return NULL; } } close(fd); ctxt->current_pos += BUFFER_SIZE; if (ctxt->current_pos >= FILE_SIZE) { ctxt->current_pos = 0; ctxt->current_file++; } } return NULL; }
static void _FFNet_Pattern_Activation_learn (FFNet me, Pattern pattern, Activation activation, long maxNumOfEpochs, double tolerance, Any parameters, int costFunctionType, int reset) { try { _FFNet_Pattern_Activation_checkDimensions (me, pattern, activation); Minimizer_setParameters (my minimizer, parameters); // Link the things to be learned my nPatterns = pattern -> ny; my inputPattern = pattern -> z; my targetActivation = activation -> z; FFNet_setCostFunction (me, costFunctionType); if (reset) { autoNUMvector<double> wbuf (1, my dimension); long k = 1; for (long i = 1; i <= my nWeights; i++) { if (my wSelected[i]) { wbuf[k++] = my w[i]; } } Minimizer_reset (my minimizer, wbuf.peek()); } Minimizer_minimize (my minimizer, maxNumOfEpochs, tolerance, 1); // Unlink my nPatterns = 0; my inputPattern = NULL; my targetActivation = NULL; } catch (MelderError) { my nPatterns = 0; my inputPattern = 0; my targetActivation = 0; } }
// convert a string in UTF-8 or 7 into the string in some multibyte encoding: // of course, this doesn't work in general as Unicode is not representable as // an 8 bit charset but it works in some common cases and is better than no // UTF-8 support at all // // FIXME this won't be needed when full Unicode support is available wxFontEncoding ConvertUTFToMB(wxString *strUtf, wxFontEncoding enc) { CHECK( strUtf, wxFONTENCODING_SYSTEM, _T("NULL string in ConvertUTFToMB") ); wxFontEncoding encConv; if ( !strUtf->empty() ) { if ( enc == wxFONTENCODING_UTF7 ) { #ifdef __WXGTK20__ // with GTK+ 2.0 we can convert everything to Unicode encConv = wxFONTENCODING_SYSTEM; wxWCharBuffer wbuf(strUtf->wc_str(wxConvUTF7)); if ( wbuf ) *strUtf = wxConvUTF8.cWC2MB(wbuf); #else encConv = ConvertToMB(strUtf, wxConvUTF7); #endif } else // !UTF-7 { ASSERT_MSG( enc == wxFONTENCODING_UTF8, _T("unknown Unicode encoding") ); // in GTK+ 2.0 we can use UTF-8 directly #ifdef __WXGTK20__ encConv = wxFONTENCODING_SYSTEM; #else return ConvertToMB(strUtf, wxConvUTF8); #endif } } else // doesn't really matter what we return from here { encConv = wxFONTENCODING_SYSTEM; } return encConv; }
BOOL Cfg::ReadIni(WCHAR *user_dir, WCHAR *virtual_dir) { if (!Init(user_dir, virtual_dir)) return FALSE; int i, j; char section[100], key[100], *p; DynBuf buf(MAX_HISTORY_CHAR_BUF); Wstr wbuf(MAX_HISTORY_CHAR_BUF); char *section_array[] = { SRC_HISTORY, DST_HISTORY, DEL_HISTORY, INC_HISTORY, EXC_HISTORY, FROMDATE_HISTORY, TODATE_HISTORY, MINSIZE_HISTORY, MAXSIZE_HISTORY }; bool is_filter_array[] = { false, false, false, true, true, false, false, false, false }; WCHAR ***history_array[] = { &srcPathHistory, &dstPathHistory, &delPathHistory, &includeHistory, &excludeHistory, &fromDateHistory, &toDateHistory, &minSizeHistory, &maxSizeHistory }; /* WCHAR wtestbuf[MAX_HISTORY_CHAR_BUF]; WCHAR *wtest[] = { L"12345", L"abc\\[abc\\]\\", L"aaa\\aaa", L"abc[\\ax]", L"abc[\\ax]\\", L"12[[]345", L"12[[]345\\", L"12\\a3\\[45\\", L"12345", NULL }; for (int i=0; wtest[i]; i++) GetFilterStrCore(wtest[i], wtestbuf); */ srcPathHistory = NULL; dstPathHistory = NULL; delPathHistory = NULL; includeHistory = NULL; excludeHistory = NULL; jobArray = NULL; jobMax = 0; finActArray = NULL; finActMax = 0; ini.SetSection(MAIN_SECTION); iniVersion = ini.GetInt(INI_VERSION_KEY, CUR_INI_VERSION); bufSize = ini.GetInt(BUFSIZE_KEY, DEFAULT_BUFSIZE); maxRunNum = ini.GetInt(MAXRUNNUM_KEY, DEFAULT_MAXRUNNUM); maxTransSize = ini.GetInt(MAXTRANSSIZE_KEY, DEFAULT_MAXTRANSSIZE); maxOvlNum = ini.GetInt(MAXOVLNUM_KEY, DEFAULT_MAXOVLNUM); maxOvlSize = ini.GetInt(MAXOVLSIZE_KEY, -1); if ((maxTransSize % maxOvlNum)) { maxTransSize = (maxTransSize + maxOvlNum - 1) / maxOvlNum * maxOvlNum; } if (bufSize < maxTransSize * BUFIO_SIZERATIO) { bufSize = maxTransSize * BUFIO_SIZERATIO; } maxOpenFiles = ini.GetInt(MAXOPENFILES_KEY, DEFAULT_MAXOPENFILES); maxAttrSize = ini.GetInt(MAXATTRSIZE_KEY, DEFAULT_MAXATTRSIZE); maxDirSize = ini.GetInt(MAXDIRSIZE_KEY, DEFAULT_MAXDIRSIZE); nbMinSizeNtfs = ini.GetInt(NONBUFMINSIZENTFS_KEY, DEFAULT_NBMINSIZE_NTFS); nbMinSizeFat = ini.GetInt(NONBUFMINSIZEFAT_KEY, DEFAULT_NBMINSIZE_FAT); timeDiffGrace = ini.GetInt(TIMEDIFFGRACE_KEY, 0); isReadOsBuf = ini.GetInt(ISREADOSBUF_KEY, FALSE); isWShareOpen = ini.GetInt(WRITESHAREOPEN_KEY, FALSE); maxHistoryNext = maxHistory = ini.GetInt(MAX_HISTORY_KEY, DEFAULT_MAX_HISTORY); copyMode = ini.GetInt(COPYMODE_KEY, DEFAULT_COPYMODE); copyFlags = ini.GetInt(COPYFLAGS_KEY, DEFAULT_COPYFLAGS); copyUnFlags = ini.GetInt(COPYUNFLAGS_KEY, DEFAULT_COPYUNFLAGS); skipEmptyDir = ini.GetInt(SKIPEMPTYDIR_KEY, DEFAULT_EMPTYDIR); forceStart = ini.GetInt(FORCESTART_KEY, DEFAULT_FORCESTART); ignoreErr = ini.GetInt(IGNORE_ERR_KEY, TRUE); estimateMode = ini.GetInt(ESTIMATE_KEY, 0); diskMode = ini.GetInt(DISKMODE_KEY, 0); netDrvMode = ini.GetInt(NETDRVMODE_KEY, 0); aclReset = ini.GetInt(ACLRESET_KEY, 0); isTopLevel = ini.GetInt(ISTOPLEVEL_KEY, FALSE); isErrLog = ini.GetInt(ISERRLOG_KEY, TRUE); isUtf8Log = ini.GetInt(ISUTF8LOG_KEY, TRUE); fileLogMode = ini.GetInt(FILELOGMODE_KEY, 0); fileLogFlags = ini.GetInt(FILELOGFLAGS_KEY, 0); aclErrLog = ini.GetInt(ACLERRLOG_KEY, FALSE); streamErrLog = ini.GetInt(STREAMERRLOG_KEY, FALSE); debugFlags = ini.GetInt(DEBUGFLAGS_KEY, 0); isRunasButton = ini.GetInt(ISRUNASBUTTON_KEY, FALSE); isSameDirRename = ini.GetInt(ISSAMEDIRRENAME_KEY, TRUE); shextAutoClose = ini.GetInt(SHEXTAUTOCLOSE_KEY, TRUE); shextTaskTray = ini.GetInt(SHEXTTASKTRAY_KEY, FALSE); shextNoConfirm = ini.GetInt(SHEXTNOCONFIRM_KEY, FALSE); shextNoConfirmDel = ini.GetInt(SHEXTNOCONFIRMDEL_KEY, FALSE); execConfirm = ini.GetInt(EXECCONRIM_KEY, FALSE); lcid = ini.GetInt(LCID_KEY, -1); waitTick = ini.GetInt(WAITTICK_KEY, DEFAULT_WAITTICK); isAutoSlowIo = ini.GetInt(ISAUTOSLOWIO_KEY, TRUE); speedLevel = ini.GetInt(SPEEDLEVEL_KEY, SPEED_FULL); alwaysLowIo = ini.GetInt(ALWAYSLOWIO_KEY, FALSE); enableOwdel = ini.GetInt(OWDEL_KEY, FALSE); enableAcl = ini.GetInt(ACL_KEY, FALSE); enableStream = ini.GetInt(STREAM_KEY, FALSE); enableVerify = ini.GetInt(VERIFY_KEY, FALSE); useOverlapIo = ini.GetInt(USEOVERLAPIO_KEY, TRUE); usingMD5 = ini.GetInt(USEMD5_KEY, TRUE); enableNSA = ini.GetInt(NSA_KEY, FALSE); delDirWithFilter= ini.GetInt(DELDIR_KEY, FALSE); enableMoveAttr = ini.GetInt(MOVEATTR_KEY, FALSE); serialMove = ini.GetInt(SERIALMOVE_KEY, TRUE); serialVerifyMove = ini.GetInt(SERIALVERIFYMOVE_KEY, TRUE); isReparse = ini.GetInt(REPARSE_KEY, TRUE); isLinkDest = ini.GetInt(LINKDEST_KEY, FALSE); maxLinkHash = ini.GetInt(MAXLINKHASH_KEY, DEFAULT_LINKHASH); allowContFsize = ini.GetInt(ALLOWCONTFSIZE_KEY, DEFAULT_ALLOWCONTFSIZE); isReCreate = ini.GetInt(RECREATE_KEY, FALSE); isExtendFilter = ini.GetInt(EXTENDFILTER_KEY, FALSE); taskbarMode = ini.GetInt(TASKBARMODE_KEY, 0); finishNotify = ini.GetInt(FINISHNOTIFY_KEY, 1); finishNotifyTout = ini.GetInt(FINISHNOTIFYTOUT_KEY, FINISH_NOTIFY_DEFAULT); infoSpan = ini.GetInt(INFOSPAN_KEY, DEFAULT_INFOSPAN); if (infoSpan < 0 || infoSpan > 2) infoSpan = DEFAULT_INFOSPAN; ini.GetStr(WINPOS_KEY, buf, MAX_PATH, ""); winpos.x = (p = strtok(buf, ", \t")) ? atoi(p) : INVALID_POINTVAL; winpos.y = p && (p = strtok(NULL, ", \t")) ? atoi(p) : INVALID_POINTVAL; winsize.cx = p && (p = strtok(NULL, ", \t")) ? atoi(p) : INVALID_SIZEVAL; winsize.cy = p && (p = strtok(NULL, ", \t")) ? atoi(p) : INVALID_SIZEVAL; ini.GetStr(DRIVEMAP_KEY, driveMap, sizeof(driveMap), ""); ini.GetStr(STATUSFONT_KEY, buf, MAX_HISTORY_CHAR_BUF, ""); IniStrToW(buf, statusFont); statusFontSize = ini.GetInt(STATUSFONTSIZE_KEY, 0); /* logfile */ ini.GetStr(LOGFILE_KEY, buf, MAX_PATH, DEFAULT_FASTCOPYLOG); IniStrToW(buf, wbuf.Buf()); if (wcschr(wbuf.s(), '\\') == NULL) { Wstr wname(wbuf); MakePathW(wbuf.Buf(), userDir, wname.s()); } errLogPath = wcsdup(wbuf.s()); /* History */ for (i=0; i < sizeof(section_array) / sizeof(char *); i++) { char *section_p = section_array[i]; bool &is_filter = is_filter_array[i]; WCHAR **&history = *history_array[i]; ini.SetSection(section_p); history = (WCHAR **)calloc(maxHistory, sizeof(WCHAR *)); for (j=0; j < maxHistory + 30; j++) { wsprintf(key, "%d", j); if (j < maxHistory) { if (is_filter) { GetFilterStr(key, buf, wbuf.Buf()); } else { ini.GetStr(key, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); } history[j] = wcsdup(wbuf.s()); } else if (!ini.DelKey(key)) break; } } /* Job */ for (i=0; i < JOB_MAX; i++) { Job job; wsprintf(section, FMT_JOB_KEY, i); ini.SetSection(section); if (ini.GetStr(TITLE_KEY, buf, MAX_HISTORY_CHAR_BUF) <= 0) break; IniStrToW(buf, wbuf.Buf()); job.title = wcsdup(wbuf.s()); ini.GetStr(SRC_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.src = wcsdup(wbuf.s()); ini.GetStr(DST_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.dst = wcsdup(wbuf.s()); ini.GetStr(CMD_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.cmd = wcsdup(wbuf.s()); GetFilterStr(INCLUDE_KEY, buf, wbuf.Buf()); job.includeFilter = wcsdup(wbuf.s()); GetFilterStr(EXCLUDE_KEY, buf, wbuf.Buf()); job.excludeFilter = wcsdup(wbuf.s()); ini.GetStr(FROMDATE_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.fromDateFilter = wcsdup(wbuf.s()); ini.GetStr(TODATE_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.toDateFilter = wcsdup(wbuf.s()); ini.GetStr(MINSIZE_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.minSizeFilter = wcsdup(wbuf.s()); ini.GetStr(MAXSIZE_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); job.maxSizeFilter = wcsdup(wbuf.s()); job.estimateMode = ini.GetInt(ESTIMATE_KEY, 0); job.diskMode = ini.GetInt(DISKMODE_KEY, 0); job.ignoreErr = ini.GetInt(IGNORE_ERR_KEY, TRUE); job.enableOwdel = ini.GetInt(OWDEL_KEY, FALSE); job.enableAcl = ini.GetInt(ACL_KEY, FALSE); job.enableStream = ini.GetInt(STREAM_KEY, FALSE); job.enableVerify = ini.GetInt(VERIFY_KEY, FALSE); job.isFilter = ini.GetInt(FILTER_KEY, FALSE); job.bufSize = ini.GetInt(BUFSIZE_KEY, DEFAULT_BUFSIZE); if (job.bufSize < maxTransSize * BUFIO_SIZERATIO) { job.bufSize = maxTransSize * BUFIO_SIZERATIO; } AddJobW(&job); } /* FinAct */ for (i=0; i < FINACT_MAX; i++) { FinAct act; wsprintf(buf, FMT_FINACT_KEY, i); ini.SetSection(buf); if (ini.GetStr(TITLE_KEY, buf, MAX_HISTORY_CHAR_BUF) <= 0) break; IniStrToW(buf, wbuf.Buf()); act.title = wcsdup(wbuf.Buf()); ini.GetStr(SOUND_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); act.sound = wcsdup(wbuf.s()); ini.GetStr(CMD_KEY, buf, MAX_HISTORY_CHAR_BUF); IniStrToW(buf, wbuf.Buf()); act.command = wcsdup(wbuf.s()); act.flags = ini.GetInt(FLAGS_KEY, 0); if (ini.GetStr(SHUTDOWNTIME_KEY, buf, MAX_HISTORY_CHAR_BUF) > 0) { act.shutdownTime = strtol(buf, 0, 10); } AddFinActW(&act); } if (::GetFileAttributesW(ini.GetIniFileNameW()) == 0xffffffff) { WriteIni(); } needIniConvert = FALSE; return TRUE; }
BOOL TInstDlg::Install(void) { char buf[MAX_PATH_U8], setupDir[MAX_PATH_U8], setupPath[MAX_PATH_U8]; char installPath[MAX_PATH_U8]; BOOL extract_only = IsDlgButtonChecked(EXTRACT_CHECK); // 現在、起動中の ipmsg を終了 int st = extract_only ? 0 : TerminateIPMsg(); if (st == 1) return FALSE; // インストールパス設定 GetDlgItemTextU8(FILE_EDIT, setupDir, sizeof(setupDir)); if (IsWinVista() && !::IsUserAnAdmin() && TIsEnableUAC() && TIsVirtualizedDirW(U8toWs(setupDir))) { if (MessageBox(GetLoadStr(IDS_REQUIREADMIN), INSTALL_STR, MB_OKCANCEL|MB_ICONINFORMATION) != IDOK) return FALSE; return RunAsAdmin(hWnd, TRUE); } CreateDirectoryU8(setupDir, NULL); DWORD attr = GetFileAttributesU8(setupDir); if (attr == 0xffffffff || (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) return MessageBox(GetLoadStr(IDS_NOTCREATEDIR), INSTALL_STR), FALSE; MakePath(setupPath, setupDir, IPMSG_EXENAME); if (st == 2) { MessageBox(GetLoadStr(IDS_CANTTERMINATE), INSTALL_STR); return FALSE; } if (!runasImm && MessageBox(GetLoadStr(IDS_START), INSTALL_STR, MB_OKCANCEL|MB_ICONINFORMATION) != IDOK) { return FALSE; } runasImm = FALSE; // ファイル生成 for (int i=0; SetupFiles[i]; i++) { MakePath(installPath, setupDir, SetupFiles[i]); CreateStatus cs = CreateFileBySelf(installPath, SetupFiles[i]); if (cs == CS_BROKEN) { MessageBox(GetLoadStr(IDS_BROKENARCHIVE), INSTALL_STR); return FALSE; } else if (cs == CS_ACCESS) { const char *msg = Fmt("%s\r\n%s", GetLoadStrU8(IDS_NOTCREATEFILE), installPath); MessageBoxU8(msg, INSTALL_STR); return FALSE; } } // 展開のみ if (extract_only) { ShellExecuteW(NULL, NULL, U8toWs(setupDir), 0, 0, SW_SHOW); return TRUE; } // スタートアップ&デスクトップに登録 TRegistry reg(HKEY_CURRENT_USER); if (reg.OpenKey(REGSTR_SHELLFOLDERS)) { char *regStr[] = { REGSTR_STARTUP, REGSTR_PROGRAMS, REGSTR_DESKTOP, NULL }; BOOL resId[] = { STARTUP_CHECK, PROGRAM_CHECK, DESKTOP_CHECK, NULL }; for (int i=0; regStr[i]; i++) { if (reg.GetStr(regStr[i], buf, sizeof(buf))) { if (i != 0 || !RemoveSameLink(buf, buf)) ::wsprintf(buf + strlen(buf), "\\%s", IPMSG_SHORTCUT_NAME); if (IsDlgButtonChecked(resId[i])) SymLink(setupPath, buf); else DeleteLink(buf); } } reg.CloseKey(); } // レジストリにアプリケーション情報を登録 reg.ChangeTopKey(HKEY_LOCAL_MACHINE); if (reg.OpenKey(REGSTR_PATH_APPPATHS)) { if (reg.CreateKey(IPMSG_EXENAME)) { reg.SetStr(NULL, setupPath); reg.SetStr(REGSTR_PATH, setupDir); reg.CloseKey(); } reg.CloseKey(); } // レジストリにアンインストール情報を登録 if (reg.OpenKey(REGSTR_PATH_UNINSTALL)) { if (reg.CreateKey(IPMSG_NAME)) { MakePath(buf, setupDir, SETUP_EXENAME); strcat(buf, " /r"); reg.SetStr(REGSTR_VAL_UNINSTALLER_DISPLAYNAME, IPMSG_FULLNAME); reg.SetStr(REGSTR_VAL_UNINSTALLER_COMMANDLINE, buf); reg.CloseKey(); } reg.CloseKey(); } // コピーしたアプリケーションを起動 const char *msg = GetLoadStr(IDS_SETUPCOMPLETE); int flg = MB_OKCANCEL|MB_ICONINFORMATION; // if (IsWinVista() && ::IsUserAnAdmin() && TIsEnableUAC()) { // msg = Fmt("%s%s", msg, GetLoadStr(IDS_COMPLETE_UACADD)); // flg |= MB_DEFBUTTON2; // } TLaunchDlg dlg(msg, this); if (dlg.Exec() == IDOK) { if (runasWnd) { Wstr wbuf(setupDir); if (::SendDlgItemMessageW(runasWnd, FILE_EDIT, WM_SETTEXT, 0, (LPARAM)wbuf.Buf())) { ::PostMessage(runasWnd, WM_IPMSG_QUIT, 1, 0); runasWnd = NULL; } } else { AppKick(); } } else { HWND hHelp = ShowHelpU8(0, setupDir, GetLoadStrU8(IDS_IPMSGHELP), "#history"); if (hHelp) { Show(SW_HIDE); while (::IsWindow(hHelp)) { this->Sleep(100); } } } if (runasWnd) { ::PostMessage(runasWnd, WM_IPMSG_QUIT, 0, 0); runasWnd = NULL; } // ShellExecuteU8(NULL, NULL, setupDir, 0, 0, SW_SHOW); ::PostQuitMessage(0); return TRUE; }
/* _UNICODE: read file (UTF-16 LE/BE, UTF-8, locale specific multibyte encoding) _MBCS: read file */ bool readFile(tstring *o_data, const tstringi &i_filename) { // get size of file #if 0 // bcc's _wstat cannot obtain file size struct _stat sbuf; if (_tstat(i_filename.c_str(), &sbuf) < 0 || sbuf.st_size == 0) return false; #else // so, we use _wstati64 for bcc struct stati64_t sbuf; if (_tstati64(i_filename.c_str(), &sbuf) < 0 || sbuf.st_size == 0) return false; // following check is needed to cast sbuf.st_size to size_t safely // this cast occurs because of above workaround for bcc if (sbuf.st_size > UINT_MAX) return false; #endif // open FILE *fp = _tfopen(i_filename.c_str(), _T("rb")); if (!fp) return false; // read file Array<BYTE> buf(static_cast<size_t>(sbuf.st_size) + 1); if (fread(buf.get(), static_cast<size_t>(sbuf.st_size), 1, fp) != 1) { fclose(fp); return false; } buf.get()[sbuf.st_size] = 0; // mbstowcs() requires null // terminated string #ifdef _UNICODE // if (buf.get()[0] == 0xffU && buf.get()[1] == 0xfeU && sbuf.st_size % 2 == 0) // UTF-16 Little Endien { size_t size = static_cast<size_t>(sbuf.st_size) / 2; o_data->resize(size); BYTE *p = buf.get(); for (size_t i = 0; i < size; ++ i) { wchar_t c = static_cast<wchar_t>(*p ++); c |= static_cast<wchar_t>(*p ++) << 8; (*o_data)[i] = c; } fclose(fp); return true; } // if (buf.get()[0] == 0xfeU && buf.get()[1] == 0xffU && sbuf.st_size % 2 == 0) // UTF-16 Big Endien { size_t size = static_cast<size_t>(sbuf.st_size) / 2; o_data->resize(size); BYTE *p = buf.get(); for (size_t i = 0; i < size; ++ i) { wchar_t c = static_cast<wchar_t>(*p ++) << 8; c |= static_cast<wchar_t>(*p ++); (*o_data)[i] = c; } fclose(fp); return true; } // try multibyte charset size_t wsize = mbstowcs(NULL, reinterpret_cast<char *>(buf.get()), 0); if (wsize != size_t(-1)) { Array<wchar_t> wbuf(wsize); mbstowcs(wbuf.get(), reinterpret_cast<char *>(buf.get()), wsize); o_data->assign(wbuf.get(), wbuf.get() + wsize); fclose(fp); return true; } // try UTF-8 { Array<wchar_t> wbuf(static_cast<size_t>(sbuf.st_size)); BYTE *f = buf.get(); BYTE *end = buf.get() + sbuf.st_size; wchar_t *d = wbuf.get(); enum { STATE_1, STATE_2of2, STATE_2of3, STATE_3of3 } state = STATE_1; while (f != end) { switch (state) { case STATE_1: if (!(*f & 0x80)) // 0xxxxxxx: 00-7F *d++ = static_cast<wchar_t>(*f++); else if ((*f & 0xe0) == 0xc0) { // 110xxxxx 10xxxxxx: 0080-07FF *d = ((static_cast<wchar_t>(*f++) & 0x1f) << 6); state = STATE_2of2; } else if ((*f & 0xf0) == 0xe0) // 1110xxxx 10xxxxxx 10xxxxxx: // 0800 - FFFF { *d = ((static_cast<wchar_t>(*f++) & 0x0f) << 12); state = STATE_2of3; } else goto not_UTF_8; break; case STATE_2of2: case STATE_3of3: if ((*f & 0xc0) != 0x80) goto not_UTF_8; *d++ |= (static_cast<wchar_t>(*f++) & 0x3f); state = STATE_1; break; case STATE_2of3: if ((*f & 0xc0) != 0x80) goto not_UTF_8; *d |= ((static_cast<wchar_t>(*f++) & 0x3f) << 6); state = STATE_3of3; break; } } o_data->assign(wbuf.get(), d); fclose(fp); return true; not_UTF_8: ; } #endif // _UNICODE // assume ascii o_data->resize(static_cast<size_t>(sbuf.st_size)); for (off_t i = 0; i < sbuf.st_size; ++ i) (*o_data)[i] = buf.get()[i]; fclose(fp); return true; }
BOOL TWin::SetDlgItemTextU8(int ctlId, const char *buf) { Wstr wbuf(buf); return ::SetDlgItemTextW(hWnd, ctlId, wbuf.s()); }
BOOL TWin::SetWindowTextU8(const char *text) { Wstr wbuf(text); return ::SetWindowTextW(hWnd, wbuf.s()); }
bool EnsureAvailableTextEncoding(wxFontEncoding *enc, wxString *text, bool mayAskUser) { CHECK( enc, false, _T("CheckEncodingAvailability: NULL encoding") ); if ( !wxFontMapper::Get()->IsEncodingAvailable(*enc) ) { // try to find another encoding wxFontEncoding encAlt; if ( wxFontMapper::Get()-> GetAltForEncoding(*enc, &encAlt, wxEmptyString, mayAskUser) ) { // translate the text (if any) to the equivalent encoding if ( text && !text->empty() ) { #if wxUSE_WCHAR_T // try converting via Unicode wxCSConv a2w(*enc); wxWCharBuffer wbuf(a2w.cMB2WC(text->c_str())); if ( *wbuf ) { wxString textConv; // special case of UTF-8 which is used all the time under wxGTK if ( encAlt == wxFONTENCODING_UTF8 ) { textConv = wxConvUTF8.cWC2MB(wbuf); } else // all the other encodings, use generic converter { wxCSConv w2a(encAlt); textConv = w2a.cWC2MB(wbuf); } if ( !textConv.empty() ) { *text = textConv; return true; } //else: fall back to wxEncodingConverter } //else: conversion to Unicode failed #endif // wxUSE_WCHAR_T wxEncodingConverter conv; if ( !conv.Init(*enc, encAlt) ) { // failed to convert the text return false; } *text = conv.Convert(*text); } //else: just return the encoding *enc = encAlt; } else // no equivalent encoding { return false; } } // we have either the requested encoding or an equivalent one return true; }