home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / src / guide / csv-buff.e < prev    next >
Text File  |  1977-12-31  |  2KB  |  69 lines

  1. PROC main()
  2.   DEF buffer, filehandle, len, filename
  3.   filename:='datafile'
  4.   /* Get the length of data in the file */
  5.   IF 0<(len:=FileLength(filename))
  6.     /* Allocate just enough room for the data + a terminating NIL */
  7.     IF buffer:=New(len+1)
  8.       IF filehandle:=Open(filename, OLDFILE)
  9.         /* Read whole file, checking amount read */
  10.         IF len=Read(filehandle, buffer, len)
  11.           /* Terminate buffer with a NIL just in case... */
  12.           buffer[len]:=NIL
  13.           process_buffer(buffer, len)
  14.         ELSE
  15.           WriteF('Error: File reading error\n')
  16.         ENDIF
  17.         /* If Open() succeeded then we must Close() the file */
  18.         Close(filehandle)
  19.       ELSE
  20.         WriteF('Error: Failed to open "\s"\n', filename)
  21.       ENDIF
  22.       /* Deallocate buffer (not really necessary in this example) */
  23.       Dispose(buffer)
  24.     ELSE
  25.       WriteF('Error: Insufficient memory to load file\n')
  26.     ENDIF
  27.   ELSE
  28.     WriteF('Error: "\s" is an empty file\n', filename)
  29.   ENDIF
  30. ENDPROC
  31.  
  32. /* buffer is like a normal string since it's NIL-terminated */
  33. PROC process_buffer(buffer, len)
  34.   DEF start=0, end
  35.   REPEAT
  36.     /* Find the index of a linefeed after the start index */
  37.     end:=InStr(buffer, '\n', start)
  38.     /* If a linefeed was found then terminate with a NIL */
  39.     IF end<>-1 THEN buffer[end]:=NIL
  40.     process_record(buffer+start)
  41.     start:=end+1
  42.   /* We've finished if at the end or no more linefeeds */
  43.   UNTIL (start>=len) OR (end=-1)
  44. ENDPROC
  45.  
  46. PROC process_record(line)
  47.   DEF i=1, start=0, end, s
  48.   /* Show the whole line being processed */
  49.   WriteF('Processing record: "\s"\n', line)
  50.   REPEAT
  51.     /* Find the index of a comma after the start index */
  52.     end:=InStr(line, ',', start)
  53.     /* If a comma was found then terminate with a NIL */
  54.     IF end<>-1 THEN line[end]:=NIL
  55.     /* Point to the start of the field */
  56.     s:=line+start
  57.     IF s[]
  58.       /* At this point we could do something useful... */
  59.       WriteF('\t\d) "\s"\n', i, s)
  60.     ELSE
  61.       WriteF('\t\d) Empty Field\n', i)
  62.     ENDIF
  63.     /* The new start is after the end we found */
  64.     start:=end+1
  65.     INC i
  66.   /* Once a comma is not found we've finished */
  67.   UNTIL end=-1
  68. ENDPROC
  69.