home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0039_Text File Objects.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-24  |  7.1 KB  |  248 lines

  1. {
  2. Here's a piece of code I wrote last year which does what wasn't
  3. uploaded. It allows text files converted to obj format to be linked in
  4. and being accessed as 'normal' turbo pascal text files. The 'object'
  5. text files support reset, read, readln eof, eoln and close file
  6. commands.
  7.  
  8. What you need to write in your program is a obj_find function which
  9. translates filenames into pointers or returns NIL to indicate an
  10. external file. Use Assign_text procedure instead. A sample of how to
  11. use it is supplied in the second program/unit. Only the two linked in
  12. files will be fetched from memory, any other name supplied will be
  13. fetched from disk as usual.
  14.  
  15. The first unit can be the same for all projects, the second one is
  16. project depended, because one will be using different files.
  17.  
  18. Question about this, ask them!
  19.  
  20. }
  21. {---------------------------------------------------------}
  22. {  Project : Object linked textfiles                      }
  23. {  By      : Ir.G.W. van der Vegt                         }
  24. {---------------------------------------------------------}
  25. {  Datum .tijd  Revisie                                   }
  26. {  930914.2200  Creatie.                                  }
  27. {  930915.2200  Support for Settextbuffer. Bufptr used    }
  28. {               again for addressing & pointer advancing  }
  29. {               adjusted.                                 }
  30. {---------------------------------------------------------}
  31. {  Usage : Convert textfile to obj with turbo's BINOBJ    }
  32. {          Add them to a unit as show in this sample      }
  33. {          Create a custom filename to func address       }
  34. {          converter as show in My_getp. This function    }
  35. {          should return NIL if the requested file isn't  }
  36. {          linked in. Use Obj_assign to get assign the    }
  37. {          filevar. Reset, Read, Readln & Close are       }
  38. {          allowed. If a file isn't found it's searched on}
  39. {          disk. Pathnames are stripped when searching for}
  40. {          linked-in files.                               }
  41. {---------------------------------------------------------}
  42.  
  43. Unit Obj_01;
  44.  
  45. INTERFACE
  46.  
  47. Type
  48.   Obj_find = Function(fn : String) : Pointer;
  49.  
  50. Var
  51.   Obj_getp : Obj_find;
  52.  
  53. Procedure Obj_Assign(VAR tpl : Text;fn : String;decoder : Obj_find);
  54.  
  55. IMPLEMENTATION
  56.  
  57. Uses
  58.   Dos;
  59.  
  60. {---------------------------------------------------------}
  61. {----To simplyfy addressing inside the buffer, the segment}
  62. {    of the pointer to the text in memmory is incremented }
  63. {    instead of using the old Longint typecast trick      }
  64. {---------------------------------------------------------}
  65.  
  66. Const
  67.   para = 16;
  68.  
  69. Type
  70.   obj_user    = Record
  71.                   base,
  72.                   curr    : Pointer;
  73.                   dummy   : ARRAY[1..8] OF Byte;
  74.                 End;
  75.  
  76. {---------------------------------------------------------}
  77. {----Ignore    handler                                    }
  78. {---------------------------------------------------------}
  79. {$F+}
  80. Function Obj_ignore(VAR f : textrec) : Integer;
  81.  
  82. Begin
  83.   Obj_ignore:=0;
  84. End; {of Obj_ignore}
  85. {$F-}
  86.  
  87. {---------------------------------------------------------}
  88. {----Inoutfunc handler                                    }
  89. {---------------------------------------------------------}
  90. {$F+}
  91. FUNCTION Obj_input(VAR f : textrec) : INTEGER;
  92.  
  93. VAR
  94.   p : Pointer;
  95.  
  96. BEGIN
  97.   WITH Textrec(f) DO
  98.     BEGIN
  99.     {----Advance Pointer obj_size paragraphs}
  100.       p:=Ptr(Seg(obj_user(userdata).curr^)+(bufsize DIV para),
  101.              Ofs(obj_user(userdata).curr^));
  102.       obj_user(userdata).curr:=p;
  103.       Move(obj_user(userdata).curr^,bufptr^,(bufsize DIV para)*para);
  104.       bufpos   :=0;
  105.       bufend   :=(bufsize DIV para)*para;
  106.     END;
  107.   obj_input:=0;
  108. END; {of obj_input}
  109. {$F-}
  110. {---------------------------------------------------------}
  111. {----Open func handler                                    }
  112. {---------------------------------------------------------}
  113. {$F+}
  114. FUNCTION obj_open(VAR f : textrec) : INTEGER;
  115.  
  116. BEGIN
  117.   WITH Textrec(f) DO
  118.     BEGIN
  119.       obj_user(userdata).curr:=obj_user(userdata).base;
  120.       Move(obj_user(userdata).base^,bufptr^,(bufsize DIV para)*para);
  121.       bufpos   :=0;
  122.       bufend   :=(bufsize DIV para)*para;
  123.     END;
  124.   obj_open:=0;
  125. END; {of obj_open}
  126. {$F-}
  127. {---------------------------------------------------------}
  128. {----Assign a link-in file or disk file                   }
  129. {---------------------------------------------------------}
  130.  
  131. Procedure Obj_Assign(VAR tpl : Text;fn : String;decoder : Obj_find);
  132.  
  133. VAR
  134.   tplp    : POINTER;
  135.   i       : Byte;
  136.  
  137. BEGIN
  138.  
  139.   If (Addr(decoder)=NIL)
  140.     THEN tplp:=NIL
  141.     ELSE tplp:=Decoder(fn);
  142.  
  143.   IF (tplp<>NIL)
  144.     THEN
  145.       WITH Textrec(tpl) DO
  146.         BEGIN
  147.           handle   :=$ffff;
  148.           mode     :=fmclosed; {fminput}
  149.           bufsize  :=SIZEOF(textbuf);
  150.           bufpos   :=0;
  151.           bufptr   :=@buffer;
  152.  
  153.           obj_user(userdata).base:=tplp;
  154.           obj_user(userdata).curr:=tplp;
  155.  
  156.           openfunc :=@obj_open;
  157.           inoutfunc:=@obj_input;
  158.           flushfunc:=@obj_ignore;
  159.           closefunc:=@obj_ignore;
  160.  
  161.           i:=0;
  162.           While (i<Length(fn)) AND (i<Sizeof(name)) DO
  163.             Begin
  164.               name[i]:=Upcase(fn[i+1]);
  165.               Inc(i);
  166.             End;
  167.           name[i]  :=#00;
  168.         END
  169.       ELSE Assign(tpl,Fexpand(fn));
  170. END; {of obj_open}
  171.  
  172. END.
  173.  
  174.  
  175. ---------------<source part II, to link in your text files.
  176.  
  177. {---------------------------------------------------------}
  178. {  Project : Object linked textfiles                      }
  179. {  Unit    : Sample program                               }
  180. {  By      : Ir.G.W. van der Vegt                         }
  181. {---------------------------------------------------------}
  182. {  Datum .tijd  Revisie                                   }
  183. {  930914.2200  Creatie.                                  }
  184. {---------------------------------------------------------}
  185.  
  186. Unit Objtext;
  187.  
  188. Interface
  189.  
  190. Procedure Assign_text(VAR tpl : Text;fn : String);
  191.  
  192. Implementation
  193.  
  194. {---------------------------------------------------------}
  195.  
  196. Uses
  197.   Dos,
  198.   Obj_01;
  199.  
  200. {---------------------------------------------------------}
  201. {----SAMPLE Get_obj Function}
  202. {$L SAMPLE_d.obj}
  203. {$L SAMPLE_m.obj}
  204.  
  205. {---------------------------------------------------------}
  206.  
  207. FUNCTION SAMPLE_D  : Byte ; External;
  208. FUNCTION SAMPLE_M  : Byte ; External;
  209.  
  210. {---------------------------------------------------------}
  211. {$F+}
  212. FUNCTION My_getp(fn : String) : Pointer;
  213.  
  214. VAR
  215.   name : String[12];
  216.   d    : dirstr;
  217.   n    : namestr;
  218.   e    : extstr;
  219.  
  220. Begin
  221.   Fsplit(Fexpand(fn),d,n,e);
  222.  
  223.   My_getp:=NIL;
  224.  
  225.   name:=Strip(Upcasestr(n+e),true,true);
  226.  
  227.           {12345678.123}
  228.   IF name=  'SAMPLE.D' THEN My_getp:=  @Sample_d;
  229.   IF name=  'SAMPLE.M' THEN My_getp:=  @Sample_m;
  230. End; {of My_getp}
  231.  
  232. {---------------------------------------------------------}
  233.  
  234. Procedure Assign_text(VAR tpl : Text;fn : String);
  235.  
  236. Begin
  237.   Obj_assign(tpl,fn,Obj_find(Assign_decoder));
  238. End;
  239.  
  240. {---------------------------------------------------------}
  241.  
  242.  
  243. {---------------------------------------------------------}
  244.  
  245. Begin
  246.   Assign_decoder:=@My_getp;
  247. End.
  248.