From: bpb9204@tamsun.tamu.edu (Brent)
Subject: Re: Parsing text files
Organization: Texas A&M Univ., Inc.
Date: Wed, 30 Sep 1992 14:50:12 GMT

u2005681@ucsvc.ucs.unimelb.edu.au writes:
|
|As part of a new application, I am trying to read a text file of numbers
|and strings into a series of variables. The idea is to use the numbers as
|co-efficients for a simulation and the strings as labels for various graphs.

|As I understand it, the Mac OS deals with text files as UNIX-C does with
|binary files ie you specify how many bytes to read and what buffer to read
|them into. But in the absence of a standard library, how is it possible to 
|read numbers and strings of differing lengths?

What do you mean, "absence of a standard library?"  What are you using?

|An explanation (or even some source code in C) would be most excellent.

I don't know the format of what you want to read in, so you'll have to
massage the following into your own form.

The idea is to use your own buffering.  You do this by either using an
array or allocating (with NewPtr() or NewHandle() ) an appropriately-sized
buffer.  The size of the buffer is important - if you make it a multiple
of the disk block size, your reads will be much faster.  A good buffer
size is 1024 or 2048 bytes.  (Is a mac disk block 512 bytes? or 1024?)

What you'll do is pass bits of your buffer to string conversion utilities
like Str2Num.  This will convert the string representation of a number
into an Extended type.  When you are reading a string, just copy characters
from the buffer to your string array directly.  
  Note: Str2Num takes a "DecStr" type, which is effectively an array
  of 255 characters.  That's how long your number representation can be.  

When your buffer position is equal to the length of the buffer, it's time
to read in another block of data.

This idea isn't difficult to implement and can be easily done in an
afternoon, for sure.  Some C books mention buffering, if you need a reference.

I decided to go ahead and write some simple stuff.  Here is my 10 minute
hack:
-------  pseudo-C follows

char buffer[1024];
int bufpos;
int endoffile=0;

LoadBuffer()
{
   OSErr err;
   long count = 1024;

/* inputRefNum is reference number of a pre-opened input file **/
   err = FSRead( inputRefNum, &count, buffer);  /* check error result */
   if (count == 0)
     endoffile = 1;
   bufpos = 0;
}

ScanNumber(Extended *e, )
{
   *e = Str2Num( &buffer[bufpos]);
   /*** update buffer position ***/
   while( buffer[bufpos] is in {0-9eE+-} )
     bufpos++;

/* Note:  this routine does not take into account the situation like:
   early data in file...... 10.23e49  ....... later data in file
   buffer read n ---------------||----------- buffer read n+1 ...
*/
}

ScanString( char *s, int maxlen)
{
   while (maxlen--)
   {
     if (bufpos == 1024)  LoadBuffer();
     *s++ = buffer[bufpos++];
     if (buffer[bufpos] is some character that does not belong in a string)
       return;
   }
}

ParseFile()
{
  while (!endoffile)
  {
    /* this will be a series of calls to ScanNumber() and ScanString()
    ** that you'll have to figger out.
    */
  }
}
-- 
------------------------------------------------------------------------------
Brent P. Burton, N5VMG                          Department of Computer Science
bpb9204@tamsun.tamu.edu                                   Texas A&M University
      What are typical family values?      Good buys at WalMart.