sgftoken.hpp File Reference

Procedures for reading tokens and delimiters from an input INI-file. More...

#include <stdlib.h>
#include "sgconf.h"

Include dependency graph for sgftoken.hpp:

This graph shows which files directly or indirectly include this file:


Classes

class  t_str_chunk_reader
 Class for reading string chunks. More...
class  t_dchr_chunk_reader
 Class for reading quoted strings and storing them in the dynamically allocated memory. More...

Defines

#define MAX_TOKEN   31
#define NEW_LINE_CHAR   '\n'
#define COMMENT_CHAR   '#'

Functions

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.
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.
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

Detailed Description

Procedures for reading tokens and delimiters from an input INI-file.

The principle of processing the input file in this project is the following. First the input file must be opened using the function 'open_input' from this module. Then the function 'read_token' allows to read a current token from the file, function 'check_token' compares this token with a given one, function 'read_string' reads all characters until a given character is encountered. At the end the input file should be closed using 'close_input'. To read a string by chunks use 'read_by_chunks'. Remark: When reading quoted strings using 'read_string' or 'read_by_chunks' the character '\' is always skipped and the following character is never treated as the quotation. The combinations \ \ (two back slashes) are then translated to '\' (one back slash). Use 'fputs_quoted' to save strings in this format.


Define Documentation

#define COMMENT_CHAR   '#'

#define MAX_TOKEN   31

#define NEW_LINE_CHAR   '\n'


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.

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

unsigned long input_line_number

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.

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.


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