fpini.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. Write/Read Options to INI File
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit FPIni;
  13. interface
  14. uses
  15. FPUtils;
  16. const
  17. ININame = 'fp.ini';
  18. ConfigDir : string = '.'+DirSep;
  19. INIFileName: string = ININame;
  20. procedure InitINIFile;
  21. function ReadINIFile: boolean;
  22. function WriteINIFile: boolean;
  23. implementation
  24. uses
  25. Dos,Objects,Drivers,
  26. WINI,{$ifndef EDITORS}WEditor{$else}Editors{$endif},
  27. FPDebug,FPConst,FPVars,
  28. FPIntf,FPTools,FPSwitch;
  29. const
  30. { INI file sections }
  31. secFiles = 'Files';
  32. secRun = 'Run';
  33. secCompile = 'Compile';
  34. secColors = 'Colors';
  35. secHelp = 'Help';
  36. secEditor = 'Editor';
  37. secBreakpoint = 'Breakpoints';
  38. secHighlight = 'Highlight';
  39. secMouse = 'Mouse';
  40. secSearch = 'Search';
  41. secTools = 'Tools';
  42. secSourcePath = 'SourcePath';
  43. { INI file tags }
  44. ieRecentFile = 'RecentFile';
  45. ieRunParameters = 'Parameters';
  46. iePrimaryFile = 'PrimaryFile';
  47. ieCompileMode = 'CompileMode';
  48. iePalette = 'Palette';
  49. ieHelpFiles = 'Files';
  50. ieDefaultTabSize = 'DefaultTabSize';
  51. ieDefaultEditorFlags='DefaultFlags';
  52. ieDefaultSaveExt = 'DefaultSaveExt';
  53. ieOpenExts = 'OpenExts';
  54. ieHighlightExts = 'Exts';
  55. ieTabsPattern = 'NeedsTabs';
  56. ieDoubleClickDelay = 'DoubleDelay';
  57. ieReverseButtons = 'ReverseButtons';
  58. ieAltClickAction = 'AltClickAction';
  59. ieCtrlClickAction = 'CtrlClickAction';
  60. ieFindFlags = 'FindFlags';
  61. ieToolName = 'Title';
  62. ieToolProgram = 'Program';
  63. ieToolParams = 'Params';
  64. ieToolHotKey = 'HotKey';
  65. ieBreakpointTyp = 'Type';
  66. ieBreakpointCount = 'Count';
  67. ieBreakpointState = 'State';
  68. ieBreakpointName = 'Name';
  69. ieBreakpointFile = 'FileName';
  70. ieBreakpointLine = 'LineNumber';
  71. ieBreakpointCond = 'Condition';
  72. ieSourceList = 'SourceList';
  73. procedure InitINIFile;
  74. var S: string;
  75. begin
  76. S:=LocateFile(ININame);
  77. if S<>'' then
  78. INIPath:=S;
  79. INIPath:=FExpand(INIPath);
  80. end;
  81. function PaletteToStr(S: string): string;
  82. var C: string;
  83. I: integer;
  84. begin
  85. C:='';
  86. for I:=1 to length(S) do
  87. begin
  88. C:=C+'#$'+IntToHexL(ord(S[I]),2);
  89. end;
  90. PaletteToStr:=C;
  91. end;
  92. function StrToPalette(S: string): string;
  93. var I,P,X: integer;
  94. C: string;
  95. Hex: boolean;
  96. OK: boolean;
  97. begin
  98. C:=''; I:=1;
  99. OK:=S<>'';
  100. while OK and (I<=length(S)) and (S[I]='#') do
  101. begin
  102. Inc(I); Hex:=false;
  103. if S[I]='$' then begin Inc(I); Hex:=true; end;
  104. P:=Pos('#',copy(S,I,255)); if P>0 then P:=I+P-1 else P:=length(S)+1;
  105. if Hex=false then
  106. begin
  107. X:=StrToInt(copy(S,I,P-I));
  108. OK:=(LastStrToIntResult=0) and (0<=X) and (X<=255);
  109. end
  110. else
  111. begin
  112. X:=HexToInt(copy(S,I,P-I));
  113. OK:=(LastHexToIntResult=0) and (0<=X) and (X<=255);
  114. end;
  115. if OK then C:=C+chr(X);
  116. Inc(I,P-I);
  117. end;
  118. StrToPalette:=C;
  119. end;
  120. procedure WriteOneBreakPointEntry(I : longint;INIFile : PINIFile);
  121. var PB : PBreakpoint;
  122. S : String;
  123. begin
  124. Str(I,S);
  125. PB:=BreakpointCollection^.At(I);
  126. If assigned(PB) then
  127. With PB^ do
  128. Begin
  129. INIFile^.SetEntry(secBreakpoint,ieBreakpointTyp+S,BreakpointTypeStr[typ]);
  130. INIFile^.SetEntry(secBreakpoint,ieBreakpointState+S,BreakpointStateStr[state]);
  131. if typ=bt_file_line then
  132. begin
  133. INIFile^.SetEntry(secBreakpoint,ieBreakpointFile+S,FileName^);
  134. INIFile^.SetIntEntry(secBreakpoint,ieBreakpointLine+S,Line);
  135. end
  136. else
  137. INIFile^.SetEntry(secBreakpoint,ieBreakpointName+S,Name^);
  138. if assigned(Conditions) then
  139. INIFile^.SetEntry(secBreakpoint,ieBreakpointCond+S,Conditions^);
  140. end;
  141. end;
  142. procedure ReadOneBreakPointEntry(i : longint;INIFile : PINIFile);
  143. var PB : PBreakpoint;
  144. S,S2,SC : string;
  145. Line : longint;
  146. typ : BreakpointType;
  147. state : BreakpointState;
  148. begin
  149. Str(I,S2);
  150. typ:=bt_invalid;
  151. S:=INIFile^.GetEntry(secBreakpoint,ieBreakpointTyp+S2,BreakpointTypeStr[typ]);
  152. for typ:=low(BreakpointType) to high(BreakpointType) do
  153. If pos(BreakpointTypeStr[typ],S)>0 then break;
  154. state:=bs_deleted;
  155. S:=INIFile^.GetEntry(secBreakpoint,ieBreakpointState+S2,BreakpointStateStr[state]);
  156. for state:=low(BreakpointState) to high(BreakpointState) do
  157. If pos(BreakpointStateStr[state],S)>0 then break;
  158. case typ of
  159. bt_invalid :;
  160. bt_file_line :
  161. begin
  162. S:=INIFile^.GetEntry(secBreakpoint,ieBreakpointFile+S2,'');
  163. Line:=INIFile^.GetIntEntry(secBreakpoint,ieBreakpointLine+S2,0);
  164. end;
  165. else
  166. begin
  167. S:=INIFile^.GetEntry(secBreakpoint,ieBreakpointName+S2,'');
  168. end;
  169. end;
  170. SC:=INIFile^.GetEntry(secBreakpoint,ieBreakpointCond+S,'');
  171. if (typ=bt_function) and (S<>'') then
  172. new(PB,init_function(S))
  173. else if (typ=bt_file_line) and (S<>'') then
  174. new(PB,init_file_line(S,Line))
  175. else
  176. new(PB,init_type(typ,S));
  177. If assigned(PB) then
  178. begin
  179. PB^.state:=state;
  180. If SC<>'' then
  181. PB^.conditions:=NewStr(SC);
  182. BreakpointCollection^.Insert(PB);
  183. end;
  184. end;
  185. function ReadINIFile: boolean;
  186. var INIFile: PINIFile;
  187. S,PS,S1,S2,S3: string;
  188. I,P: integer;
  189. BreakPointCount:longint;
  190. ts : TSwitchMode;
  191. W: word;
  192. begin
  193. ReadINIFile:=false;
  194. if not ExistsFile(INIPath) then
  195. exit;
  196. New(INIFile, Init(INIPath));
  197. { Files }
  198. OpenExts:=INIFile^.GetEntry(secFiles,ieOpenExts,OpenExts);
  199. RecentFileCount:=High(RecentFiles);
  200. for I:=Low(RecentFiles) to High(RecentFiles) do
  201. begin
  202. S:=INIFile^.GetEntry(secFiles,ieRecentFile+IntToStr(I),'');
  203. if (S='') and (RecentFileCount>I-1) then RecentFileCount:=I-1;
  204. with RecentFiles[I] do
  205. begin
  206. P:=Pos(',',S); if P=0 then P:=length(S)+1;
  207. FileName:=copy(S,1,P-1); Delete(S,1,P);
  208. P:=Pos(',',S); if P=0 then P:=length(S)+1;
  209. LastPos.X:=Max(0,StrToInt(copy(S,1,P-1))); Delete(S,1,P);
  210. P:=Pos(',',S); if P=0 then P:=length(S)+1;
  211. LastPos.Y:=Max(0,StrToInt(copy(S,1,P-1))); Delete(S,1,P);
  212. end;
  213. end;
  214. { Run }
  215. SetRunParameters(INIFile^.GetEntry(secRun,ieRunParameters,GetRunParameters));
  216. { Compile }
  217. PrimaryFile:=INIFile^.GetEntry(secCompile,iePrimaryFile,PrimaryFile);
  218. S:=INIFile^.GetEntry(secCompile,ieCompileMode,'');
  219. for ts:=low(TSwitchMode) to high(TSwitchMode) do
  220. if SwitchesModeStr[ts]=S then
  221. SwitchesMode:=ts;
  222. { Help }
  223. S:=INIFile^.GetEntry(secHelp,ieHelpFiles,'');
  224. repeat
  225. P:=Pos(';',S); if P=0 then P:=length(S)+1;
  226. PS:=copy(S,1,P-1);
  227. if PS<>'' then HelpFiles^.Insert(NewStr(PS));
  228. Delete(S,1,P);
  229. until S='';
  230. { Editor }
  231. {$ifndef EDITORS}
  232. DefaultTabSize:=INIFile^.GetIntEntry(secEditor,ieDefaultTabSize,DefaultTabSize);
  233. DefaultCodeEditorFlags:=INIFile^.GetIntEntry(secEditor,ieDefaultEditorFlags,DefaultCodeEditorFlags);
  234. DefaultSaveExt:=INIFile^.GetEntry(secEditor,ieDefaultSaveExt,DefaultSaveExt);
  235. {$endif}
  236. { Highlight }
  237. HighlightExts:=INIFile^.GetEntry(secHighlight,ieHighlightExts,HighlightExts);
  238. TabsPattern:=INIFile^.GetEntry(secHighlight,ieTabsPattern,TabsPattern);
  239. { SourcePath }
  240. SourceDirs:=INIFile^.GetEntry(secSourcePath,ieSourceList,SourceDirs);
  241. { Mouse }
  242. DoubleDelay:=INIFile^.GetIntEntry(secMouse,ieDoubleClickDelay,DoubleDelay);
  243. MouseReverse:=boolean(INIFile^.GetIntEntry(secMouse,ieReverseButtons,byte(MouseReverse)));
  244. AltMouseAction:=INIFile^.GetIntEntry(secMouse,ieAltClickAction,AltMouseAction);
  245. CtrlMouseAction:=INIFile^.GetIntEntry(secMouse,ieCtrlClickAction,CtrlMouseAction);
  246. { Search }
  247. FindFlags:=INIFile^.GetIntEntry(secSearch,ieFindFlags,FindFlags);
  248. { Breakpoints }
  249. BreakpointCount:=INIFile^.GetIntEntry(secBreakpoint,ieBreakpointCount,0);
  250. for i:=1 to BreakpointCount do
  251. ReadOneBreakPointEntry(i-1,INIFile);
  252. { Tools }
  253. for I:=1 to MaxToolCount do
  254. begin
  255. S:=IntToStr(I);
  256. S1:=INIFile^.GetEntry(secTools,ieToolName+S,'');
  257. if S1='' then Break; { !!! }
  258. S2:=INIFile^.GetEntry(secTools,ieToolProgram+S,'');
  259. S3:=INIFile^.GetEntry(secTools,ieToolParams+S,'');
  260. W:=Max(0,Min(65535,INIFile^.GetIntEntry(secTools,ieToolHotKey+S,0)));
  261. AddTool(S1,S2,S3,W);
  262. end;
  263. { Colors }
  264. S:=AppPalette;
  265. PS:=StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_1_40',PaletteToStr(copy(S,1,40))));
  266. PS:=PS+StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_41_80',PaletteToStr(copy(S,41,40))));
  267. PS:=PS+StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_81_120',PaletteToStr(copy(S,81,40))));
  268. PS:=PS+StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_121_160',PaletteToStr(copy(S,121,40))));
  269. PS:=PS+StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_161_200',PaletteToStr(copy(S,161,40))));
  270. PS:=PS+StrToPalette(INIFile^.GetEntry(secColors,iePalette+'_201_240',PaletteToStr(copy(S,201,40))));
  271. AppPalette:=PS;
  272. Dispose(INIFile, Done);
  273. ReadINIFile:=true;
  274. end;
  275. function WriteINIFile: boolean;
  276. var INIFile: PINIFile;
  277. S: string;
  278. S1,S2,S3: string;
  279. W: word;
  280. BreakPointCount:longint;
  281. I: integer;
  282. OK: boolean;
  283. procedure ConcatName(P: PString); {$ifndef FPC}far;{$endif}
  284. begin
  285. if (S<>'') then S:=S+';';
  286. S:=S+P^;
  287. end;
  288. begin
  289. New(INIFile, Init(INIPath));
  290. { Files }
  291. INIFile^.SetEntry(secFiles,ieOpenExts,'"'+OpenExts+'"');
  292. for I:=1 to High(RecentFiles) do
  293. begin
  294. if I<=RecentFileCount then
  295. with RecentFiles[I] do S:=FileName+','+IntToStr(LastPos.X)+','+IntToStr(LastPos.Y)
  296. else
  297. S:='';
  298. INIFile^.SetEntry(secFiles,ieRecentFile+IntToStr(I),S);
  299. end;
  300. { Run }
  301. INIFile^.SetEntry(secRun,ieRunParameters,GetRunParameters);
  302. { Compile }
  303. INIFile^.SetEntry(secCompile,iePrimaryFile,PrimaryFile);
  304. INIFile^.SetEntry(secCompile,ieCompileMode,SwitchesModeStr[SwitchesMode]);
  305. { Help }
  306. S:='';
  307. HelpFiles^.ForEach(@ConcatName);
  308. INIFile^.SetEntry(secHelp,ieHelpFiles,'"'+S+'"');
  309. { Editor }
  310. {$ifndef EDITORS}
  311. INIFile^.SetIntEntry(secEditor,ieDefaultTabSize,DefaultTabSize);
  312. INIFile^.SetIntEntry(secEditor,ieDefaultEditorFlags,DefaultCodeEditorFlags);
  313. INIFile^.SetEntry(secEditor,ieDefaultSaveExt,DefaultSaveExt);
  314. {$endif}
  315. { Highlight }
  316. INIFile^.SetEntry(secHighlight,ieHighlightExts,'"'+HighlightExts+'"');
  317. INIFile^.SetEntry(secHighlight,ieTabsPattern,'"'+TabsPattern+'"');
  318. { SourcePath }
  319. INIFile^.SetEntry(secSourcePath,ieSourceList,'"'+SourceDirs+'"');
  320. { Mouse }
  321. INIFile^.SetIntEntry(secMouse,ieDoubleClickDelay,DoubleDelay);
  322. INIFile^.SetIntEntry(secMouse,ieReverseButtons,byte(MouseReverse));
  323. INIFile^.SetIntEntry(secMouse,ieAltClickAction,AltMouseAction);
  324. INIFile^.SetIntEntry(secMouse,ieCtrlClickAction,CtrlMouseAction);
  325. { Search }
  326. INIFile^.SetIntEntry(secSearch,ieFindFlags,FindFlags);
  327. { Breakpoints }
  328. BreakPointCount:=BreakpointCollection^.Count;
  329. INIFile^.SetIntEntry(secBreakpoint,ieBreakpointCount,BreakpointCount);
  330. for i:=1 to BreakpointCount do
  331. WriteOneBreakPointEntry(I-1,INIFile);
  332. { Tools }
  333. INIFile^.DeleteSection(secTools);
  334. for I:=1 to GetToolCount do
  335. begin
  336. S:=IntToStr(I);
  337. GetToolParams(I-1,S1,S2,S3,W);
  338. if S1<>'' then S1:='"'+S1+'"';
  339. if S2<>'' then S2:='"'+S2+'"';
  340. if S3<>'' then S3:='"'+S3+'"';
  341. INIFile^.SetEntry(secTools,ieToolName+S,S1);
  342. INIFile^.SetEntry(secTools,ieToolProgram+S,S2);
  343. INIFile^.SetEntry(secTools,ieToolParams+S,S3);
  344. INIFile^.SetIntEntry(secTools,ieToolHotKey+S,W);
  345. end;
  346. { Colors }
  347. if AppPalette<>CIDEAppColor then
  348. begin
  349. { this has a bug. if a different palette has been read on startup, and
  350. then changed back to match the default, this will not update it in the
  351. ini file, eg. the original (non-default) will be left unmodified... }
  352. S:=AppPalette;
  353. INIFile^.SetEntry(secColors,iePalette+'_1_40',PaletteToStr(copy(S,1,40)));
  354. INIFile^.SetEntry(secColors,iePalette+'_41_80',PaletteToStr(copy(S,41,40)));
  355. INIFile^.SetEntry(secColors,iePalette+'_81_120',PaletteToStr(copy(S,81,40)));
  356. INIFile^.SetEntry(secColors,iePalette+'_121_160',PaletteToStr(copy(S,121,40)));
  357. INIFile^.SetEntry(secColors,iePalette+'_161_200',PaletteToStr(copy(S,161,40)));
  358. INIFile^.SetEntry(secColors,iePalette+'_201_240',PaletteToStr(copy(S,201,40)));
  359. end;
  360. OK:=INIFile^.Update;
  361. Dispose(INIFile, Done);
  362. WriteINIFile:=OK;
  363. end;
  364. end.
  365. {
  366. $Log$
  367. Revision 1.13 1999-02-22 02:15:14 peter
  368. + default extension for save in the editor
  369. + Separate Text to Find for the grep dialog
  370. * fixed redir crash with tp7
  371. Revision 1.12 1999/02/19 18:43:46 peter
  372. + open dialog supports mask list
  373. Revision 1.11 1999/02/10 09:53:14 pierre
  374. * better storing of breakpoints
  375. Revision 1.10 1999/02/05 13:08:42 pierre
  376. + new breakpoint types added
  377. Revision 1.9 1999/02/05 12:11:55 pierre
  378. + SourceDir that stores directories for sources that the
  379. compiler should not know about
  380. Automatically asked for addition when a new file that
  381. needed filedialog to be found is in an unknown directory
  382. Stored and retrieved from INIFile
  383. + Breakpoints conditions added to INIFile
  384. * Breakpoints insterted and removed at debin and end of debug session
  385. Revision 1.8 1999/02/04 17:52:38 pierre
  386. * bs_invalid renamed bs_deleted
  387. Revision 1.7 1999/02/04 17:19:24 peter
  388. * linux fixes
  389. Revision 1.6 1999/02/04 13:32:04 pierre
  390. * Several things added (I cannot commit them independently !)
  391. + added TBreakpoint and TBreakpointCollection
  392. + added cmResetDebugger,cmGrep,CmToggleBreakpoint
  393. + Breakpoint list in INIFile
  394. * Select items now also depend of SwitchMode
  395. * Reading of option '-g' was not possible !
  396. + added search for -Fu args pathes in TryToOpen
  397. + added code for automatic opening of FileDialog
  398. if source not found
  399. Revision 1.5 1999/01/21 11:54:15 peter
  400. + tools menu
  401. + speedsearch in symbolbrowser
  402. * working run command
  403. Revision 1.4 1999/01/04 11:49:45 peter
  404. * 'Use tab characters' now works correctly
  405. + Syntax highlight now acts on File|Save As...
  406. + Added a new class to syntax highlight: 'hex numbers'.
  407. * There was something very wrong with the palette managment. Now fixed.
  408. + Added output directory (-FE<xxx>) support to 'Directories' dialog...
  409. * Fixed some possible bugs in Running/Compiling, and the compilation/run
  410. process revised
  411. Revision 1.1 1998/12/28 15:47:45 peter
  412. + Added user screen support, display & window
  413. + Implemented Editor,Mouse Options dialog
  414. + Added location of .INI and .CFG file
  415. + Option (INI) file managment implemented (see bottom of Options Menu)
  416. + Switches updated
  417. + Run program
  418. }