summaryrefslogtreecommitdiff
path: root/apps/Gateway/Gateway/File_Parser.cpp
blob: be33e9a96d205b739a425ee8103d148a7d751089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#if !defined (FILE_PARSER_C)
// $Id$

#define FILE_PARSER_C

#include "ace/OS.h"
#include "File_Parser.h"

// This fixes a nasty bug with cfront-based compilers (like
// Centerline).
typedef FP::Return_Type FP_RETURN_TYPE;

// File_Parser stuff.

template <class ENTRY> int
File_Parser<ENTRY>::open (const char filename[]) 
{
  return (this->infile_ = ACE_OS::fopen (filename, "r")) == 0 ? -1 : 0;
}

template <class ENTRY> int
File_Parser<ENTRY>::close (void)
{ 
  return ACE_OS::fclose (this->infile_); 
}

template <class ENTRY> FP_RETURN_TYPE
File_Parser<ENTRY>::getword (char buf[])
{
  FP_RETURN_TYPE read_result = this->readword(buf);
  if (read_result == FP::SUCCESS)
    return FP::SUCCESS;
  else
    return read_result;
}

// Get the next string from the file via this->readword()
// Check make sure the string forms a valid number.
template <class ENTRY> FP_RETURN_TYPE
File_Parser<ENTRY>::getint (int &value)
{
  char buf[BUFSIZ];
  FP_RETURN_TYPE read_result = this->readword(buf);
  if (read_result == FP::SUCCESS)
    {
      // ptr is used for error checking with ACE_OS::strtol
      char *ptr;

      // try to convert the buf to a decimal number
      value = ACE_OS::strtol (buf, &ptr, 10);

      // check if the buf is a decimal or not
      if ((value == 0) && (ptr == buf))
	return FP::ERROR;
      else
	return FP::SUCCESS;
    }
  else
    return read_result;
}
  

template <class ENTRY> FP_RETURN_TYPE
File_Parser<ENTRY>::readword (char buf[])
{
  int wordlength = 0;
  int c;

  // Skip over leading delimiters and get word.

  while ((c = getc (this->infile_)) != EOF && c != '\n')
    if (this->delimiter (c))
      {
	// We've reached the end of a "word".
	if (wordlength > 0)
	  break;
      }
    else
      buf[wordlength++] = c;

  buf[wordlength] = '\0';

  if (c == EOF) {
    // If the EOF is just a dilimeter, don't return EOF so that the
    // word gets processed
    if (wordlength > 0) 
      {
	ungetc (c, this->infile_);
	return FP::SUCCESS;
      }
    else
      // else return EOF so that read loops stop
      return FP::EOFILE;
  }
  else if (c == '\n')
    {
      // if the EOLINE is just a dilimeter, don't return EOLINE
      // so that the word gets processed
      if (wordlength > 0)
	ungetc (c, this->infile_);
      else
	return FP::EOLINE; 
    }

  // Skip comments.
  if (this->comments (buf[0])) 
    {
      if (this->skipline () == EOF)
	return FP::EOFILE;
      else
	return FP::COMMENT;
    }
  else
    return FP::SUCCESS;
}

template <class ENTRY> int
File_Parser<ENTRY>::delimiter (char ch)
{
  return ch == ' ' || ch == ',' || ch == '\t';
}

template <class ENTRY> int
File_Parser<ENTRY>::comments (char ch)
{
  return ch == '#';
}

template <class ENTRY> int
File_Parser<ENTRY>::skipline (void)
{
  // Skip the remainder of the line.

  int c; 

  while ((c = getc (this->infile_)) != '\n' && c != EOF)
    continue;

  return c;
}

#endif /* _FILE_PARSER_C */