static void editor_save_file(editor c, const char *name, int enc) { textbox t = getdata(c); FILE *f; char buf[MAX_PATH+30], tname[MAX_PATH+1]; 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); reEnc2(name, tname, MAX_PATH+1, CE_UTF8, CE_NATIVE, 3); sname = tname; 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); } }
/* The following is a version of R_ExpandFileName that assumes s is in UTF-8 and returns the final result in that encoding as well. */ const char *R_ExpandFileNameUTF8(const char *s) { if (s[0] !='~' || (s[0] && isalpha(s[1]))) return s; else { char home[PATH_MAX]; reEnc2(R_ExpandFileName("~"), home, PATH_MAX, CE_NATIVE, CE_UTF8, 3); if (strlen(home) + strlen(s+1) < PATH_MAX) { strcpy(newFileName, home); strcat(newFileName, s+1); return newFileName; } else return s; } }
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 editorsaveas(editor c) { textbox t = getdata(c); EditorData p = getdata(t); wchar_t *wname; setuserfilterW(L"R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0"); wname = askfilesaveW(G_("Save script as"), ""); if (wname) { char name[4*MAX_PATH+1]; wcstoutf8(name, wname, MAX_PATH); /* now check if it has an extension */ char *q = strchr(name, '.'); if(!q) strncat(name, ".R", 4*MAX_PATH); editor_save_file(c, name, CE_UTF8); p->file = 1; reEnc2(name, p->filename, MAX_PATH+1, CE_UTF8, CE_NATIVE, 3); gsetmodified(t, 0); editor_set_title(c, p->filename); } show(c); }
static void editoropen(const char *default_name) { wchar_t *wname; char name[4*MAX_PATH], title[4*MAX_PATH]; int i; textbox t; EditorData p; setuserfilterW(L"R files (*.R)\0*.R\0S files (*.q, *.ssc, *.S)\0*.q;*.ssc;*.S\0All files (*.*)\0*.*\0\0"); wname = askfilenameW(G_("Open script"), default_name); /* returns NULL if open dialog cancelled */ if (wname) { wcstoutf8(name, wname, MAX_PATH); /* check if file is already open in an editor. If so, close and open again */ for (i = 0; i < neditors; ++i) { t = getdata(REditors[i]); p = getdata(t); if (!strcmp (name, p->filename)) { editorclose(REditors[i]); break; } } reEnc2(name, title, MAX_PATH+1, CE_UTF8, CE_NATIVE, 3); Rgui_Edit(name, CE_UTF8, title, 0); } else show(RConsole); }