Skip to content

Commit

Permalink
created a token parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Park authored and Mike Park committed Jun 23, 2021
1 parent e799eac commit 41e288a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/ref_phys.c
Expand Up @@ -368,6 +368,30 @@ REF_STATUS ref_phys_read_mapbc(REF_DICT ref_dict, const char *mapbc_filename) {
return REF_SUCCESS;
}

REF_STATUS ref_phys_read_mapbc_token(REF_DICT ref_dict,
const char *mapbc_filename,
const char *token) {
FILE *file;
REF_INT i, n, id, type;
char name[1024];
file = fopen(mapbc_filename, "r");
if (NULL == (void *)file) printf("unable to open %s\n", mapbc_filename);
RNS(file, "unable to open file");
RES(1, fscanf(file, "%d", &n), "number of lines");
for (i = 0; i < n; i++) {
RES(1, fscanf(file, "%d", &id), "read id");
RES(1, fscanf(file, "%d", &type), "read type");
REIS(32, fgetc(file), "expected space");
fgets(name, sizeof(name), file);
/* printf(">%s<>%s<\n",name,token); */
if (0 == strncmp(name, token, strlen(token))) {
RSS(ref_dict_store(ref_dict, id, type), "store");
}
}
fclose(file);
return REF_SUCCESS;
}

REF_STATUS ref_phys_parse_tags(REF_DICT ref_dict, const char *tags) {
REF_INT id, type;
char *token, *copy;
Expand Down
3 changes: 3 additions & 0 deletions src/ref_phys.h
Expand Up @@ -51,6 +51,9 @@ REF_STATUS ref_phys_mask_strong_bcs(REF_GRID ref_grid, REF_DICT ref_dict,
REF_BOOL *replace, REF_INT ldim);

REF_STATUS ref_phys_read_mapbc(REF_DICT ref_dict, const char *mapbc_filename);
REF_STATUS ref_phys_read_mapbc_token(REF_DICT ref_dict,
const char *mapbc_filename,
const char *token);
REF_STATUS ref_phys_parse_tags(REF_DICT ref_dict, const char *tags);
REF_STATUS ref_phys_av_tag_attributes(REF_DICT ref_dict, REF_GEOM ref_geom);

Expand Down
31 changes: 31 additions & 0 deletions src/ref_phys_test.c
Expand Up @@ -1459,6 +1459,37 @@ int main(int argc, char *argv[]) {
REIS(0, remove(file), "test clean up");
}

if (ref_mpi_once(ref_mpi)) {
char file[] = "ref_phys_test_inflate.mapbc";
char token[] = "inflate";
FILE *f;
REF_DICT ref_dict;
REF_INT id, type;

f = fopen(file, "w");
fprintf(f, "4\n");
fprintf(f, "1 5000 inflow\n");
fprintf(f, "2 5000 inflate\n");
fprintf(f, "3 5000 inflate\n");
fprintf(f, "4 5000 outflow\n");
fclose(f);

RSS(ref_dict_create(&ref_dict), "create");

RSS(ref_phys_read_mapbc_token(ref_dict, file, token), "read mapbc");

REIS(2, ref_dict_n(ref_dict), "lines");
id = 2;
RSS(ref_dict_value(ref_dict, id, &type), "retrieve");
REIS(5000, type, "type");
id = 3;
RSS(ref_dict_value(ref_dict, id, &type), "retrieve");
REIS(5000, type, "type");

RSS(ref_dict_free(ref_dict), "free");
REIS(0, remove(file), "test clean up");
}

{ /* parse string of 1 bc tag into ref_dict */
char tags[] = "7";
REF_INT id, type;
Expand Down

0 comments on commit 41e288a

Please sign in to comment.