1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <stddef.h> |
---|
4 | #include <string.h> |
---|
5 | #include "cnames.h" |
---|
6 | |
---|
7 | #define START -1 |
---|
8 | |
---|
9 | static int user_center = 0, user_subcenter = 0, user_ptable = 0; |
---|
10 | static enum {filled, not_found, not_checked, no_file, init} status = init; |
---|
11 | |
---|
12 | struct ParmTable parm_table_user[256]; |
---|
13 | |
---|
14 | /* |
---|
15 | * sets up user parameter table |
---|
16 | */ |
---|
17 | |
---|
18 | int setup_user_table(int center, int subcenter, int ptable) { |
---|
19 | |
---|
20 | int i, j, c0, c1, c2; |
---|
21 | static FILE *input; |
---|
22 | static int file_open = 0; |
---|
23 | char *filename, line[300]; |
---|
24 | |
---|
25 | if (status == init) { |
---|
26 | for (i = 0; i < 256; i++) { |
---|
27 | parm_table_user[i].name = parm_table_user[i].comment = NULL; |
---|
28 | } |
---|
29 | status = not_checked; |
---|
30 | } |
---|
31 | |
---|
32 | if (status == no_file) return 0; |
---|
33 | |
---|
34 | if ((user_center == -1 || center == user_center) && |
---|
35 | (user_subcenter == -1 || subcenter == user_subcenter) && |
---|
36 | (user_ptable == -1 || ptable == user_ptable)) { |
---|
37 | |
---|
38 | if (status == filled) return 1; |
---|
39 | if (status == not_found) return 0; |
---|
40 | } |
---|
41 | |
---|
42 | /* open gribtab file if not open */ |
---|
43 | |
---|
44 | if (!file_open) { |
---|
45 | filename = getenv("GRIBTAB"); |
---|
46 | if (filename == NULL) filename = getenv("gribtab"); |
---|
47 | if (filename == NULL) filename = "gribtab"; |
---|
48 | |
---|
49 | if ((input = fopen(filename,"r")) == NULL) { |
---|
50 | status = no_file; |
---|
51 | return 0; |
---|
52 | } |
---|
53 | file_open = 1; |
---|
54 | } |
---|
55 | else { |
---|
56 | rewind(input); |
---|
57 | } |
---|
58 | |
---|
59 | user_center = center; |
---|
60 | user_subcenter = subcenter; |
---|
61 | user_ptable = ptable; |
---|
62 | |
---|
63 | /* scan for center & subcenter and ptable */ |
---|
64 | for (;;) { |
---|
65 | if (fgets(line, 299, input) == NULL) { |
---|
66 | status = not_found; |
---|
67 | return 0; |
---|
68 | } |
---|
69 | if (atoi(line) != START) continue; |
---|
70 | i = sscanf(line,"%d:%d:%d:%d", &j, ¢er, &subcenter, &ptable); |
---|
71 | if (i != 4) { |
---|
72 | fprintf(stderr,"illegal gribtab center/subcenter/ptable line: %s\n", line); |
---|
73 | continue; |
---|
74 | } |
---|
75 | if ((center == -1 || center == user_center) && |
---|
76 | (subcenter == -1 || subcenter == user_subcenter) && |
---|
77 | (ptable == -1 || ptable == user_ptable)) break; |
---|
78 | } |
---|
79 | |
---|
80 | user_center = center; |
---|
81 | user_subcenter = subcenter; |
---|
82 | user_ptable = ptable; |
---|
83 | |
---|
84 | /* free any used memory */ |
---|
85 | if (parm_table_user[i].name != NULL) { |
---|
86 | for (i = 0; i < 256; i++) { |
---|
87 | free(parm_table_user[i].name); |
---|
88 | free(parm_table_user[i].comment); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /* read definitions */ |
---|
93 | |
---|
94 | for (;;) { |
---|
95 | if (fgets(line, 299, input) == NULL) break; |
---|
96 | if ((i = atoi(line)) == START) break; |
---|
97 | line[299] = 0; |
---|
98 | |
---|
99 | /* find the colons and end-of-line */ |
---|
100 | for (c0 = 0; line[c0] != ':' && line[c0] != 0; c0++) ; |
---|
101 | /* skip blank lines */ |
---|
102 | if (line[c0] == 0) continue; |
---|
103 | |
---|
104 | for (c1 = c0 + 1; line[c1] != ':' && line[c1] != 0; c1++) ; |
---|
105 | c2 = strlen(line); |
---|
106 | if (line[c2-1] == '\n') line[--c2] = '\0'; |
---|
107 | if (c2 <= c1) { |
---|
108 | fprintf(stderr,"illegal gribtab line:%s\n", line); |
---|
109 | continue; |
---|
110 | } |
---|
111 | line[c0] = 0; |
---|
112 | line[c1] = 0; |
---|
113 | |
---|
114 | parm_table_user[i].name = (char *) malloc(c1 - c0); |
---|
115 | parm_table_user[i].comment = (char *) malloc(c2 - c1); |
---|
116 | strcpy(parm_table_user[i].name, line+c0+1); |
---|
117 | strcpy(parm_table_user[i].comment, line+c1+1); |
---|
118 | } |
---|
119 | |
---|
120 | /* now to fill in undefined blanks */ |
---|
121 | for (i = 0; i < 255; i++) { |
---|
122 | if (parm_table_user[i].name == NULL) { |
---|
123 | parm_table_user[i].name = (char *) malloc(7); |
---|
124 | sprintf(parm_table_user[i].name, "var%d", i); |
---|
125 | parm_table_user[i].comment = (char *) malloc(strlen("undefined")+1); |
---|
126 | strcpy(parm_table_user[i].comment, "undefined"); |
---|
127 | } |
---|
128 | } |
---|
129 | status = filled; |
---|
130 | return 1; |
---|
131 | } |
---|