home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / READVAR.ZIP / READVAR.PAS
Encoding:
Pascal/Delphi Source File  |  1987-11-08  |  2.5 KB  |  49 lines

  1. { ************************************************************ }
  2. { This function is very useful if you manipulate a lot of CP/M }
  3. { comma delimited text files.  It takes embedded commas into   }
  4. { consideration by enclosing the substring in quotes (").      }
  5. { EXAMPLE: 'string,Albuquerque, NM,string' would show up in the}
  6. { file as: 'string,"Albuquerque, NM",string'. It takes care of }
  7. { reading the following comma so you are set to read the next  }
  8. { file variable.  If you have fixed length fields mixed with   }
  9. { variable length fields you MUST read the following comma your}
  10. { self. EXAMPLE read(filename,filevar,ch) where filename is the}
  11. { file name, filevar is a file variable of fixed length and ch }
  12. { is of type String[1].                                        }
  13. { The function is called in the following manner:              }
  14. { ******    VariableName:=Readvar(VariableName);       *****   }
  15. { Global declaration :                                         }
  16. {                       type  Str40:       String[40];         }
  17. { Written for Turbo Pascal Ver. 2, but it should work on any   }
  18. { compiler.         Steve Davis    8/21/84                     }
  19.  
  20. Function ReadVar(Var TempVar: Str40):Str40;
  21. Var
  22.    Ch:       Char;
  23. begin
  24.     TempVar:='';                     { Get Rid of any Garbage }
  25.     If not Eoln(FileVar) then        { If Not Eoln Read a char }
  26.     begin
  27.         Read(FileVar,Ch);            { Read a character        }
  28.         If Ch = '"' Then             { Test for First quote    }
  29.         begin
  30.             Read(FileVar,Ch);         { Read Until the Second }
  31.             Repeat                    { Quote is Read in      }
  32.                  TempVar:=TempVar+Ch; { Update TempVar        }
  33.                  Read(FileVar,Ch)     { Read Next Character   }
  34.             Until Ch = '"';           { End Repeat "          }
  35.             TempVar:='"'+TempVar+'"'; { Put the quotes back to write }
  36.                                       { it back to a file.           }
  37.             If Ch='"' then
  38.                Read(FileVar,Ch)       { If " then read the comma }
  39.          end
  40.          Else If Ch <> Comma then       { Else Read Until A Comma }
  41.             Repeat                      { is Encountered          }
  42.                  TempVar:=TempVar+Ch;
  43.                  Read(FileVar,Ch)
  44.             Until (Ch = Comma) or (Eoln(FileVar));
  45.     end;
  46.     ReadVar:=TempVar    { Assign the string. If the next ch was a comma }
  47.                         { TempVar:= ''. ie ,string,,string,             }
  48. end;                      { Function ReadVar }
  49.