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
|
///////////////////////////////////////////////////////////////////////////////
// //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
// //
// ModelBlocks is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// ModelBlocks is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with ModelBlocks. If not, see <http://www.gnu.org/licenses/>. //
// //
// ModelBlocks developers designate this particular file as subject to //
// the "Moses" exception as provided by ModelBlocks developers in //
// the LICENSE file that accompanies this code. //
// //
///////////////////////////////////////////////////////////////////////////////
#ifndef _NL_MODEL_FILE__
#define _NL_MODEL_FILE__
#include "nl-string.h"
#include "nl-iomacros.h"
#include <netinet/in.h>
static bool OUTPUT_QUIET = false;
///////////////////////////////////////////////////////////////////////////////
void processModelFilePtr ( FILE* pf, bool rF(Array<char*>&) ) {
int i=0; int numFields=0; int c=' '; int line=1;
CONSUME_ALL(pf,c,WHITESPACE(c),line); // Get to first record
while ( c!=EOF ) { // For each record
if ( c=='#' ) CONSUME_ALL(pf, c, c!='\n' && c!='\0', line ) ; // If comment, consume
else { // If no comment,
Array<char*> aps(100);
String psBuff(1000);
CONSUME_STR ( pf, c, (c!='\n' && c!='\0'), psBuff, i, line );
char* psT=NULL;
for(int i=0;true;i++) {
char* z = strtok_r ( (0==i)?psBuff.c_array():NULL, " :=", &psT );
if (!z) break;
aps[i]=z;
}
if ( !rF(aps) ) // Try to process fields, else complain
fprintf( stderr, "\nERROR: %d %d-arg %s in line %d\n\n", numFields, aps.size(), aps[0], line);
}
CONSUME_ALL(pf,c,WHITESPACE(c),line); // Consume whitespace
}
}
///////////////////////////////////////////////////////////////////////////////
void processModelFile ( const char* ps, bool rF(Array<char*>&) ) {
FILE* pf;
if(!OUTPUT_QUIET) fprintf ( stderr, "Reading model file %s...\n", ps ) ;
if ( NULL == (pf=fopen(ps,"r")) ) // Complain if file not found
fprintf ( stderr, "\nERROR: file %s could not be opened.\n\n", ps ) ;
processModelFilePtr ( pf, rF );
fclose(pf);
if(!OUTPUT_QUIET) fprintf ( stderr, "Model file %s loaded.\n", ps ) ;
}
///////////////////////////////////////////////////////////////////////////////
void processModelSocket ( const int tSockfd, int& c, bool rF(Array<char*>&) ) {
int i=0; int numFields=0; int line=1;
CONSUME_ALL_SOCKET(tSockfd,c,WHITESPACE(c),line); // Get to first record
while ( c!='\0' && c!='\5' ) { // For each record
if ( c=='#' ) CONSUME_ALL_SOCKET(tSockfd, c, (c!='\n' && c!='\0' && c!='\5'), line ) ; // If comment, consume
else { // If no comment,
Array<char*> aps(100);
String psBuff(1000);
CONSUME_STR_SOCKET ( tSockfd, c, (c!='\n' && c!='\0' && c!='\5'), psBuff, i, line );
////cerr<<"|"<<psBuff.c_array()<<"|"<<endl;
char* psT=NULL;
for(int i=0;true;i++) {
char* z = strtok_r ( (0==i)?psBuff.c_array():NULL, " :=", &psT );
if (!z) break;
aps[i]=z;
}
if ( !rF(aps) ) // Try to process fields, else complain
fprintf( stderr, "\nERROR: %d-arg %s in line %d\n\n", numFields, aps[0], line);
}
CONSUME_ALL_SOCKET(tSockfd,c,WHITESPACE(c),line); // Consume whitespace
}
}
void processModelSocket ( const int tSockfd, bool rF(Array<char*>&) ) {
int c=' ';
processModelSocket ( tSockfd, c, rF );
}
///////////////////////////////////////////////////////////////////////////////
/*
void processModelString ( String& sBuff, bool rF(Array<char*>&) ) {
if ('#'!=sBuff[0]) {
Array<char*> aps(100);
char* psT=NULL;
for(int i=0;true;i++) {
char* z = strtok_r ( (0==i)?sBuff.c_array():NULL, " :=", &psT );
if (!z) break;
aps[i]=z;
}
if ( !rF(aps) ) // Try to process fields, else complain
fprintf( stderr, "\nERROR: %d-arg %s in line %d\n\n", numFields, aps[0], line);
}
}
*/
///////////////////////////////////////////////////////////////////////////////
#endif //_NL_MODEL_FILE__
|