bool SciDoc::SaveToFile(const char*filename, bool as_itself) { #ifdef WIN32 FXFile fh(filename, FXIO::WriteOnly|FXIO::Truncate|((FXStat::exists(filename))?0:FXIO::Create)); #else FXFile fh(filename, FXIO::Writing); #endif _lasterror=""; if (fh.isOpen()) { FXbool en=isEnabled(); FXbool hf=hasFocus(); FXTextCodec *codec=NULL; FXString recode=FXString::null; if (en) { disable(); } FXint len=sendMessage(SCI_GETLENGTH,0,0); const char*buf=(const char*)sendMessage(SCI_GETCHARACTERPOINTER,0,0); if (bom[0]) { fh.writeBlock(bom,strlen(bom)); switch ((FXuchar)bom[0]) { case 0xFF: { codec=new FXUTF16LECodec(); break; } case 0xFE: { codec=new FXUTF16BECodec(); break; } } } if (codec) { FXint old_len=len; len=codec->utf2mblen(buf,old_len); recode.length(len); codec->utf2mb((char*)recode.text(),len,buf,old_len); delete codec; buf=recode.text(); } FXival wrote=fh.writeBlock(buf,len); if (en) { enable(); } if (hf) { setFocus(); } if (fh.close() && (wrote==len)) { if (as_itself) { if (_filename.empty() && !getLanguage()) { if (!SetLanguageForHeader(filename)) { if (!setLanguageFromFileName(FXPath::name(filename).text())) { setLanguageFromContent(); } } } _filename=FXPath::absolute(filename); _filetime=FXStat::modified(_filename); sendMessage(SCI_SETSAVEPOINT,0,0); DoStaleTest(true); } return true; } } _lasterror=SystemErrorStr(); return false; }
bool BackupMgr::MakePath(const FXString& path) { lasterror=""; FXString parts=FXPath::absolute(path); FXString dirs=FXPath::root(parts); parts.append(PATHSEP); FXint nseps=parts.contains(PATHSEP); FXint i; for (i=1;i<nseps;i++) { dirs.append(parts.section(PATHSEP,i)); if (!(IsDir(dirs)||FXDir::create(dirs,FXIO::OwnerFull))) { lasterror=SystemErrorStr(); ErrorMessage(_("Failed to create backup directory"), dirs); return false; } dirs.append(PATHSEP); } return true; }
bool SciDoc::DoLoadFromFile(const char*filename,bool insert) { _lasterror=""; errno=0; memset(bom,0,sizeof(bom)); bool rv=true; bool ro=GetReadOnly(); FXTextCodec *codec=NULL; if (ro&&insert) { _lasterror=_("Document is marked read-only."); return false; } if (FXStat::isDirectory(filename)) { _lasterror=_("is a directory"); return false; } bool DefaultToAscii=SettingsBase::instance()->DefaultToAscii; bool DefaultToSbcs=SettingsBase::instance()->DefaultToSbcs; switch (get_file_encoding(filename)) { case 'B': { // Binary file if ( !ConfirmOpenBinary(this,filename) ) { _lasterror=BinaryFileMessage(); return false; } if (!insert) { SetUTF8(!DefaultToSbcs); } break; } case 'T': { // Plain US-ASCII text file if (!insert) { SetUTF8(!DefaultToAscii); } break; } case 'H': { // High (extended ASCII) text file. if (!insert) { SetUTF8(!DefaultToSbcs); } break; } case 'U': { // UTF-8 encoded text file w/o BOM. if (!insert) { SetUTF8(true); } break; } case 'M': { // UTF-8 BOM. if (!insert) { strcpy(bom,"\0357\0273\0277"); SetUTF8(true); } break; } case 'Z': { // Zero-length (empty) file. if (!insert) { SetUTF8(!DefaultToAscii); } break; } case 'e': { // UTF-16LE BOM. if (!insert) { codec=new FXUTF16LECodec(); strcpy(bom,"\377\376"); SetUTF8(false); } break; } case 'E': { // UTF-16BE BOM. if (!insert) { codec=new FXUTF16BECodec(); strcpy(bom,"\376\377"); SetUTF8(false); } break; } case 'F': { // Failure, could not read the file. _lasterror=SystemErrorStr(); return false; break; } } FXFile fh(filename, FXFile::Reading); if (fh.isOpen()) { if (ro) { // This might happen e.g. if we are updating a document that has been modified externally sendMessage(SCI_SETREADONLY,0,0); } static const int BUFSIZE=1025; char buf[BUFSIZE]; fh.position(strlen(bom),FXIO::Begin); long p=0; _loading=!insert; if (insert) { p=sendMessage(SCI_GETCURRENTPOS, 0,0); sendMessage(SCI_BEGINUNDOACTION, 0, 0); } else { sendMessage(SCI_CLEARALL,0,0); } do { memset(buf,0,BUFSIZE); FXival n=fh.readBlock(buf,BUFSIZE-1); if (n<0) { _lasterror=SystemErrorStr(); rv=false; break; } buf[n]='\0'; if (insert) { _dirty=true; if (GetSelLength()>0) { sendString(SCI_REPLACESEL,0,buf); p=sendMessage(SCI_GETCURRENTPOS, 0,0); } else { sendString(SCI_INSERTTEXT,p,buf); p+=n; } } else { sendString(SCI_APPENDTEXT,n,buf); } } while (!fh.eof()); fh.close(); if (rv) { if (insert) { GoToPos(p); sendMessage(SCI_CONVERTEOLS,sendMessage(SCI_GETEOLMODE,0,0),0); sendMessage(SCI_ENDUNDOACTION,0,0); } else { _filename=FXPath::absolute(filename); _filetime=FXStat::modified(_filename); _dirty=false; need_backup=false; if (codec) { const char *orig=(const char*)sendMessage(SCI_GETCHARACTERPOINTER,0,0); FXString recode; recode.length(codec->mb2utflen(orig,sendMessage(SCI_GETLENGTH,0,0))); codec->mb2utf((char*)recode.text(),recode.length(),orig,sendMessage(SCI_GETLENGTH,0,0)); delete codec; SetUTF8(true); sendString(SCI_SETTEXT,0,recode.text()); } SetEolModeFromContent(); sendMessage(SCI_EMPTYUNDOBUFFER,0,0); } AdjustHScroll(); } if (ro) { sendMessage(SCI_SETREADONLY,1,0); } } else { _lasterror=SystemErrorStr(); rv=false; } _loading=false; return rv; }
static void AppendError(FXString &errors, const char*err, const FXString &filename) { FXString tmp; tmp.format("\n%s (%s): %s", err, SystemErrorStr(), filename.text()); errors+=tmp; }
Settings::~Settings() { LangStyle*ls; for (ls=languages; ls->name; ls++) { int i; SaveStyles(style_reg, ls->name, ls->styles); style_reg->writeStringEntry(ls->name,"FileMask",ls->mask?ls->mask:""); style_reg->writeStringEntry(ls->name,"ShebangApps",ls->apps?ls->apps:""); style_reg->writeIntEntry(ls->name,"TabPolicy", ls->tabs); style_reg->writeIntEntry(ls->name,"TabWidth", ls->tabwidth); for (i=0; ls->words[i]; i++) { char key[32]; snprintf(key, sizeof(key)-1, "words_%d", i+1); #ifdef FOX_1_6 // fox 1.6 will choke reading string entries > 2000 chars char buf[1952]; memset(buf,0,sizeof(buf)); strncpy(buf, ls->words[i], sizeof(buf)-1); style_reg->writeStringEntry(ls->name,key,buf); #else style_reg->writeStringEntry(ls->name,key,ls->words[i]); #endif } } SaveStyles(style_reg, global_sect, GlobalStyle); SaveStyle(style_reg, global_sect,&WhiteSpaceStyle); SaveStyle(style_reg, global_sect,&CaretLineStyle); SaveStyle(style_reg, global_sect,&RightMarginStyle); reg->deleteSection(keys_sect); reg->deleteSection(tbar_sect); WriteInt(ShowStatusBar); WriteInt(ShowWhiteSpace); WriteInt(ShowOutputPane); bool InvertColors=ColorFuncs::ColorsInverted(); WriteInt(InvertColors); WriteInt(SplitView); WriteInt(OutputPaneHeight); WriteInt(ShowLineNumbers); WriteInt(ShowToolbar); WriteInt(SmartHome); WriteInt(WrapAwareHomeEnd); WriteInt(AutoIndent); WriteInt(BraceMatch); WriteInt(UseTabs); WriteInt(CaretPastEOL); WriteInt(TabWidth); WriteInt(IndentWidth); WriteInt(CaretWidth); WriteInt(RightEdgeColumn); WriteInt(ShowRightEdge); WriteInt(ShowIndentGuides); WriteInt(ShowCaretLine); WriteInt(SmoothScroll); WriteInt(WheelLines); WriteInt(SearchWrap); WriteInt(SearchGui); WriteInt(SearchVerbose); WriteInt(SearchOptions); WriteInt(ZoomFactor); WriteInt(MaxFiles); WriteInt(PromptCloseMultiMenu); WriteInt(PromptCloseMultiExit); WriteInt(WatchExternChanges); WriteInt(Autosave); WriteInt(AutosaveInterval); WriteInt(SaveBeforeFilterSel); WriteInt(SaveBeforeInsCmd); WriteInt(SaveBeforeExecCmd); WriteInt(WhitespaceShowsEOL); WriteInt(DefaultFileFormat); WriteInt(WrapToolbar); WriteInt(WordWrap); WriteInt(ToolbarButtonSize); WriteInt(Left); WriteInt(Top); WriteInt(Width); WriteInt(Height); WriteInt(Maximize); WriteInt(FontSize); WriteInt(FontAscent); WriteInt(FontDescent); WriteInt(DefaultToAscii); WriteInt(DefaultToSbcs); WriteInt(KeepFileFilter); WriteInt(FileFilterIndex); WriteInt(FileOpenMulti); WriteInt(TabTitleMaxWidth); if (!(DocTabPosition && strchr("TBLR",DocTabPosition))) { DocTabPosition='T'; } char dtp[2]={DocTabPosition,'\0'}; reg->writeStringEntry(view_sect,"DocTabPosition",dtp); if (!(DocTabsPacked && strchr("UPA",DocTabsPacked))) { DocTabsPacked='T'; } dtp[0]=DocTabsPacked; reg->writeStringEntry(view_sect,"DocTabsPacked",dtp); WriteStr(FontName); WriteStr(FileFilters); WriteStr(ShellCommand); FreeAllKeywordLists(); mnumgr->WriteMenuSpecs(reg,keys_sect); mnumgr->WriteToolbarButtons(reg,tbar_sect); mnumgr->WritePopupMenu(reg,popup_sect); SaveErrorPatterns(reg); SaveSysIncPaths(reg); if (!style_reg->unparseFile(style_file)) { FXMessageBox::error(app,MBOX_OK, _("Configuration error"), "%s \n%s\n%s", _("Failed to save settings to"), style_file.text(), SystemErrorStr() ); } delete style_reg; ini_sort(style_file.text()); }
Settings::Settings(FXMainWindow*w, const FXString &configdir, MenuMgr*mmgr):SettingsBase() { mnumgr=mmgr; FXString tmp; reg=&(w->getApp()->reg()); style_reg=new FXSettings(); app=w->getApp(); style_file=configdir+STYLE_FILE; if (use_xdg_config()) { style_file.append(".rc"); } #ifdef FOX_1_6 // Fox-1.6 will choke reading string entries > 2000 chars, so rewrite // the styles file, in case it was written by a newer version of Fox. if (FXStat::exists(style_file)) { FXFile style_fh(style_file,FXIO::Reading); if (style_fh.isOpen()) { FXString style_data('\0', style_fh.size()+1); style_fh.readBlock((char*)style_data.text(), style_fh.size()); style_fh.close(); FXint n=style_data.contains(ENDLINE); bool toolong=false; for (FXint i=0; i<=n; i++) { if (style_data.section(ENDLINE,i).length()>1952) { toolong=true; break; } } if (toolong && style_fh.open(style_file,FXIO::Writing)) { FXint eoln=strlen(ENDLINE); for (FXint i=0; i<=n; i++) { FXString line=style_data.section(ENDLINE,i); line.trunc(1952); if (strlen(line.text())) { style_fh.writeBlock(line.text(),line.length()); } style_fh.writeBlock(ENDLINE,eoln); } style_fh.close(); } } } #endif if ( (FXStat::exists(style_file)) && (!style_reg->parseFile(style_file, true))) { FXMessageBox::error(app,MBOX_OK, _("Configuration error"), "%s \n%s\n%s", _("Failed to read settings from"), style_file.text(), SystemErrorStr() ); } ReadInt(ShowStatusBar,true); ReadInt(ShowLineNumbers,false); ReadInt(ShowToolbar,true); ReadInt(ShowWhiteSpace,false); ReadInt(ShowOutputPane,false); bool InvertColors=false; ReadInt(InvertColors,false); ColorFuncs::InvertColors(InvertColors); ReadIntRng(SplitView,0,SPLIT_NONE,SPLIT_BESIDE); ReadInt(OutputPaneHeight,64); ReadInt(SmartHome,false); ReadInt(WrapAwareHomeEnd,false); ReadIntRng(BraceMatch,BRACEMATCH_EITHER,BRACEMATCH_NONE,BRACEMATCH_AFTER); ReadInt(UseTabs,true); ReadInt(CaretPastEOL,false); ReadIntRng(TabWidth,4,1,16); ReadIntRng(IndentWidth,TabWidth,1,16); ReadIntRng(CaretWidth,1,1,3); ReadIntRng(RightEdgeColumn,80,1,1024); ReadInt(ShowRightEdge,false); ReadInt(ShowIndentGuides,false); ReadInt(ShowCaretLine,true); ReadInt(SmoothScroll,true); ReadIntRng(WheelLines,3,1,32); ReadIntRng(SearchWrap,SEARCH_WRAP_ASK,SEARCH_WRAP_NEVER,SEARCH_WRAP_ASK); ReadIntRng(SearchGui,SEARCH_GUI_BELOW,SEARCH_GUI_ABOVE,SEARCH_GUI_FLOAT); ReadIntRng(AutoIndent,AUTO_INDENT_NONE,AUTO_INDENT_NONE,AUTO_INDENT_SMART); ReadInt(SearchVerbose,true); ReadInt(SearchOptions,0); ReadInt(ZoomFactor,0); ReadIntRng(MaxFiles,128,1,999); ReadInt(PromptCloseMultiMenu,true); ReadInt(PromptCloseMultiExit,true); ReadInt(WatchExternChanges,true); ReadInt(Autosave,false); ReadIntRng(AutosaveInterval,60,15,900); ReadInt(SaveBeforeFilterSel,false); ReadInt(SaveBeforeInsCmd,false); ReadInt(SaveBeforeExecCmd,true); ReadInt(WhitespaceShowsEOL,true); ReadIntRng(DefaultFileFormat,DEFAULT_EOL_FORMAT,0,2); ReadInt(DefaultToAscii,!LocaleIsUTF8()); ReadInt(DefaultToSbcs,true); ReadIntRng(KeepFileFilter,REMEMBER_NEVER,REMEMBER_NEVER,REMEMBER_ALWAYS); if (KeepFileFilter==REMEMBER_ALWAYS) { ReadInt(FileFilterIndex,0); } else { FileFilterIndex=0; } ReadInt(FileOpenMulti,false); ReadInt(WordWrap,false); ReadInt(WrapToolbar,true); ReadIntRng(ToolbarButtonSize,1,0,2);// 0=small; 1=medium; 2=large placement=reg->existingEntry(geom_sect,"Top")?PLACEMENT_DEFAULT:PLACEMENT_SCREEN; ReadInt(Left,0); ReadInt(Top,0); ReadInt(Width,0); ReadInt(Height,0); ScreenWidth=w->getApp()->getRootWindow()->getDefaultWidth(); ScreenHeight=w->getApp()->getRootWindow()->getDefaultHeight(); if ((Width==0)&&(Height==0)) { // First run, size based on screen dimensions Width=ScreenWidth*(4.0/5.0); Height=ScreenHeight*(3.0/4.0); LIMIT_RANGE(Width,600,Width); LIMIT_RANGE(Height,400,Height); } LIMIT_RANGE(Left,0,Left); LIMIT_RANGE(Top,0,Top); LIMIT_RANGE(Width,160,Width); LIMIT_RANGE(Height,120,Height); ReadInt(Maximize,false); ReadIntRng(TabTitleMaxWidth,ScreenWidth/6,0,ScreenWidth); reg->deleteSection("LastFocused"); ReadInt(FontSize,120); ReadIntRng(FontAscent,2,0,16); ReadIntRng(FontDescent,0,0,16); if (reg->existingEntry(edit_sect,"FontName")) { ReadStr(FontName,"Fixed [Misc]"); } else { FindFont(FontName); } tmp=reg->readStringEntry(view_sect,"DocTabPosition","T"); if (tmp.empty() || !strchr("TBLR",tmp.text()[0])) { DocTabPosition='T'; } else { DocTabPosition=tmp.text()[0]; } tmp=reg->readStringEntry(view_sect,"DocTabsPacked","A"); if (tmp=="0") { tmp="U"; } else if (tmp=="1") { tmp="P"; } if (tmp.empty() || !strchr("UPA",tmp.text()[0])) { DocTabsPacked='A'; } else { DocTabsPacked=tmp.text()[0]; } ReadStr(FileFilters, default_file_filters); FileFilters.substitute("|", "\n", true); ReadStr(ShellCommand,SHELL_COMMAND); ParseStyles(style_reg, global_sect, GlobalStyle); ParseStyle(style_reg, global_sect, &WhiteSpaceStyle); ParseStyle(style_reg, global_sect, &CaretLineStyle); ParseStyle(style_reg, global_sect, &RightMarginStyle); for (LangStyle*ls=languages; ls->name; ls++) { if (ls->words) { int i; for (i=0; ls->words[i]; i++) { char buf[256]; snprintf(buf,sizeof(buf)-1, "words_%d", i+1); tmp=style_reg->readStringEntry(ls->name,buf,ls->words[i]); SetKeywordList(ls,i,tmp); } } ParseStyles(style_reg, ls->name, ls->styles); tmp=style_reg->readStringEntry(ls->name,"FileMask",ls->mask?ls->mask:""); if ( (ls->mask && (strcmp(ls->mask,tmp.text())!=0))||((ls->mask==NULL)&&(!tmp.empty())) ) { ls->mask=strdup(tmp.text()); ls->mallocs |= LANGSTYLE_MASK_ALLOCD; } tmp=style_reg->readStringEntry(ls->name,"ShebangApps",ls->apps?ls->apps:""); if ( (ls->apps && (strcmp(ls->apps,tmp.text())!=0))||((ls->apps==NULL)&&(!tmp.empty())) ){ ls->apps=strdup(tmp.text()); ls->mallocs |= LANGSTYLE_APPS_ALLOCD; } ls->tabs=(TabPolicy)(style_reg->readIntEntry(ls->name,"TabPolicy", ls->tabs)); ls->tabwidth=style_reg->readIntEntry(ls->name,"TabWidth", 0); LIMIT_RANGE(ls->tabwidth,0,16); } styles=GlobalStyle; mnumgr->ReadMenuSpecs(reg,keys_sect); mnumgr->ReadToolbarButtons(reg,tbar_sect); mnumgr->ReadPopupMenu(reg,popup_sect); ReadErrorPatterns(reg); ReadSysIncPaths(reg); }