fpini.pas 14 KB

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