FrontISTR  5.2.0
Large-scale structural analysis program with finit element method
hecmw_result_io.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stddef.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include "hecmw_util.h"
13 #include "hecmw_config.h"
14 #include "hecmw_result_io.h"
15 
16 int istep;
17 int nnode;
18 int nelem;
19 #ifdef OLD_RES_FORMAT
20 int filever_major=1;
21 int filever_minor=0;
22 #else
25 #endif // OLD_RES_FORMAT
29 
33 
36 
37 static int is_valid_label(char *label) {
38 #define ALLOW_CHAR_FIRST "_" /* and alphabet */
39 #define ALLOW_CHAR "_-+" /* and alphabet, digit */
40  int c;
41  char *p, *q;
42 
43  if (label == NULL) return 0;
44 
45  c = label[0];
46 
47  /* check first character */
48  if (!isalpha(c)) {
49  q = ALLOW_CHAR_FIRST;
50  while (*q) {
51  if (*q == c) break;
52  q++;
53  }
54  if (!*q) return 0;
55  }
56 
57  /* check 2nd character or later */
58  p = &label[1];
59  while (*p) {
60  if (!isalnum(*p)) {
61  q = ALLOW_CHAR;
62  while (*q) {
63  if (*q == *p) break;
64  q++;
65  }
66  if (!*q) return 0;
67  }
68  p++;
69  }
70  return 1;
71 }
72 
74  struct result_list *p, *q;
75 
76  for (p = global_list; p; p = q) {
77  q = p->next;
78  HECMW_free(p->label);
79  HECMW_free(p);
80  }
81  global_list = NULL;
82 
83  for (p = node_list; p; p = q) {
84  q = p->next;
85  HECMW_free(p->label);
86  HECMW_free(p);
87  }
88  node_list = NULL;
89 
90  for (p = elem_list; p; p = q) {
91  q = p->next;
92  HECMW_free(p->label);
93  HECMW_free(p);
94  }
95  elem_list = NULL;
96 
97  nnode = nelem = 0;
98  strcpy(head, "");
99 
102 }
103 
104 int HECMW_result_init_body(int n_node, int n_elem, int *nodeID, int *elemID,
105  int i_step, char *header, char *comment) {
106  int len;
107  char *p, *q;
108 
109  nnode = n_node;
110  nelem = n_elem;
111  istep = i_step;
112 
113  node_global_ID = nodeID;
114  elem_global_ID = elemID;
115 
116  if (header == NULL) {
117  head[0] = '\0';
118  return 0;
119  }
120 
121  len = 0;
122  p = header;
123  q = head;
124  while (len < sizeof(head) - 1 && *p && *p != '\n') {
125  *q++ = *p++;
126  len++;
127  }
128  *q++ = '\0';
129 
130  if (comment == NULL) {
131  comment_line[0] = '\0';
132  return 0;
133  }
134 
135  len = 0;
136  p = comment;
137  q = comment_line;
138  while (len < sizeof(comment_line) - 1 && *p && *p != '\n') {
139  *q++ = *p++;
140  len++;
141  }
142  *q++ = '\0';
143 
144  return 0;
145 }
146 
147 static int add_to_global_list(struct result_list *result) {
148  struct result_list *p, *q;
149 
150  q = NULL;
151  for (p = global_list; p; p = (q = p)->next)
152  ;
153 
154  if (q == NULL) {
155  global_list = result;
156  } else {
157  q->next = result;
158  }
159  return 0;
160 }
161 
162 static int add_to_node_list(struct result_list *result) {
163  struct result_list *p, *q;
164 
165  q = NULL;
166  for (p = node_list; p; p = (q = p)->next)
167  ;
168 
169  if (q == NULL) {
170  node_list = result;
171  } else {
172  q->next = result;
173  }
174  return 0;
175 }
176 
177 static int add_to_elem_list(struct result_list *result) {
178  struct result_list *p, *q;
179 
180  q = NULL;
181  for (p = elem_list; p; p = (q = p)->next)
182  ;
183 
184  if (q == NULL) {
185  elem_list = result;
186  } else {
187  q->next = result;
188  }
189  return 0;
190 }
191 
192 static struct result_list *make_result_list(int n_dof, char *label,
193  double *ptr) {
194  struct result_list *result;
195  char *new_label;
196 
197  result = HECMW_malloc(sizeof(*result));
198  if (result == NULL) {
199  HECMW_set_error(errno, "");
200  goto error;
201  }
202 
203  new_label = HECMW_strdup(label);
204  if (new_label == NULL) {
205  HECMW_set_error(errno, "");
206  goto error;
207  }
208 
209  result->label = new_label;
210  result->ptr = ptr;
211  result->n_dof = n_dof;
212  result->next = NULL;
213 
214  return result;
215 error:
216  HECMW_free(result);
217  HECMW_free(new_label);
218  return NULL;
219 }
220 
221 int HECMW_result_add(int dtype, int n_dof, char *label, double *ptr) {
222  struct result_list *result;
223 
224  if (dtype < 1 && dtype > 3) {
226  goto error;
227  }
228 
229  if (!is_valid_label(label)) {
231  goto error;
232  }
233 
234  result = make_result_list(n_dof, label, ptr);
235  if (result == NULL) {
236  goto error;
237  }
238 
239  if (dtype == 1) {
240  /* node */
241  if (add_to_node_list(result)) goto error;
242  } else if (dtype == 2) {
243  /* elem */
244  if (add_to_elem_list(result)) goto error;
245  } else {
246  /* global */
247  if (add_to_global_list(result)) goto error;
248  }
249 
250  return 0;
251 error:
252  HECMW_free(result);
253  return -1;
254 }
255 
257  int ng_comp;
258  struct result_list *p;
259 
260  ng_comp = 0;
261  for (p = global_list; p; p = p->next) {
262  ng_comp++;
263  }
264  return ng_comp;
265 }
266 
268  int nn_comp;
269  struct result_list *p;
270 
271  nn_comp = 0;
272  for (p = node_list; p; p = p->next) {
273  nn_comp++;
274  }
275  return nn_comp;
276 }
277 
279  int ne_comp;
280  struct result_list *p;
281 
282  ne_comp = 0;
283  for (p = elem_list; p; p = p->next) {
284  ne_comp++;
285  }
286  return ne_comp;
287 }
#define HECMW_MSG_LEN
Definition: hecmw_config.h:74
#define HECMW_HEADER_LEN
Definition: hecmw_config.h:68
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
#define LINEBUF_SIZE
#define NULL
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
#define HECMW_strdup(s)
Definition: hecmw_malloc.h:23
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
#define HECMW_UTIL_E0206
Definition: hecmw_msgno.h:368
#define HECMW_UTIL_E0207
Definition: hecmw_msgno.h:369
int HECMW_result_init_body(int n_node, int n_elem, int *nodeID, int *elemID, int i_step, char *header, char *comment)
struct result_list * node_list
int filever_major
struct result_list * elem_list
char line_buf[LINEBUF_SIZE+1]
int * node_global_ID
int filever_minor
#define ALLOW_CHAR_FIRST
int HECMW_result_count_ne_comp(void)
int HECMW_result_count_ng_comp(void)
struct result_list * global_list
int HECMW_result_add(int dtype, int n_dof, char *label, double *ptr)
int * elem_global_ID
int nelem
int istep
void HECMW_result_clear()
int nnode
char comment_line[HECMW_MSG_LEN+1]
int HECMW_result_count_nn_comp(void)
char head[HECMW_HEADER_LEN+1]
#define ALLOW_CHAR
double * ptr
struct result_list * next