sgftoken.cpp File Reference

Procedures for reading tokens and delimiters from an input INI-file. Cf. sgtoken.hpp for details. More...

#include <string.h>
#include <iostream>
#include <fstream>
#include "sgftoken.hpp"

Include dependency graph for sgftoken.cpp:


Functions

void read_token ()
 Reads the current token from the file.
unsigned read_string (char term, char *str, unsigned max_len)
 Reads characters from 'sg_input_f' until 'term' is encountered.
bool read_by_chunks (char term, t_str_chunk_reader &reader)
bool open_input (char *file_name)
 Opens the input file and sets the first entry of input_token_buf to '\0' and last_delim to '\0'.
void close_input ()
 Closes the input stream.
int check_token (char *tok)
 Compares a token given in the argument with the token in input_token_buf.
int check_long (long *num)
 Converts the token in input_token_buf to an integer.
int check_double (double *num)
 Converts the token in input_token_buf to a real number.

Variables

char input_token_buf [MAX_TOKEN+1]
 The global buffer for a current token. This buffer is initialized by 'read_token' and used by 'check_token' or some external user.
unsigned long input_line_number = 1

Detailed Description

Procedures for reading tokens and delimiters from an input INI-file. Cf. sgtoken.hpp for details.


Function Documentation

int check_double ( double *  num  ) 

Converts the token in input_token_buf to a real number.

It returns nonzero if succeded to convert the whole contents of input_token_buf, and 0 otherwise.

int check_long ( long *  num  ) 

Converts the token in input_token_buf to an integer.

It returns nonzero if succeded to convert the whole contents of input_token_buf, and 0 otherwise.

int check_token ( char *  tok  ) 

Compares a token given in the argument with the token in input_token_buf.

It returns nonzero if the tokens are equal and zero otherwise.

void close_input (  ) 

Closes the input stream.

It returns the return value of 'fclose'.

bool open_input ( char *  file_name  ) 

Opens the input file and sets the first entry of input_token_buf to '\0' and last_delim to '\0'.

Read the first token using the function read_token, after that the same values of input_token_buf and last_delim mean EOF (in contrast to the situation before the first run of read_token). This function returns 0 if the operation succeded and nonzero if it failed.

bool read_by_chunks ( char  term,
t_str_chunk_reader reader 
)

The characters are written to 'reader.buffer'. As soon as the buffer is filled up it is flushed using 'reader.get_chunk'. If this function returns nonzero, the further characters up to 'term' or the end of the file are skipped. Unless EOF encounered the terminating character 'term' is written to input_token_buf, else (if EOF encountered) the first entry of input_token_buf is set to zero. last_delim is always set to zero. The function returns zero if 'reader.get_chunk' has returned zero at all the calls and nonzero otherwise.

Note:
'\' is always skipped and the following character is not checked for 'term'. \ \ is translated to '\'.
Bug:
zero or non-zero not appropriate for boolean value.

unsigned read_string ( char  term,
char *  str,
unsigned  max_len 
)

Reads characters from 'sg_input_f' until 'term' is encountered.

The first max. 'max_len' characters are written to 'str', other characters are skipped. Unless EOF encounered the terminating character 'term' is written to input_token_buf, else (if EOF encountered) the first entry of input_token_buf is set to zero. last_delim is always set to zero. The function returns the number of characters written to 'str'.

Note:
'\' is always skipped and the following character is not checked for 'term'. \ \ is translated to '\'.

void read_token (  ) 

Reads the current token from the file.

The token is placed in input_token_buf, the delimiter encountered at its end is written to last_delim. The previous values of these buffers are lost. The function follows the rules below:

  1. if the value of last_delim is in 'independent_delim'
    then this value followed by '\0' is written to
    input_token_buf, and last_delim is set to 0.
  2. else all the white space characters and comments from
    the current position in the input file are skipped
    and then the characters are read to input_token_buf
    until a delimiter or a comment encountered, or MAX_TOKEN
    characters are read, or EOF encountered.
    1. if a delimiter encountered then:
      • if no characters are written to input_token_buf
        this delimiter is written to that buffer
        and last_delim is set to 0 (this delimiter
        cannot be a white space characters because
        all such characters were skipped)
        • else this delimiter is written to last_delim
    2. if a comment encountered, COMMENT_CHAR is written
      to last_delim.
    3. if MAX_TOKEN characters are read then all further
      characters till a delimiter are skipped and the
      functions works as if all the skipped characters
      are written to input_token_buf.
    4. if EOF encountered, it is considered as a delimiter,
      but last_delim is set to 0.

Thus input_token_buf is empty after this function if and only if the EOF is encountered.


Variable Documentation

input_line_number = 1

This global variable stores the current line number, i. e. the number of new-line characters read and transfered to input_token_buf plus one. It is assumed that NL is always a white space character, so it newer appears in input_token_buf. Thus all the characters in input_token_buf belong to one line. The character writter currently to last_delim is not taken into account. The code of the new-line caracter is given by the macro NEW_LINE_CHAR.

input_token_buf

The global buffer for a current token. This buffer is initialized by 'read_token' and used by 'check_token' or some external user.


Generated on Fri Sep 21 12:33:47 2007 for SG2 by  doxygen 1.5.2