FrontISTR  5.7.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  strncpy(s, fstr, len);
41  s[len] = '\0';
42  return s;
43 }
44 
45 char *HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize) {
46  int i, len;
47 
48  if (fstr == NULL) return NULL;
49  if (flen <= 0) return NULL;
50  if (buf == NULL) return NULL;
51  if (bufsize <= 0) return NULL;
52 
53  len = 0;
54  for (i = flen - 1; i >= 0; i--) {
55  if (fstr[i] != ' ') {
56  len = i + 1;
57  break;
58  }
59  }
60  if (len == 0) {
61  buf[0] = '\0';
62  return buf;
63  }
64  if (len > bufsize - 1) {
65  len = bufsize - 1;
66  }
67  strncpy(buf, fstr, len);
68  buf[len] = '\0';
69  return buf;
70 }
71 
72 int HECMW_strcpy_c2f(const char *cstr, char *fstr, int flen) {
73  int clen;
74 
75  if (fstr == NULL) return 0;
76  if (flen <= 0) return 0;
77 
78  if (cstr == NULL) {
79  clen = 0;
80  } else {
81  clen = strlen(cstr);
82  }
83  if (clen > flen) {
84  clen = flen;
85  }
86  memset(fstr, ' ', flen);
87  strncpy(fstr, cstr, clen);
88  return flen;
89 }
HECMW_malloc
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
HECMW_strcpy_c2f
int HECMW_strcpy_c2f(const char *cstr, char *fstr, int flen)
Definition: hecmw_lib_fc.c:72
HECMW_strdup
#define HECMW_strdup(s)
Definition: hecmw_malloc.h:23
HECMW_strcpy_f2c
char * HECMW_strcpy_f2c(const char *fstr, int flen)
Definition: hecmw_lib_fc.c:11
HECMW_strcpy_f2c_r
char * HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize)
Definition: hecmw_lib_fc.c:45
HECMW_set_error
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
hecmw_util.h