cis_config
AsciiFileComm.h
1 
2 #ifndef CISASCIIFILECOMM_H_
3 #define CISASCIIFILECOMM_H_
4 
5 #include <../tools.h>
6 #include <CommBase.h>
7 #include <../dataio/AsciiFile.h>
8 #include <../dataio/AsciiTable.h>
9 
10 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
11 extern "C" {
12 #endif
13 
15 static unsigned _cisAsciiFilesCreated;
16 
22 static inline
23 int init_ascii_file_comm(comm_t *comm) {
24  // Don't check base validity since address is name
25  comm->is_file = 1;
26  comm->type = ASCII_FILE_COMM;
27  strcpy(comm->address, comm->name);
28  asciiFile_t *handle = (asciiFile_t*)malloc(sizeof(asciiFile_t));
29  if (handle == NULL) {
30  cislog_error("init_ascii_file_comm: Failed to malloc asciiFile handle.");
31  return -1;
32  }
33  if (strcmp(comm->direction, "send") == 0)
34  handle[0] = asciiFile(comm->address, "w", NULL, NULL);
35  else
36  handle[0] = asciiFile(comm->address, "r", NULL, NULL);
37  comm->handle = (void*)handle;
38  int ret = af_open(handle);
39  if (ret != 0) {
40  cislog_error("init_ascii_file_comm: Could not open %s", comm->name);
41  comm->valid = 0;
42  }
43  return ret;
44 };
45 
51 static inline
52 int new_ascii_file_address(comm_t *comm) {
53  sprintf(comm->name, "temp%d", _cisAsciiFilesCreated);
54  int ret = init_ascii_file_comm(comm);
55  return ret;
56 };
57 
63 static inline
64 int free_ascii_file_comm(comm_t *x) {
65  if (x->handle != NULL) {
66  asciiFile_t *file = (asciiFile_t*)x->handle;
67  af_close(file);
68  free(x->handle);
69  x->handle = NULL;
70  }
71  return 0;
72 };
73 
79 static inline
80 int ascii_file_comm_nmsg(const comm_t x) {
81  // Prevent C4100 warning on windows by referencing param
82 #ifdef _WIN32
83  x;
84 #endif
85  // TODO: Count lines in file.
86  return 0;
87 };
88 
98 static inline
99 int ascii_file_comm_send(const comm_t x, const char *data, const size_t len) {
100  if (is_eof(data))
101  return 0;
102  // Prevent C4100 warning on windows by referencing param
103 #ifdef _WIN32
104  len;
105 #endif
106  asciiFile_t *file = (asciiFile_t*)x.handle;
107  return af_writeline_full(file[0], data);
108 };
109 
122 static inline
123 int ascii_file_comm_recv(const comm_t x, char **data, size_t len,
124  const int allow_realloc) {
125  asciiFile_t *file = (asciiFile_t*)x.handle;
126  if (allow_realloc) {
127  return af_readline_full(file[0], data, (size_t*)(&len));
128  } else {
129  return af_readline_full_norealloc(file[0], data[0], len);
130  }
131 };
132 
137 #define ascii_file_comm_send_nolimit ascii_file_comm_send
138 
143 #define ascii_file_comm_recv_nolimit ascii_file_comm_recv
144 
145 
146 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
147 }
148 #endif
149 
150 #endif /*CISASCIIFILECOMM_H_*/
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
Communication structure.
Definition: CommBase.h:23
char address[COMM_ADDRESS_SIZE]
Comm address.
Definition: CommBase.h:26
comm_type type
Comm type.
Definition: CommBase.h:24
int is_file
Flag specifying if the comm connects directly to a file.
Definition: CommBase.h:40
char direction[COMM_DIR_SIZE]
send or recv for direction messages will go.
Definition: CommBase.h:27
char name[COMM_NAME_SIZE]
Comm name.
Definition: CommBase.h:25
int valid
1 if communicator initialized, 0 otherwise.
Definition: CommBase.h:28
Structure containing information about an ASCII text file.
Definition: AsciiFile.h:15