| Line 26: |
Line 26: |
| | ==Example Code== | | ==Example Code== |
| | | | |
| − | <tt> | + | <syntaxhighlight lang=objective-c> |
| − | | |
| | - (BOOL)readFile:(NSString *)filename returnError:(NSMutableString *)errorString | | - (BOOL)readFile:(NSString *)filename returnError:(NSMutableString *)errorString |
| | | | |
| | { | | { |
| − | :unsigned long i;
| + | unsigned long i; |
| | | | |
| − | :unsigned long num_lines, num_rows, lines_to_skip;
| + | unsigned long num_lines, num_rows, lines_to_skip; |
| | | | |
| − | :unsigned long row,column;
| + | unsigned long row,column; |
| | | | |
| − | :FILE *fp;
| + | FILE *fp; |
| | | | |
| − | :[errorString setString:@"no error"];
| + | [errorString setString:@"no error"]; |
| | | | |
| − | :fp = fopen([filename cStringUsingEncoding:NSASCIIStringEncoding],"r");
| + | fp = fopen([filename cStringUsingEncoding:NSASCIIStringEncoding],"r"); |
| | | | |
| − | :// set up a line buffer
| + | // set up a line buffer |
| | | | |
| − | :LineBuffer *line = NewLineBuffer(1000);
| + | LineBuffer *line = NewLineBuffer(1000); |
| | | | |
| − | :// how many lines in the file
| + | // how many lines in the file |
| | | | |
| − | :num_lines = CountLinesInFile(line,fp);
| + | num_lines = CountLinesInFile(line,fp); |
| | | | |
| − | :// count lines rewinds the file, so we're back at the beginning..
| + | // count lines rewinds the file, so we're back at the beginning.. |
| | | | |
| − | :lines_to_skip = NUM_HEADER_LINES;
| + | lines_to_skip = NUM_HEADER_LINES; |
| | | | |
| − | :num_rows = num_lines – lines_to_skip;
| + | num_rows = num_lines – lines_to_skip; |
| | | | |
| − | :// skip the header lines, if they exist
| + | // skip the header lines, if they exist |
| | | | |
| − | :for (i=0;i<lines_to_skip;i++) GetNextLine(line,fp);
| + | for (i=0;i<lines_to_skip;i++) GetNextLine(line,fp); |
| | | | |
| − | :// read in all the rows and columns
| + | // read in all the rows and columns |
| | | | |
| − | :column = 0;
| + | column = 0; |
| | | | |
| − | :for(row=0;row<num_rows;row++) {
| + | for(row=0;row<num_rows;row++) { |
| | | | |
| − | ::if (GetNextLine(line,fp)) {
| + | if (GetNextLine(line,fp)) { |
| | | | |
| − | :::while (GetNextTabField(line, tabtext)) {
| + | while (GetNextTabField(line, tabtext)) { |
| | | | |
| − | ::::SetRowColumnText(row,column,tabtext);
| + | SetRowColumnText(row,column,tabtext); |
| | | | |
| − | ::::column++;
| + | column++; |
| | | | |
| − | :::}
| + | } |
| | | | |
| − | ::}
| + | } |
| | | | |
| − | :}
| + | } |
| | | | |
| | | | |
| − | :// all the rows have been read in
| + | // all the rows have been read in |
| | | | |
| − | :// done reading from the file, so dispose of file stuff
| + | // done reading from the file, so dispose of file stuff |
| | | | |
| − | :fclose(fp);
| + | fclose(fp); |
| | | | |
| − | :DisposeLineBuffer(line);
| + | DisposeLineBuffer(line); |
| | | | |
| − | :return(TRUE);
| + | return(TRUE); |
| | | | |
| | } | | } |
| − | </tt> | + | </syntaxhighlight> |
| | | | |
| | ==LineBuffer data structure== | | ==LineBuffer data structure== |
| − | <blockquote>
| |
| − | <tt> typedef struct {
| |
| | | | |
| | + | <syntaxhighlight lang=objective-c> |
| | + | typedef struct { |
| | | | |
| − | :unsigned long max; // maximum size of the line (i.e. size of buffer pointed to by text)
| + | unsigned long max; // maximum size of the line (i.e. size of buffer pointed to by text) |
| | | | |
| − | :char *text; // the buffer that contains the last line read from the file
| + | char *text; // the buffer that contains the last line read from the file |
| | | | |
| − | :char *textptr; // for internal use; a ptr to the start of the next tab field
| + | char *textptr; // for internal use; a ptr to the start of the next tab field |
| | | | |
| − | :char *textend; // for internal use; a pointer to the end of the line
| + | char *textend; // for internal use; a pointer to the end of the line |
| | | | |
| − | :unsigned long length; // the length of the line, ie. the number of characters that were read in from the file
| + | unsigned long length; // the length of the line, ie. the number of characters that were read in from the file |
| | | | |
| − | :char *buffer_ptr; // points to start of next line in a buffer -- initialized by SetBuffer
| + | char *buffer_ptr; // points to start of next line in a buffer -- initialized by SetBuffer |
| | | | |
| − | :unsigned long buffer_size; // the total number of characters in the buffer -- initialized by SetBuffer
| + | unsigned long buffer_size; // the total number of characters in the buffer -- initialized by SetBuffer |
| | | | |
| | + | } LineBuffer; |
| | + | </syntaxhighlight> |
| | | | |
| − | } LineBuffer;
| |
| − | </tt>
| |
| − | </blockquote>
| |
| | | | |
| | ==Routines== | | ==Routines== |