home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / OUT18.ZIP / OUTFILES.INC < prev    next >
Encoding:
Text File  |  1986-11-24  |  12.0 KB  |  461 lines

  1.  
  2. (*
  3.  * outline - a simple "outline" oriented document generator
  4.  *
  5.  * outfiles.inc - this module contains all of the file access
  6.  *                procedures.  Saves and Loads are
  7.  *                handled by the procedures in this module.
  8.  *
  9.  * Author:  Samuel H. Smith, 11-Jan-86
  10.  *
  11.  *)
  12.  
  13.  
  14. procedure select_file(pattern:    anystring;
  15.                       prompt:     linestring;
  16.                       var name:   anystring;
  17.                       var key:    char);     {select a filename for reading
  18.                                               or writing; this may give a
  19.                                               directory of files in a future
  20.                                               version}
  21. var
  22.    i:    integer;
  23.    x, y: integer;
  24.    n:    integer;
  25.    sel:  anystring;
  26.    dir:  anystring;
  27.  
  28. begin
  29.    clrscr;
  30.    getdir(0,dir);
  31.    getfiles(pattern, filetable, filecount);    {get the files matching
  32.                                                 the pattern}
  33.    clrscr;
  34.    lowvideo;
  35.    gotoxy(1, 1);
  36.    displn('List of files matching '+ pattern+ ' in directory '+dir);
  37.  
  38.    normvideo;
  39.    x := 4;
  40.    y := 2;
  41.    for i := 1 to filecount do                  {display the filenames in
  42.                                                 up to 5 columns on screen}
  43.    begin
  44.       gotoxy(x, y);
  45.       disp(filetable[i]);
  46.  
  47.       y := y + 1;
  48.       if y > 15 then
  49.       begin
  50.          y := 2;
  51.          x := x + 16;
  52.       end;
  53.    end;
  54.  
  55.  
  56.    gotoxy(1, 18);
  57.    normvideo;
  58.    displn(prompt+' ──────────────');  {display the prompt and give a little
  59.                                        help with keys in this function}
  60.    gotoxy(1, 25);
  61.    lowvideo;
  62.    disp('Enter a filename,  UP or DOWN to select from the directory,  ESC to cancel');
  63.    gotoxy(1, 20);
  64.    disp('File name: ');
  65.    n := 0;
  66.  
  67.    normvideo;
  68.    repeat
  69.       if n = 0 then
  70.          sel := name
  71.       else
  72.          sel := filetable[n];
  73.  
  74.       gotoxy(12, 20);
  75.       disp(sel);
  76.       i := length(sel) + 1;
  77.       edit_string(12, 20, i, sel, key, 64);   {allow user to edit a filename
  78.                                                or to press a function key}
  79.  
  80.       case key of
  81.  
  82.          UP:   begin
  83.                   n := n - 1;
  84.                   if n < 0 then
  85.                      n := filecount;
  86.                end;
  87.  
  88.  
  89.          DOWN: begin
  90.                   n := n + 1;
  91.                   if n > filecount then
  92.                      n := 0;
  93.                end;
  94.  
  95.  
  96.          NEWLINE, 
  97.          ESC:  ;
  98.  
  99.  
  100.          else  write(^G);
  101.      end;
  102.  
  103.    until (key in [NEWLINE, ESC]);
  104.  
  105.  
  106.    name := sel;
  107.    for i := 1 to length(name) do
  108.       name[i] := upcase(name[i]);      {map the filename to all upper case}
  109.  
  110.    gotoxy(1, 25);
  111.    clreol;
  112.    gotoxy(1, 21);                      {remove extra prompts from screen so
  113.                                         that the caller can print some
  114.                                         more, if needed}
  115. end;
  116.  
  117.  
  118.  
  119. function file_exists(name:  anystring): boolean;      {check to see if a file
  120.                                                        already exists.  knows
  121.                                                        about special devices
  122.                                                        and won't complain
  123.                                                        about them}
  124. var
  125.    fd:  textfile;
  126.    i:   integer;
  127.    dev: anystring;
  128.  
  129. begin
  130.  
  131.    dev := copy(name, 1, 4);
  132.    if dev[4] in [':', '.'] then
  133.       dev := copy(dev, 1, 3);                {get the 3 letter device name
  134.                                               from the filename, if any was
  135.                                               present}
  136.  
  137.    file_exists := false;
  138.    if dev = 'LST' then exit;
  139.    if dev = 'AUX' then exit;
  140.    if dev = 'NUL' then exit;
  141.    if dev = 'CON' then exit;
  142.    if dev = 'PRN' then exit;                 {don't bother checking on these
  143.                                               files; they are special}
  144.  
  145.  
  146.    assign(fd, name);                {it looks like an ordinary disk file;
  147.                                      try to open it to see if it is there}
  148. {$I-}
  149.    reset(fd);
  150. {$I+}
  151.    if ioresult = 0 then
  152.    begin
  153.       file_exists := true;
  154.       close(fd);
  155.    end
  156.    else
  157.       file_exists := false;
  158. end;
  159.  
  160.  
  161. procedure save_section(var sec: section_ptr;
  162.                        var fd:  textfile);       {save a section in special
  163.                                                   .OLF format; recursively
  164.                                                   calls itself for sub-
  165.                                                   sections}
  166. var
  167.    i:           integer;
  168.    textcount:   integer;
  169.    subscount:   integer;
  170.  
  171. begin
  172.    with sec^ do
  173.    begin
  174.       writeln(fd, title);                   {save the title}
  175.  
  176.       textcount := 0;
  177.       for i := 1 to max_text do
  178.          if text^[i] <> '' then
  179.             textcount := textcount + 1;     {count the lines of text}
  180.  
  181.       writeln(fd, textcount, ' ',estimate:7:2, ' ', onpage);
  182.       for i := 1 to max_text do
  183.          if text^[i] <> '' then
  184.             writeln(fd, text^[i]);           {save the text lines}
  185.  
  186.  
  187.       subscount := 0;
  188.       for i := 1 to max_subsects do
  189.          if subsect[i] <> nil then
  190.             if subsect[i]^.title <> '' then
  191.                subscount := subscount + 1;     {count the subsections}
  192.  
  193.       writeln(fd, subscount);
  194.       for i := 1 to max_subsects do
  195.          if subsect[i] <> nil then
  196.             if subsect[i]^.title <> '' then
  197.                save_section(subsect[i], fd);   {save all the subsections}
  198.    end;
  199. end;
  200.  
  201.  
  202. procedure save_document;       {user interface for saving the current
  203.                                 outline to a file so that it can be
  204.                                 loaded at a later time}
  205. var
  206.    fd:      textfile;
  207.    key:     char;
  208.  
  209. begin
  210.  
  211.    select_file('*.OLF', 'Save outline to a file', docfile, key);
  212.  
  213.    if (key <> NEWLINE) or (docfile = '') then
  214.       exit;
  215.  
  216.    if pos('.', docfile) = 0 then
  217.       docfile := docfile + '.OLF';
  218.  
  219.    if file_exists(docfile) then
  220.    begin
  221.       writeln;
  222.       displn('WARNING:  The file '+ docfile+ ' already exists!  ');
  223.       disp('Overwrite it? (Y/N) ');
  224.  
  225.       if upcase(getkey) <> 'Y' then
  226.          exit;
  227.  
  228.       displn('Yes');
  229.    end;
  230.  
  231.    assign(fd, docfile);
  232.    rewrite(fd);
  233.  
  234.    writeln(fd,'OUTLINE OLF 3');
  235.    save_section(document, fd);
  236.    close(fd);
  237.  
  238.    saved := true;
  239. end;
  240.  
  241.  
  242.  
  243.  
  244. function load_section(var fd:  textfile):  section_ptr;
  245.                                               {loads a single section
  246.                                                of an outline from
  247.                                                an .OLF format file.
  248.                                                returns section
  249.                                                pointer for the loaded
  250.                                                section.  recursively
  251.                                                calls itself for
  252.                                                loading subsections}
  253. var
  254.    i:       integer;
  255.    count:   integer;
  256.    sec:     section_ptr;
  257.  
  258. begin
  259.    if eof(fd) then
  260.    begin
  261.       load_section := nil;
  262.       displn('ERROR:  Unexpected end of file!!!'^G^G^G);
  263.       exit;
  264.    end;
  265.  
  266.    sec := new_section;             {create the section to load}
  267.  
  268.    with sec^ do
  269.    begin
  270.       readln(fd, title);           {get the section title}
  271.  
  272.       case olf_format of
  273.          2: readln(fd, count, estimate);
  274.          3: readln(fd, count, estimate, onpage);
  275.          else
  276.             readln(fd, count);
  277.       end;
  278.  
  279.       if count <> 0 then
  280.       begin
  281.          allocate_text(sec);
  282.          for i := 1 to count do
  283.             readln(fd, text^[i]);      {get all text lines for this section}
  284.       end;
  285.  
  286.       readln(fd, count);
  287.       for i := 1 to count do
  288.          subsect[i] := load_section(fd);
  289.                                    {recursively load all subsections for
  290.                                     this section}
  291.    end;
  292.  
  293.    load_section := sec;            {return pointer to the loaded section}
  294. end;
  295.  
  296.  
  297. procedure save_if_needed;
  298. var
  299.    key: char;
  300.  
  301. begin
  302.    if not saved then
  303.    begin
  304.       clrscr;
  305.       gotoxy(1,15);
  306.       displn('WARNING:  There are unsaved changes in the current outline.'^G);
  307.       writeln;
  308.       disp  ('          Do you want to save? (Y/N) ');
  309.  
  310.       repeat
  311.          key := upcase(getkey);
  312.       until key in ['Y','N'];
  313.  
  314.       if key = 'Y' then
  315.          save_document;
  316.    end;
  317.  
  318.    clrscr;
  319. end;
  320.  
  321.  
  322. procedure load_document;      {user interface to load an outline from
  323.                                a .OLF format file}
  324. var
  325.    fd:      textfile;
  326.    key:     char;
  327.  
  328. begin
  329.  
  330.    save_if_needed;
  331.    select_file('*.OLF', 'Load outline from a file', docfile, key);
  332.  
  333.    if (key <> NEWLINE) or (docfile = '') then
  334.       exit;
  335.  
  336.    if pos('.', docfile) = 0 then
  337.       docfile := docfile + '.OLF';
  338.  
  339.  
  340.    docfile := locate_file(docfile);
  341.    if not file_exists(docfile) then
  342.    begin
  343.       displn('I can''t find '+ docfile+ ';  try another filename.'^G);
  344.       delay(3000);
  345.       exit;
  346.    end;
  347.  
  348.  
  349.    assign(fd, docfile);
  350.    reset(fd);
  351.  
  352.    readln(fd,lineout);            {determine outline file format.  some
  353.                                    old formats need different handling}
  354.  
  355.    if lineout = 'OUTLINE OLF 2' then
  356.       olf_format := 2
  357.    else
  358.    if lineout = 'OUTLINE OLF 3' then
  359.       olf_format := 3
  360.    else
  361.    begin
  362.       olf_format := 1;
  363.       close(fd);
  364.       reset(fd);
  365.    end;
  366.  
  367.    marksec := nil;
  368.    marksub := 0;                   {remove any markers that are
  369.                                     set when we go to a new section
  370.  
  371.    delete_section(document);       {delete the current document from memory}
  372.  
  373.    document := load_section(fd);   {load in a new document.  note that this
  374.                                     could be used to load in a subsection
  375.                                     if there was a user interface for it.
  376.                                     that would then allow you to "merge"
  377.                                     outlines}
  378.  
  379.    close(fd);
  380.    saved := true;
  381. end;
  382.  
  383.  
  384.  
  385. procedure load_options;           {load in the options file and set the
  386.                                    various program paremeters accordingly}
  387. var
  388.    fd:     text;
  389.    par:    integer;
  390.    name:   anystring;
  391. begin
  392.  
  393.    name := locate_file('outline.opt');
  394.    if not file_exists(name) then
  395.    begin
  396.       displn('Can''t open option file: '+name);
  397.       halt;
  398.    end;
  399.  
  400.    assign(fd,name);
  401.    reset(fd);
  402.  
  403.    readln(fd);                  {skip initial comment line}
  404.  
  405.    readln(fd,pagelen);          {max number of lines to print on a page}
  406.  
  407.    readln(fd,minlines);         {minimum number of lines on a page before
  408.                                  a new page can be started}
  409.  
  410.    readln(fd,indentation);      {amount of indentation for each level of
  411.                                  subsection nesting in printouts}
  412.  
  413.    readln(fd,right_margin);     {right margin for reformatted print file
  414.                                  outputs}
  415.  
  416.    readln(fd,par);              {should titles be underlined?}
  417.    if par = 1 then
  418.       underline_titles := true
  419.    else
  420.       underline_titles := false;
  421.  
  422.  
  423.    readln(fd,par);
  424.    if par = 1 then
  425.       paragraph_reformat := true
  426.    else
  427.       paragraph_reformat := false;
  428.                                 {should paragraphs of text be reformatted?}
  429.  
  430.    readln(fd,par);
  431.    if par = 1 then
  432.       break_into_pages := true
  433.    else
  434.       break_into_pages := false;
  435.                                 {should output be divided into pages?}
  436.  
  437.    readln(fd,par);
  438.    textmode(par);               {set text modes}
  439.  
  440.    readln(fd,par);
  441.    if par = 1 then
  442.       section_numbering := true
  443.    else
  444.       section_numbering := false;
  445.                                 {should sections be numbered?}
  446.  
  447.    readln(fd,par);
  448.    if par = 1 then
  449.       justify := true
  450.    else
  451.       justify := false;
  452.                                 {should text be right justified?}
  453.  
  454. {$I-}
  455.    close(fd);
  456. {$I+}
  457. end;
  458.  
  459.  
  460.  
  461.