DATADEF.MOD 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. MODULE DataDef;
  2. (*
  3. * ModBase
  4. * Release 3.0
  5. * (c) Copyright 1986 - 1991 PMI
  6. Copyright 1988 - 1991 John McMonagle
  7. * P.O. Box 8402
  8. * Green Bay Wi 53308
  9. * All Rights Reserved
  10. * by Ed Ross
  11. *)
  12. FROM Gen IMPORT GenFile;
  13. FROM MakeDBF IMPORT MakeDataBaseFile;
  14. FROM DataTypes IMPORT DataElmtRec,IndexElmtRec,TableName,FldList,IdxList,
  15. DBFName,FullTableName;
  16. FROM GenEdt IMPORT GenEdtFile;
  17. FROM EnvironUtils IMPORT ReadEnvironment;
  18. FROM NumTypes IMPORT Real8;
  19. FROM Str IMPORT Copy;
  20. FROM SmartScreen IMPORT ClearScreen;
  21. FROM Hash IMPORT Define,Insert,KeyFind,HashTable,GetData;
  22. FROM StrConv IMPORT RealToStr,CardinalToStr;
  23. FROM ScrnUtl1 IMPORT GetFieldImageRec;
  24. FROM Tables IMPORT Table,CellValue,GetCell,PutCell,BuildTable,
  25. DefineTextCol,DefineIntegerCol,DefineGroupCol,DefineTable,ControlTable,
  26. DeleteTable,ShowTable,NumberOfRows;
  27. FROM GenLists IMPORT GenList,ListLength,GetElmtAdr,BlockToList,
  28. GetElmt, ListInsertAdr,NewList,DisposeList,ListInsert,ShellSortList;
  29. FROM HandleIO IMPORT BlockRead,BlockWrite,FileExists,
  30. OpenFile,CloseHandle,CreateFile;
  31. FROM StringIO IMPORT ErrorMessage,NoError,WriteEol,outp;
  32. FROM PosUtils IMPORT Equal,Present,Pos;
  33. FROM Prompts IMPORT PromptStr,PromptYN;
  34. FROM M2Strings IMPORT Length,CompareStr;
  35. FROM StrEdit IMPORT CAPstr,CrunchBlanks,Append,SetLength,AssignStr,LowerStr,
  36. DeleteRightJustified,CAPstr;
  37. FROM ControlUtils IMPORT AddMenuItem,ChangeField,ReadInput,Control;
  38. FROM DirManager IMPORT DirToFrame;
  39. FROM Drectory IMPORT GetDrivePathAndName,GetCurrentDir,GetDefaultDrive,
  40. SetDefaultDrive,ChDir;
  41. FROM FramePainter IMPORT ShowDisplayFrame;
  42. FROM InputManager IMPORT ControlFrame;
  43. FROM ScrnTypes IMPORT InitDisplayFrame,DisplayFrame,AFrameName,ImageElement;
  44. FROM ScrnUtl2 IMPORT CloseDisplayFrame;
  45. FROM LowLevel IMPORT Fill;
  46. FROM ModBase3 IMPORT DBFile, DBFieldDescriptor,AppendBlank,GetField,
  47. CloseDBF,WriteDBRec,BuildDBF;
  48. FROM DBIndxes IMPORT DBIndex,InitIndex,CloseIndex,BuildIndex,AddRecord,
  49. InitCompIndex,BuildCompIndex;
  50. FROM SYSTEM IMPORT TSIZE,ADR,ADDRESS;
  51. IMPORT VWindows;
  52. FROM VStorage IMPORT DosAlloc,DosDealloc;
  53. FROM DBUtils IMPORT GetSelected,SelectFromScreen;
  54. IMPORT InitCompilerMods;
  55. VAR
  56. DF : DisplayFrame;
  57. Tab : Table;
  58. SerList : GenList;
  59. FileName : ARRAY[0..20] OF CHAR;
  60. Card : CARDINAL;
  61. EM : ErrorMessage;
  62. Code,J : CARDINAL;
  63. Bool : BOOLEAN;
  64. Environ : ARRAY[0..60] OF CHAR;
  65. (* Table should look like this
  66. 1234567890123456789012345678901234567890123456789012345678901234567890123456
  67. 1 2 3 4 5 6 7
  68. Index Name Type Length Dec Description *)
  69. PROCEDURE SetupTable();
  70. BEGIN
  71. DefineTable(Tab,'Data Definitions');
  72. DefineTextCol(Tab,3,1,'Index',FALSE);
  73. DefineTextCol(Tab,10,10,'Name',FALSE);
  74. DefineTextCol(Tab,22,1,'Type',FALSE);
  75. DefineIntegerCol(Tab,27,5,'Length',0,9999);
  76. DefineIntegerCol(Tab,35,2,'Dec',0,99);
  77. DefineTextCol(Tab,40,35,'Desc',FALSE);
  78. END SetupTable;
  79. PROCEDURE GetDataElmts(FileName : ARRAY OF CHAR);
  80. (* return a list of all products *)
  81. VAR
  82. H : CARDINAL;
  83. FldDesc: DataElmtRec;
  84. EM : ErrorMessage;
  85. Size : CARDINAL;
  86. J : CARDINAL;
  87. BEGIN
  88. IF FileExists(FileName)
  89. THEN
  90. EM := OpenFile(H,FileName);
  91. EM := BlockRead(H,ADR(FldDesc),SIZE(FldDesc)); (* get the first*)
  92. WHILE (EM = 0) DO
  93. ListInsert(FldDesc,1,FldList,1000); (* put at the end*)
  94. EM := BlockRead(H,ADR(FldDesc),SIZE(FldDesc));
  95. END; (* end while *)
  96. EM := CloseHandle(H);
  97. END;
  98. END GetDataElmts;
  99. PROCEDURE UpdateDataElmtsFile(FileName : ARRAY OF CHAR);
  100. VAR
  101. Row : CARDINAL;
  102. FldDesc : DataElmtRec;
  103. Cell : CellValue;
  104. Code,Size : CARDINAL;
  105. H : CARDINAL;
  106. EM : ErrorMessage;
  107. BEGIN
  108. DisposeList(FldList); (* start the list over *)
  109. NewList(FldList);
  110. EM := CreateFile(H,FileName);
  111. FOR Row := 1 TO NumberOfRows(Tab) DO (* read the screen & get elemts*)
  112. GetCell(Tab,1,Row,Cell);
  113. FldDesc.Idx := Cell.Str[0]; (* indexed field*)
  114. CAPstr(FldDesc.Idx);
  115. GetCell(Tab,2,Row,Cell); (* fld name *)
  116. CAPstr(Cell.Str);
  117. Copy(FldDesc.Name , Cell.Str);
  118. GetCell(Tab, 3,Row,Cell); (* Fld Type *)
  119. CAPstr(Cell.Str);
  120. FldDesc.Type := Cell.Str[0];
  121. GetCell(Tab,4,Row,Cell); (* fld lenght *)
  122. FldDesc.Len := Cell.I;
  123. GetCell(Tab,5,Row,Cell); (* dec positions *)
  124. FldDesc.Dec := Cell.I;
  125. GetCell(Tab,6,Row,Cell);
  126. Copy(FldDesc.Desc , Cell.Str); (* description *)
  127. IF (FldDesc.Name[0] # ' ') AND(FldDesc.Idx # 'D')
  128. THEN
  129. ListInsert(FldDesc,1,FldList,1000); (* put at the end*)
  130. EM := BlockWrite(H,ADR(FldDesc),SIZE(FldDesc));
  131. END;
  132. END; (* end for row *)
  133. EM := CloseHandle(H);
  134. END UpdateDataElmtsFile;
  135. PROCEDURE DefineDataElmts(TableName : ARRAY OF CHAR);
  136. VAR
  137. J : CARDINAL;
  138. Cell : CellValue;
  139. Row : CARDINAL;
  140. DataElmt : POINTER TO DataElmtRec;
  141. Nbr : ARRAY[0..4] OF CHAR;
  142. Size,Code : CARDINAL;
  143. BEGIN
  144. BuildTable(Tab,ListLength(FldList)+20); (* increase table size by 20*)
  145. FOR Row := 1 TO ListLength(FldList) DO
  146. GetElmtAdr(FldList,Row,DataElmt,Size,Code); (* get address of data elmt *)
  147. Cell.Str[0] := DataElmt^.Idx;
  148. Cell.Str[1] := 0C;
  149. PutCell(Tab,1,Row,Cell); (* indexed *)
  150. Copy(Cell.Str , DataElmt^.Name); (* field names *)
  151. PutCell(Tab,2,Row,Cell);
  152. Cell.Str[0] := DataElmt^.Type; (* field type *)
  153. Cell.Str[1] := 0C;
  154. PutCell(Tab,3,Row,Cell);
  155. Cell.I := DataElmt^.Len; (* field length *)
  156. PutCell(Tab,4,Row,Cell);
  157. Cell.I := DataElmt^.Dec; (* decimal positions *)
  158. PutCell(Tab,5,Row,Cell);
  159. Copy(Cell.Str , DataElmt^.Desc); (* Description *)
  160. PutCell(Tab,6,Row,Cell);
  161. END;
  162. ControlTable(Tab,2,5,78,20);
  163. END DefineDataElmts;
  164. PROCEDURE SelectTable(VAR TableName : ARRAY OF CHAR);
  165. VAR
  166. DF : DisplayFrame;
  167. NxtFrame : AFrameName;
  168. ReturnVal: ARRAY[0..40] OF CHAR;
  169. Row : CARDINAL;
  170. Drive : CHAR;
  171. Card : CARDINAL;
  172. PathName : ARRAY[0..63] OF CHAR;
  173. Str : ARRAY[0..12] OF CHAR;
  174. FileName : AFrameName;
  175. ImageRec: ImageElement;
  176. BEGIN
  177. InitDisplayFrame(DF,VWindows.CurrentWindow);
  178. DF^.action := 'I';
  179. AddMenuItem(DF,2,2,'NEW',30,1,'NEW');
  180. DF^.headline := 3;
  181. EM := DirToFrame('*.ddf',DF,1); (* find data definition file *)
  182. ControlFrame(DF,1,'',TRUE,FileName);
  183. GetFieldImageRec( DF, DF^.CurrentField, ImageRec );
  184. Copy(FileName , ImageRec.text);
  185. Copy(TableName , FileName);
  186. CrunchBlanks(TableName);
  187. IF Equal(TableName,'NEW')
  188. THEN
  189. PromptStr('Enter Table Name ',TableName);
  190. CrunchBlanks(TableName);
  191. IF Present('.',TableName)
  192. THEN
  193. SetLength(TableName,Pos('.',TableName)); (* make sure .ddf type*)
  194. END;
  195. Append(TableName,'.DDF')
  196. END;
  197. Drive := GetDefaultDrive();
  198. Card := GetCurrentDir(Drive,PathName);
  199. FullTableName[0] := Drive;
  200. FullTableName[1] := 0C;
  201. Append(FullTableName,':');
  202. Append(FullTableName,PathName);
  203. IF FullTableName[Length(FullTableName)-1] <> '\'
  204. THEN Append(FullTableName,'\');
  205. END;
  206. Append(FullTableName,TableName);
  207. END SelectTable;
  208. PROCEDURE FixLists();
  209. (* go through the list of fields and create a list of indexes *)
  210. (* Give each index a accelerator key (highlighted key on menu) *)
  211. (* by checking each letter in the field for an unused letter *)
  212. (* the index will be given an name = Fieldname+'IDX' *)
  213. (* the data base will be given the name of the table + 'DBF' *)
  214. PROCEDURE MakeRep(VAR Item : DataElmtRec);
  215. VAR Str : ARRAY [0..10] OF CHAR;
  216. BEGIN
  217. CASE Item.Type OF
  218. 'C' : IF Item.Len = 1
  219. THEN Item.RecType := 'CHAR'
  220. ELSE Item.RecType := 'ARRAY[0..';
  221. CardinalToStr(Item.Len,3,Str);
  222. Append(Item.RecType,Str);
  223. Append(Item.RecType,'] OF CHAR;');
  224. END;
  225. |'N' : IF (Item.Dec = 0 ) AND (Item.Len < 6)
  226. THEN Item.RecType := 'CARDINAL';
  227. ELSIF (Item.Dec = 0)
  228. THEN Item.RecType := 'LONGINT';
  229. ELSE Item.RecType := 'Real8';
  230. END;
  231. |'M' : Item.RecType := 'Memo';
  232. |'D' : Item.RecType := 'Date';
  233. |'L' : Item.RecType := 'BOOLEAN';
  234. END;
  235. END MakeRep;
  236. VAR
  237. J : CARDINAL;
  238. Item : POINTER TO DataElmtRec;
  239. IdxItem : IndexElmtRec;
  240. Size,Code : CARDINAL;
  241. CharSet : SET OF CHAR;
  242. C : CHAR;
  243. K : CARDINAL;
  244. BEGIN
  245. SetLength(TableName,Pos('.',TableName)); (* get rid of file type in name*)
  246. LowerStr(TableName);
  247. CAPstr(TableName[0]);
  248. Copy(DBFName , TableName);
  249. Append(DBFName,'DBF');
  250. (* the index fields will become menu items in
  251. the generated EDT file. Each menu item will
  252. have a selection character highlighted -
  253. Find an unused character in the index name to
  254. highlight *)
  255. CharSet := CharSet/CharSet; (* Charset = the set of used characters *)
  256. INCL(CharSet,'F'); (* First record*)
  257. INCL(CharSet,'L'); (* Last record *)
  258. INCL(CharSet,'N'); (* Next record *)
  259. INCL(CharSet,'P'); (* Prev record *)
  260. INCL(CharSet,'A'); (* Add Record *)
  261. INCL(CharSet,'Q'); (* The oddballs*)
  262. NewList(IdxList);
  263. FOR J := 1 TO ListLength(FldList) DO
  264. GetElmtAdr(FldList,J,Item,Size,Code);
  265. MakeRep(Item^);
  266. CrunchBlanks(Item^.Desc);
  267. IF Item^.Idx = 'I'
  268. THEN
  269. CrunchBlanks(Item^.Name);
  270. Copy(IdxItem.FldName , Item^.Name);
  271. LowerStr(IdxItem.FldName);
  272. CAPstr(IdxItem.FldName[0]);
  273. Copy(IdxItem.EdtName , IdxItem.FldName);
  274. Copy(IdxItem.IdxName , IdxItem.FldName);
  275. IF Equal(Item^.RecType ,'Real8')
  276. THEN IdxItem.IndexType := 'R' (* real type *)
  277. ELSIF Equal(Item^.RecType, 'CARDINAL')
  278. THEN IdxItem.IndexType := 'N' (* cardinal type *)
  279. ELSE IdxItem.IndexType := 'C'; (* everything else is char*)
  280. END;
  281. IF Length(IdxItem.IdxName) > 8
  282. THEN
  283. IdxItem.IdxName[8] := 0C; (* set to max of 8 *)
  284. END;
  285. CrunchBlanks(IdxItem.EdtName);
  286. K := 0;
  287. LOOP
  288. IF K > Length(IdxItem.FldName)
  289. THEN
  290. CAPstr(IdxItem.FldName[0]); (* this will cause a compiler error*)
  291. IdxItem.HighLight := 'Q';
  292. EXIT; (* in the generated program CASE stm*)
  293. END;
  294. C := IdxItem.FldName[K];
  295. CAPstr(C);
  296. IF NOT (C IN CharSet)
  297. THEN
  298. CAPstr(IdxItem.EdtName[K]); (* make highlighted char *)
  299. INCL(CharSet,IdxItem.EdtName[K]); (* add to set of used char*)
  300. IdxItem.HighLight := IdxItem.EdtName[K];
  301. EXIT;
  302. END;
  303. INC(K);
  304. END;
  305. ListInsert(IdxItem,1,IdxList,100);
  306. END;
  307. END; (* end for j *)
  308. END FixLists;
  309. BEGIN
  310. NewList(FldList);
  311. SelectTable(TableName); (* Get name of table *)
  312. GetDataElmts(FullTableName); (* get the elements from the file *)
  313. ClearScreen();
  314. IF PromptYN('Update Table ?',DF)
  315. THEN
  316. ClearScreen();
  317. WriteEol(outp,
  318. ' ....This may take a few minutes to create the data structures ..');
  319. SetupTable();
  320. DefineDataElmts(TableName);
  321. UpdateDataElmtsFile(FullTableName);
  322. END;
  323. FixLists();
  324. IF PromptYN('Create New database file ?',DF)
  325. THEN
  326. ClearScreen();
  327. MakeDataBaseFile(FldList);
  328. END;
  329. (* GenCode(TableName,FldList); *)
  330. IF PromptYN('Create the EDT file?',DF)
  331. THEN
  332. GenEdtFile(TableName,FldList,IdxList);
  333. END;
  334. IF PromptYN('Generate Gode ?',DF)
  335. THEN
  336. ClearScreen();
  337. InitDisplayFrame(DF,VWindows.CurrentWindow);
  338. DF^.action := 'I';
  339. DF^.headline := 2;
  340. EM := DirToFrame('*.TPL',DF,1); (* find data definition file *)
  341. SelectFromScreen(DF);
  342. GetSelected(SerList,DF);
  343. FOR J := 1 TO ListLength(SerList) DO
  344. GetElmt(SerList,J,FileName,Code);
  345. GenFile(FileName);
  346. END;
  347. END;
  348. ClearScreen();
  349. END DataDef.
  350.