C语言解析txt中的参数

C语言解析txt中的参数

// ParseParam.h — See ParseParam.c for example of use
//http://www.gdargaud.net/Hack/SourceCode.html

H文件

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
// ParseParam.h -- See ParseParam.c for example of use
//http://www.gdargaud.net/Hack/SourceCode.html

#ifndef _PARSE_PARAM
#define _PARSE_PARAM

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

extern char* ReadParseParam(const char* FileName, char *VariableName);
extern char *TempPP;

int SeperateIntegerFromArray(char *Array, int *List);
int SeperateFloatFromArray(char *Array, float *List);
int SeperateStringFromArray(char *Array, char **List);

#define ParseParamString(FileName, Str) \
if ((TempPP=ReadParseParam((ParamFileName), #Str))!=NULL) \
strcpy(Str, TempPP); else Str[0]='\0'

#define ParseParamInt(FileName, Int) \
if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
Int=atoi(TempPP); else Int=0

#define ParseParamHex(FileName, Int) \
if ((TempPP=ReadParseParam((ParamFileName), #Int))!=NULL) \
Int=strtol(TempPP, NULL, 16); else Int=0

#define ParseParamFloat(FileName, Flt) \
if ((TempPP=ReadParseParam((ParamFileName), #Flt))!=NULL) \
Flt=atof(TempPP); else Flt=0

#define ParseParamBool(FileName, B) \
if ((TempPP=ReadParseParam((ParamFileName), #B))!=NULL) \
B=(toupper(TempPP[0])=='Y' || toupper(TempPP[0])=='T'|| TempPP[0]=='1'); else B=0


#endif

CPP文件

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/******************************************************************************
MODULE: ParseParam
PURPOSE: Parses a parameter file into variables

EXAMPLE:
ParseParamString("Param.txt", DirSfcBin);
ParseParamInt("Param.txt", MaxLat);
ParseParamFloat("Param.txt", MaxScale);
ParseParamBool("Param.txt", DoLegend);
ParseParamHex("Param.txt", ColorZero);
ParseParamHex("Param.txt", ColorNan);
ParseParamHex("Param.txt", ColorCurve);
will parse the following file:

-------- Start of file "Param.txt"
; This is the parameter file for RainMerge.exe and MergeMonth.exe
; Comments are preceded by ; # or !
; You can use blank lines too

DirSfcBin="/raid/data/SfcRain/" ; Use double quotes around strings
NAN=-9999. ; float, ignored value
MaxLat = 40 ; integer
ColorZero= 0xC0C0C0 ; hex value
ColorNan = FFFFFF ; optional 0x
ColorCuve= 0x101040 ; Syntax error
MaxScale=10. ; leading/trailing spaces are ignored
DoLegend=Y ; Bool can be T, F, Y, N, True, False, Yes, No, 0, 1...
------- End of file "Param.txt"

******************************************************************************/
#include <stdio.h>
#include <string.h>

#include "ParseParam.h"

#define LINE_DIM 8192
char *TempPP=NULL;

/******************************************************************************
FUNCTION: ReadParam
PURPOSE: Read one parameter by parsing a parameter file
RETURNS: a pointer to a string containing the value or NULL if not found
Use the macros to convert to typed values
******************************************************************************/
char* ReadParseParam(const char* FileName, char *VariableName) {
static char Str[LINE_DIM];
char *VarName, *Comment=NULL, *Equal=NULL;
char *FirstQuote, *LastQuote, *P1, *P2;
int Line=0, Len=0, Pos=0;
FILE *file=fopen(FileName, "r");

if (file==NULL) {
fprintf(stderr, "\nError: Could not find file %s", FileName);
exit(1);
}

while (fgets(Str, LINE_DIM-1, file) != NULL) {
Line++;
Len=strlen(Str);
if (Len==0) goto Next;
if (Str[Len-1]=='\n' || Str[Len-1]=='\r') Str[--Len]='\0';
Equal = strchr (Str, '='); // search for equal sign
Pos = strcspn (Str, ";#!"); // search for comment
Comment = (Pos==Len) ? NULL : Str+Pos;
if (Equal==NULL || ( Comment!=NULL && Comment<=Equal)) goto Next; // Only comment
*Equal++ = '\0';
if (Comment!=NULL) *Comment='\0';

// String
FirstQuote=strchr (Equal, '"'); // search for double quote char
LastQuote=strrchr (Equal, '"');
if (FirstQuote!=NULL) {
if (LastQuote==NULL) {
fprintf(stderr, "\nError reading parameter file %s line %d - Missing end quote.", FileName, Line);
goto Next;
}
*FirstQuote=*LastQuote='\0';
Equal=FirstQuote+1;
}

// removes leading/trailing spaces
Pos=strspn (Str, " \t");
if (Pos==strlen(Str)) {
fprintf(stderr, "\nError reading parameter file %s line %d - Missing variable name.", FileName, Line);
goto Next; // No function name
}
while ((P1=strrchr(Str, ' '))!=NULL || (P2=strrchr(Str, '\t'))!=NULL)
if (P1!=NULL) *P1='\0';
else if (P2!=NULL) *P2='\0';
VarName=Str+Pos;
//while (strspn(VarName, " \t")==strlen(VarName)) VarName++;

Pos=strspn (Equal, " \t");
if (Pos==strlen(Equal)) {
fprintf(stderr, "\nError reading parameter file %s line %d - Missing value.", FileName, Line);
goto Next; // No function name
}
Equal+=Pos;

// printf("%s=%s\n", VarName, Equal);
if (strcmp(VarName, VariableName)==0) { // Found it
fclose(file);
return Equal;
}
Next:;
}

// not found
fprintf(stderr, "Error reading parameter file %s - Variable %s not found.",
FileName, VariableName);
fclose(file);
return NULL;
}

int SeperateIntegerFromArray(char *Array, int *List)
{
//input: the array of integer with ',' as the seperater
//output: the number of segments and the array of parsed integers
int count;
char *pch, *pre_start;
char item[1024];

pch = strchr(Array, ',');
count = 0;
pre_start = Array;
while (pch!=NULL)
{
int length = pch-pre_start;
strncpy(item, pre_start, length);
item[length] = '\0';
pre_start = pre_start + length + 1;
List[count] = atoi(item);
count++;
pch = strchr(pre_start, ',');
}
sprintf(item, "%s", pre_start);
List[count] = atoi(item);
count++;
return count;
}

int SeperateFloatFromArray(char *Array, float *List)
{
//input: the array of integer with ',' as the seperater
//output: the number of segments and the array of parsed integers
int count;
char *pch, *pre_start;
char item[1024];

pch = strchr(Array, ',');
count = 0;
pre_start = Array;
while (pch!=NULL)
{
int length = pch-pre_start;
strncpy(item, pre_start, length);
item[length] = '\0';
pre_start = pre_start + length + 1;
List[count] = atof(item);
count++;
pch = strchr(pre_start, ',');
}
sprintf(item, "%s", pre_start);
List[count] = atof(item);
count++;
return count;
}

int SeperateStringFromArray(char *Array, char **List)
{
//input: the array of integer with ',' as the seperater
//output: the number of segments and the array of parsed integers
int count;
char *pch, *pre_start;

pch = strchr(Array, ',');
count = 0;
pre_start = Array;
while (pch!=NULL)
{
int length = pch-pre_start;
strncpy(List[count], pre_start, length);
List[count][length] = '\0';
pre_start = pre_start + length + 1;
count++;
pch = strchr(pre_start, ',');
}
sprintf(List[count], "%s", pre_start);
count++;
return count;
}

使用方法(例子)

1
2
3
4
5
6
   char ParamFileName[300];
sprintf(ParamFileName, "%s", argv[1]); //ParamFileName=argv[1]="parfile.txt"
ParseParamInt(ParamFileName, Image_X_Size);
ParseParamInt(ParamFileName, Image_Y_Size);
ParseParamInt(ParamFileName, Image_Z_Size);
ParseParamString(ParamFileName, AtlasFiles);