cis_config
AsciiFile.h
1 
2 #ifndef ASCIIFILE_H_
3 #define ASCIIFILE_H_
4 
5 #include <../tools.h>
6 
7 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
8 extern "C" {
9 #endif
10 
12 #define LINE_SIZE_MAX 1024*2
13 
15 typedef struct asciiFile_t {
16  const char *filepath;
17  char io_mode[64];
18  char comment[64];
19  char newline[64];
20  FILE *fd;
21 } asciiFile_t;
22 
28 static inline
29 int af_is_open(const asciiFile_t t) {
30  if (t.fd == NULL)
31  return 0;
32  else
33  return 1;
34 };
35 
41 static inline
42 int af_open(asciiFile_t *t) {
43  int ret = -1;
44  if (af_is_open(*t) == 0) {
45  (*t).fd = fopen((*t).filepath, (*t).io_mode);
46  if ((*t).fd != NULL)
47  ret = 0;
48  } else {
49  ret = 0;
50  }
51  return ret;
52 };
53 
59 static inline
60 int af_close(asciiFile_t *t) {
61  int ret;
62  if (af_is_open(*t) == 1) {
63  fclose((*t).fd);
64  (*t).fd = NULL;
65  ret = 0;
66  } else {
67  ret = 0;
68  }
69  return ret;
70 };
71 
78 static inline
79 int af_is_comment(const asciiFile_t t, const char *line) {
80  if (strncmp(line, t.comment, strlen(t.comment)) == 0)
81  return 1;
82  else
83  return 0;
84 };
85 
95 static inline
96 int af_readline_full_norealloc(const asciiFile_t t, char *line, size_t n) {
97  if (af_is_open(t) == 1) {
98  if (fgets(line, (int)n, t.fd) == NULL) {
99  return -1;
100  }
101  int nread = (int)strlen(line);
102  if ((nread < ((int)n - 1)) || (line[nread - 1] == '\n') || (feof(t.fd)))
103  return nread;
104  }
105  return -1;
106 };
107 
118 static inline
119 int af_readline_full(const asciiFile_t t, char **line, size_t *n) {
120  if (af_is_open(t) == 1) {
121  return (int)getline(line, n, t.fd);
122  }
123  return -1;
124 };
125 
132 static inline
133 int af_writeline_full(const asciiFile_t t, const char *line) {
134  if (af_is_open(t) == 1)
135  return (int)fwrite(line, 1, strlen(line), t.fd);
136  return -1;
137 };
138 
147 static inline
148 int af_update(asciiFile_t *t, const char *filepath, const char *io_mode) {
149  t->filepath = filepath;
150  strcpy(t->io_mode, io_mode);
151  return 0;
152 };
153 
165 static inline
166 asciiFile_t asciiFile(const char *filepath, const char *io_mode,
167  const char *comment, const char *newline) {
168  asciiFile_t t;
169  t.fd = NULL;
170  af_update(&t, filepath, io_mode);
171  // Set defaults for optional parameters
172  if (comment == NULL)
173  strcpy(t.comment, "# ");
174  else
175  strcpy(t.comment, comment);
176  if (newline == NULL)
177  strcpy(t.newline, "\n");
178  else
179  strcpy(t.newline, newline);
180  return t;
181 };
182 
183 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
184 }
185 #endif
186 
187 #endif /*ASCIIFILE_H_*/
const char * filepath
Full path to file.
Definition: AsciiFile.h:16
char comment[64]
Character(s) indicating a comment.
Definition: AsciiFile.h:18
char newline[64]
Character(s) indicating a newline.
Definition: AsciiFile.h:19
char io_mode[64]
I/O mode. &#39;r&#39; for read, &#39;w&#39; for write.
Definition: AsciiFile.h:17
Structure containing information about an ASCII text file.
Definition: AsciiFile.h:15
FILE * fd
File identifier for ASCII file when open.
Definition: AsciiFile.h:20