FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_lib_fc.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 <string.h>
8 #include <errno.h>
9 #include "hecmw_util.h"
10 
11 char *HECMW_strcpy_f2c(const char *fstr, int flen) {
12  int i, len;
13  char *s;
14 
15  if (fstr == NULL) return NULL;
16  if (flen <= 0) return NULL;
17 
18  len = 0;
19  for (i = flen - 1; i >= 0; i--) {
20  if (fstr[i] != ' ') {
21  len = i + 1;
22  break;
23  }
24  }
25 
26  if (len == 0) {
27  s = HECMW_strdup("");
28  if (s == NULL) {
29  HECMW_set_error(errno, "");
30  return NULL;
31  }
32  return s;
33  }
34 
35  s = HECMW_malloc(len + 1);
36  if (s == NULL) {
37  HECMW_set_error(errno, "");
38  return NULL;
39  }
40  snprintf(s, len + 1, "%.*s", len, fstr);
41  return s;
42 }
43 
44 char *HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize) {
45  int i, len;
46 
47  if (fstr == NULL) return NULL;
48  if (flen <= 0) return NULL;
49  if (buf == NULL) return NULL;
50  if (bufsize <= 0) return NULL;
51 
52  len = 0;
53  for (i = flen - 1; i >= 0; i--) {
54  if (fstr[i] != ' ') {
55  len = i + 1;
56  break;
57  }
58  }
59  if (len == 0) {
60  buf[0] = '\0';
61  return buf;
62  }
63  if (len > bufsize - 1) {
64  len = bufsize - 1;
65  }
66  snprintf(buf, bufsize, "%.*s", len, fstr);
67  return buf;
68 }
69 
70 int HECMW_strcpy_c2f(const char *cstr, char *fstr, int flen) {
71  int clen;
72 
73  if (fstr == NULL) return 0;
74  if (flen <= 0) return 0;
75 
76  if (cstr == NULL) {
77  clen = 0;
78  } else {
79  clen = strlen(cstr);
80  }
81  if (clen > flen) {
82  clen = flen;
83  }
84  memset(fstr, ' ', flen);
85  if (clen > 0) memcpy(fstr, cstr, clen);
86  return flen;
87 }
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:33
#define NULL
char * HECMW_strcpy_f2c(const char *fstr, int flen)
Definition: hecmw_lib_fc.c:11
int HECMW_strcpy_c2f(const char *cstr, char *fstr, int flen)
Definition: hecmw_lib_fc.c:70
char * HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize)
Definition: hecmw_lib_fc.c:44
#define HECMW_strdup(s)
Definition: hecmw_malloc.h:23
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20