cis_config
AsciiTableComm.h
1 
2 #ifndef CISASCIITABLECOMM_H_
3 #define CISASCIITABLECOMM_H_
4 
5 #include <../tools.h>
6 #include <CommBase.h>
7 #include <../dataio/AsciiTable.h>
8 
9 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
10 extern "C" {
11 #endif
12 
14 static unsigned _cisAsciiTablesCreated;
15 
21 static inline
22 int init_ascii_table_comm(comm_t *comm) {
23  int flag = 0;
24  // Don't check base validity since address is name
25  comm->is_file = 1;
26  comm->type = ASCII_TABLE_COMM;
27  strcpy(comm->address, comm->name);
28  // Initialize table as handle
29  flag = update_serializer(comm->serializer, ASCII_TABLE_SERI, NULL);
30  if (flag != 0) {
31  cislog_error("init_ascii_table_comm: Could not update serializer.");
32  return -1;
33  }
34  asciiTable_t *handle = (asciiTable_t*)(comm->serializer->info);
35  if (strcmp(comm->direction, "send") == 0)
36  flag = at_update(handle, comm->address, "w");
37  else
38  flag = at_update(handle, comm->address, "r");
39  if (flag != 0) {
40  cislog_error("init_ascii_table_comm: Could not set asciiTable address.");
41  return -1;
42  }
43  comm->handle = (void*)handle;
44  // Simplify received formats
45  if (strcmp(comm->direction, "recv") == 0) {
46  flag = simplify_formats(handle->format_str, CIS_MSG_MAX);
47  if (flag < 0) {
48  cislog_error("init_ascii_table_comm: Failed to simplify recvd format.");
49  return -1;
50  }
51  }
52  // Open the table
53  flag = at_open(handle);
54  if (flag != 0) {
55  cislog_error("init_ascii_table_comm: Could not open %s", comm->name);
56  comm->valid = 0;
57  return -1;
58  }
59  // Write format to file if "send"
60  if (strcmp(comm->direction, "send") == 0)
61  at_writeformat(handle[0]);
62  // Set AsciiTable serializer
63  /* comm->serializer.type = ASCII_TABLE_SERI; */
64  /* comm->serializer.info = (void*)handle; */
65  return 0;
66 };
67 
68 // TODO: Don't create a new file, just send to original
74 static inline
75 int new_ascii_table_address(comm_t *comm) {
76  sprintf(comm->name, "tempASCIITable.%d", _cisAsciiTablesCreated);
77  int ret = init_ascii_table_comm(comm);
78  _cisAsciiTablesCreated++;
79  return ret;
80 };
81 
87 static inline
88 int init_ascii_table_array_comm(comm_t *comm) {
89  int ret = init_ascii_table_comm(comm);
90  comm->serializer->type = ASCII_TABLE_ARRAY_SERI;
91  return ret;
92 };
93 
99 static inline
100 int new_ascii_table_array_address(comm_t *comm) {
101  sprintf(comm->name, "tempASCIITableArray.%d", _cisAsciiTablesCreated);
102  int ret = init_ascii_table_array_comm(comm);
103  _cisAsciiTablesCreated++;
104  return ret;
105 };
106 
112 static inline
113 int free_ascii_table_comm(comm_t *x) {
114  if (x->handle != NULL) {
115  asciiTable_t *table = (asciiTable_t*)x->handle;
116  at_close(table);
117  at_cleanup(table);
118  x->serializer->info = NULL; // Duplicate pointer to handle
119  free(x->handle);
120  x->handle = NULL;
121  }
122  return 0;
123 };
124 
130 static inline
131 int ascii_table_comm_nmsg(const comm_t x) {
132  // Prevent C4100 warning on windows by referencing param
133 #ifdef _WIN32
134  x;
135 #endif
136  // TODO: Count lines in table.
137  return 0;
138 };
139 
149 static inline
150 int ascii_table_comm_send(const comm_t x, const char *data, const size_t len) {
151  if (is_eof(data))
152  return 0;
153  // Prevent C4100 warning on windows by referencing param
154 #ifdef _WIN32
155  len;
156 #endif
157  asciiTable_t *table = (asciiTable_t*)x.handle;
158  return at_writeline_full(table[0], data);
159 };
160 
173 static inline
174 int ascii_table_comm_recv(const comm_t x, char **data, const size_t len,
175  const int allow_realloc) {
176  asciiTable_t *table = (asciiTable_t*)x.handle;
177  return at_readline_full_realloc(table[0], data, len, allow_realloc);
178 };
179 
184 #define ascii_table_comm_send_nolimit ascii_table_comm_send
185 
190 #define ascii_table_comm_recv_nolimit ascii_table_comm_recv
191 
192 
193 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
194 }
195 #endif
196 
197 #endif /*CISASCIITABLECOMM_H_*/
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
char format_str[LINE_SIZE_MAX]
Format string for rows.
Definition: AsciiTable.h:82
Communication structure.
Definition: CommBase.h:23
char address[COMM_ADDRESS_SIZE]
Comm address.
Definition: CommBase.h:26
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:21
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
seri_t * serializer
Serializer for comm messages.
Definition: CommBase.h:31
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 table.
Definition: AsciiTable.h:80
seri_type type
Serializer type.
Definition: SerializeBase.h:20