static void editor_save_file(editor c, const char *name, int enc) { textbox t = getdata(c); FILE *f; char buf[MAX_PATH+30]; const char *sname; if (name == NULL) return; else { if(enc == CE_UTF8) { wchar_t wname[MAX_PATH+1]; Rf_utf8towcs(wname, name, MAX_PATH+1); sname = reEnc(name, CE_UTF8, CE_NATIVE, 3); f = R_wfopen(wname, L"w"); } else { sname = name; f = R_fopen(sname, "w"); } if (f == NULL) { snprintf(buf, MAX_PATH+30, G_("Could not save file '%s'"), sname); askok(buf); return; } fprintf(f, "%s", gettext(t)); fclose(f); } }
/* To be fixed: during creation, memory is allocated two times (faster for small files but a big waste otherwise) */ static xbuf file2xbuf(const char *name, int enc, int del) { HANDLE f; DWORD rr, vv; char *p; xlong dim, cnt; xint ms; xbuf xb; wchar_t *wp, *q; if (enc == CE_UTF8) { wchar_t wfn[MAX_PATH+1]; Rf_utf8towcs(wfn, name, MAX_PATH+1); f = CreateFileW(wfn, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); } else f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (f == INVALID_HANDLE_VALUE) { R_ShowMessage(G_("Error opening file")); return NULL; } vv = GetFileSize(f, NULL); p = (char *) malloc((size_t) vv + 1); if (!p) { CloseHandle(f); R_ShowMessage(G_("Insufficient memory to display file in internal pager")); return NULL; } ReadFile(f, p, vv, &rr, NULL); CloseHandle(f); if (del) DeleteFile(name); p[rr] = '\0'; cnt = mbstowcs(NULL, p, 0); wp = (wchar_t *) malloc((cnt+1) * sizeof(wchar_t)); mbstowcs(wp, p, cnt+1); for (q = wp, ms = 1, dim = cnt; *q; q++) { if (*q == '\t') dim += TABSIZE; else if (*q == '\n') { dim++; ms++; } } free(p); if ((xb = newxbuf(dim + 1, ms + 1, 1))) for (q = wp, ms = 0; *q; q++) { if (*q == L'\r') continue; if (*q == L'\n') { ms++; xbufaddxc(xb, *q); /* next line interprets underlining in help files */ if (q[1] == L'_' && q[2] == L'\b') xb->user[ms] = -2; } else xbufaddxc(xb, *q); } free(wp); return xb; }
static void editor_load_file(editor c, const char *name, int enc) { textbox t = getdata(c); EditorData p = getdata(t); FILE *f; char *buffer = NULL, tmp[MAX_PATH+50], tname[MAX_PATH+1]; const char *sname; long num = 1, bufsize; if(enc == CE_UTF8) { wchar_t wname[MAX_PATH+1]; Rf_utf8towcs(wname, name, MAX_PATH+1); f = R_wfopen(wname, L"r"); reEnc2(name, tname, MAX_PATH+1, CE_UTF8, CE_NATIVE, 3); sname = tname; } else { f = R_fopen(name, "r"); sname = name; } if (f == NULL) { snprintf(tmp, MAX_PATH+50, G_("unable to open file %s for reading"), sname); R_ShowMessage(tmp); return; } p->file = 1; strncpy(p->filename, name, MAX_PATH+1); bufsize = 0; while (num > 0) { buffer = realloc(buffer, bufsize + 3000 + 1); num = fread(buffer + bufsize, 1, 3000 - 1, f); if (num >= 0) { bufsize += num; buffer[bufsize] = '\0'; } else { snprintf(tmp, MAX_PATH+50, G_("Could not read from file '%s'"), sname); askok(tmp); } } setlimittext(t, 2 * strlen(buffer)); settext(t, buffer); gsetmodified(t, 0); free(buffer); fclose(f); }
static void pcreate(const char* cmd, cetype_t enc, int newconsole, int visible, HANDLE hIN, HANDLE hOUT, HANDLE hERR, PROCESS_INFORMATION *pi) { DWORD ret; STARTUPINFO si; STARTUPINFOW wsi; HANDLE dupIN, dupOUT, dupERR; WORD showWindow = SW_SHOWDEFAULT; int inpipe; char *ecmd; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; /* FIXME: this might need to be done in wchar_t */ if (!(ecmd = expandcmd(cmd, 0))) return; /* error message already set */ inpipe = (hIN != INVALID_HANDLE_VALUE) || (hOUT != INVALID_HANDLE_VALUE) || (hERR != INVALID_HANDLE_VALUE); if (inpipe) { HANDLE hNULL = CreateFile("NUL:", GENERIC_READ | GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, NULL); HANDLE hTHIS = GetCurrentProcess(); if (hIN == INVALID_HANDLE_VALUE) hIN = hNULL; if (hOUT == INVALID_HANDLE_VALUE) hOUT = hNULL; if (hERR == INVALID_HANDLE_VALUE) hERR = hNULL; DuplicateHandle(hTHIS, hIN, hTHIS, &dupIN, 0, TRUE, DUPLICATE_SAME_ACCESS); DuplicateHandle(hTHIS, hOUT, hTHIS, &dupOUT, 0, TRUE, DUPLICATE_SAME_ACCESS); DuplicateHandle(hTHIS, hERR, hTHIS, &dupERR, 0, TRUE, DUPLICATE_SAME_ACCESS); CloseHandle(hTHIS); CloseHandle(hNULL); } switch (visible) { case -1: showWindow = SW_HIDE; break; case 0: showWindow = SW_SHOWMINIMIZED; break; } if(enc == CE_UTF8) { wsi.cb = sizeof(wsi); wsi.lpReserved = NULL; wsi.lpReserved2 = NULL; wsi.cbReserved2 = 0; wsi.lpDesktop = NULL; wsi.lpTitle = NULL; wsi.dwFlags = STARTF_USESHOWWINDOW; wsi.wShowWindow = showWindow; if (inpipe) { wsi.dwFlags |= STARTF_USESTDHANDLES; wsi.hStdInput = dupIN; wsi.hStdOutput = dupOUT; wsi.hStdError = dupERR; } } else { si.cb = sizeof(si); si.lpReserved = NULL; si.lpReserved2 = NULL; si.cbReserved2 = 0; si.lpDesktop = NULL; si.lpTitle = NULL; si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = showWindow; if (inpipe) { si.dwFlags |= STARTF_USESTDHANDLES; si.hStdInput = dupIN; si.hStdOutput = dupOUT; si.hStdError = dupERR; } } if(enc == CE_UTF8) { int n = strlen(ecmd); /* max no of chars */ wchar_t wcmd[n+1]; Rf_utf8towcs(wcmd, ecmd, n+1); ret = CreateProcessW(NULL, wcmd, &sa, &sa, TRUE, (newconsole && (visible == 1)) ? CREATE_NEW_CONSOLE : 0, NULL, NULL, &wsi, pi); } else ret = CreateProcess(NULL, ecmd, &sa, &sa, TRUE, (newconsole && (visible == 1)) ? CREATE_NEW_CONSOLE : 0, NULL, NULL, &si, pi); if (inpipe) { CloseHandle(dupIN); CloseHandle(dupOUT); CloseHandle(dupERR); } if (!ret) snprintf(RunError, 500, _("'CreateProcess' failed to run '%s'"), ecmd); else CloseHandle(pi->hThread); free(ecmd); return; }