Skip to content

Commit 82b534b

Browse files
committed
Merge branch 'master' into simmetrix
2 parents 6fcb7e0 + 8c46710 commit 82b534b

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

phasta/phIO.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ void ph_write_header(FILE* f, const char* name, size_t bytes,
2929
fprintf(f, "\n");
3030
}
3131

32+
static void skip_leading_spaces(char** s)
33+
{
34+
while (**s == ' ') ++(*s);
35+
}
36+
3237
static void cut_trailing_spaces(char* s)
3338
{
3439
char* e = s + strlen(s);
@@ -47,6 +52,7 @@ static void parse_header(char* header, char** name, size_t* bytes,
4752
header = strtok_r(header, ":", &saveptr);
4853
if (name) {
4954
*name = header;
55+
skip_leading_spaces(name);
5056
cut_trailing_spaces(*name);
5157
}
5258
strtok_r(NULL, "<", &saveptr);
@@ -59,7 +65,7 @@ static void parse_header(char* header, char** name, size_t* bytes,
5965
}
6066
}
6167

62-
static void find_header(FILE* f, const char* name, char header[PH_LINE])
68+
static int find_header(FILE* f, const char* name, char header[PH_LINE])
6369
{
6470
char* hname;
6571
size_t bytes;
@@ -70,10 +76,12 @@ static void find_header(FILE* f, const char* name, char header[PH_LINE])
7076
strcpy(tmp, header);
7177
parse_header(tmp, &hname, &bytes, 0, NULL);
7278
if (!strcmp(name, hname))
73-
return;
79+
return 1;
7480
fseek(f, bytes, SEEK_CUR);
7581
}
76-
header[0] = '\0';
82+
if (!PCU_Comm_Self())
83+
fprintf(stderr,"warning: phIO could not find \"%s\"\n",name);
84+
return 0;
7785
}
7886

7987
static void get_now_string(char s[PH_LINE])
@@ -97,8 +105,7 @@ static void write_magic_number(FILE* f)
97105
static int seek_after_header(FILE* f, const char* name)
98106
{
99107
char dummy[PH_LINE];
100-
find_header(f, name, dummy);
101-
return dummy[0] != '\0';
108+
return find_header(f, name, dummy);
102109
}
103110

104111
static void my_fread(void* p, size_t size, size_t nmemb, FILE* f)
@@ -111,7 +118,7 @@ static int read_magic_number(FILE* f)
111118
{
112119
if (!seek_after_header(f, magic_name)) {
113120
if (!PCU_Comm_Self())
114-
fprintf(stderr,"warning: no byteorder magic number. not swapping\n");
121+
fprintf(stderr,"warning: not swapping bytes\n");
115122
rewind(f);
116123
return 0;
117124
}
@@ -163,17 +170,15 @@ void ph_read_field(const char* file, const char* field, double** data,
163170
size_t bytes, n;
164171
char header[PH_LINE];
165172
int should_swap;
173+
int ok;
166174
FILE* f = fopen(file, "r");
167175
if (!f) {
168176
fprintf(stderr,"could not open \"%s\"\n", file);
169177
abort();
170178
}
171179
should_swap = read_magic_number(f);
172-
find_header(f, field, header);
173-
if (header[0] == '\0') {
174-
fprintf(stderr,"could not find field \"%s\"\n",field);
175-
abort();
176-
}
180+
ok = find_header(f, field, header);
181+
assert(ok);
177182
parse_params(header, &bytes, nodes, vars, step);
178183
assert(((bytes - 1) % sizeof(double)) == 0);
179184
n = (bytes - 1) / sizeof(double);

0 commit comments

Comments
 (0)