File: paramreader.hc

package info (click to toggle)
craft 3.5-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,172 kB
  • ctags: 1,605
  • sloc: cpp: 3,794; makefile: 2,322; ansic: 857; sh: 842
file content (165 lines) | stat: -rw-r--r-- 4,285 bytes parent folder | download | duplicates (4)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*======================================================================*/
/*= CHANGES AND UPDATES                                                =*/
/*======================================================================*/
/*= date   person file            subject                              =*/
/*=--------------------------------------------------------------------=*/
/*=                                                                    =*/
/*= 121192 hua    paramreader.hc  .params postfix for param file name  =*/
/*=                               is extended implicitly               =*/
/*=                                                                    =*/
/*= 011292 hua    paramreader.hc  added check params                   =*/
/*=                                                                    =*/
/*======================================================================*/

#include "paramreader.h"
#include "xmath.h"

/*----------------------------------------------------------------------*/
/* parameter check functionsons)                                        */
/*----------------------------------------------------------------------*/

void check_params (int num_params)
  {if (num_params < 2)
      handle_param_error;

.  handle_param_error
     {system    ("sound /home/hua/sound/laughter");
      errorstop (2, "paramreader", "Oh boy, better play golf");
     }.
  }

/*----------------------------------------------------------------------*/
/* CLASS paramreader (functions)                                        */
/*----------------------------------------------------------------------*/

void paramreader::read_sym (char sym [], bool &is_eof)
  {is_eof = false;
   get_sym;
   perhaps_skip_comment;
   perhaps_eof;
   perhaps_include;

.  perhaps_skip_comment
     while (is_comment)
       skip_comment.

.  skip_comment
     {char tc;

      while ((tc = getc (act_f)) != '\n' && tc != '#')
        {};
      get_sym;
     }.

.  is_comment
     (sym [0] == '#' && strcmp (sym, "#include") != 0).

.  get_sym
     fscanf (act_f, "%s", sym).  

.  act_f
     f [num_includes - 1].

.  perhaps_eof
     if (is_eof_sym)
        pop_f.

.  pop_f
     {num_includes--; 
      fclose (f [num_includes]);
      if  (num_includes == 0)
          is_eof = true;
      else read_sym (sym, is_eof);
     }.

.  is_eof_sym
     (strcmp (name [num_params], "//") == 0).

.  perhaps_include
     if (strcmp (sym, "#include") == 0)
        push_f.

.  push_f
     {get_sym;
      num_includes++;
      f [num_includes-1] = fopen (sym, "r");
      read_sym (sym, is_eof);
     }.
  
  }
   
paramreader::paramreader (char param_file_name [])
  {bool is_eof = false;

   num_params = 0;
   open_param_file;
   perhaps_file_error;
   read_operation;
   while (! is_eof)
     read_one_param;

.  open_param_file   
     {char file_name [128];

      strcpy (file_name, param_file_name);
      strcat (file_name, ".params");
      f [0]        = fopen (file_name, "r");
      num_includes = 1;
     }.

.  perhaps_file_error
     if (f [0] == NULL)
        errorstop (1, "PARAMREADER",param_file_name,"parameter file unknown").

.  read_operation
     {read_sym (name  [num_params], is_eof);
      if (! is_eof)
         read_sym (value [num_params], is_eof);
     }.

.  read_one_param
     {num_params++;
      read_operation;
     }.

  } 

void paramreader::dump ()
  {for (int i = 0; i < num_params; i++)
     printf (">%s< = >%s<\n", name [i], value [i]);
  }

int paramreader::param_no (char p_name [])
  {for (int no = 0; no < num_params; no++)
     if (strcmp (name [no], p_name) == 0)
        return no;
   printf ("paramreader error, symbol '%s' unknown\n", p_name);
   exit   (1);
   return (0);
  }

char * paramreader::s_param (char name [])
  {return value [param_no (name)];
  }

double paramreader::d_param (char name [])
  {return atof (value [param_no (name)]);
  }

int paramreader::i_param (char name [])
  {return atoi (value [param_no (name)]);
  }

int paramreader::max_i_name ()
  {int max = - INT_MAX;

   for (int i = 0; i < num_params; i++)
     max = i_max (max, atoi (name [i]));
   return max;
  }

void paramreader::set (char p_name [], char p_value [])
  {int pno = param_no (p_name);

   strcpy (value [pno], p_value);
  }