HPCToolkit
hpcfmt.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // General and helper functions for reading/writing a HPC data
54 // files from/to a binary file.
55 //
56 // These routines *must not* allocate dynamic memory; if such memory
57 // is needed, callbacks to the user's allocator should be used.
58 //
59 // Description:
60 // [The set of functions, macros, etc. defined in the file]
61 //
62 //***************************************************************************
63 
64 //************************* System Include Files ****************************
65 
66 #include <stdlib.h>
67 #include <stdio.h>
68 #include <string.h>
69 
70 
71 //*************************** User Include Files ****************************
72 
73 #include "hpcfmt.h"
74 #include "hpcio.h"
75 
76 #include <include/uint.h>
77 
78 //*************************** Forward Declarations **************************
79 
80 //***************************************************************************
81 
82 //***************************************************************************
83 // hpcfmt_str_t
84 //***************************************************************************
85 
86 int
87 hpcfmt_str_fread(char** str, FILE* infs, hpcfmt_alloc_fn alloc)
88 {
89  uint32_t len;
90  char* buf = NULL;
91 
93  if (alloc) {
94  buf = (char*) alloc(len+1);
95  }
96  if (!buf) {
97  return HPCFMT_ERR;
98  }
99 
100  for (int i = 0; i < len; i++) {
101  int c = fgetc(infs);
102  if (c == EOF) {
103  return HPCFMT_ERR;
104  }
105  buf[i] = (char) c;
106  }
107  buf[len] = '\0';
108 
109  *str = buf;
110  return HPCFMT_OK;
111 }
112 
113 
114 int
115 hpcfmt_str_fwrite(const char* str, FILE* outfs)
116 {
117  uint32_t len = (str) ? strlen(str) : 0;
118 
119  hpcfmt_int4_fwrite(len, outfs);
120 
121  for (int i = 0; i < len; i++) {
122  int c = fputc(str[i], outfs);
123  if (c == EOF) {
124  return HPCFMT_ERR;
125  }
126  }
127  return HPCFMT_OK;
128 }
129 
130 
131 void
132 hpcfmt_str_free(const char* str, hpcfmt_free_fn dealloc)
133 {
134  dealloc((void *)str);
135 }
136 
137 
138 //***************************************************************************
139 // generic read and write
140 //***************************************************************************
141 
142 
143 int
144 hpcfmt_fread(void *data, size_t size, FILE *infs)
145 {
146  size_t bytes = fread(data, sizeof(char), size, infs);
147  if (bytes == size) {
148  return HPCFMT_OK;
149  }
150  return HPCFMT_ERR;
151 }
152 
153 
154 int
155 hpcfmt_fwrite(void *data, size_t size, FILE *outfs)
156 {
157  size_t bytes = fwrite(data, sizeof(char), size, outfs);
158  if (bytes == size)
159  return HPCFMT_OK;
160  return HPCFMT_ERR;
161 }
162 
163 
164 //***************************************************************************
165 //
166 //***************************************************************************
167 
168 int
170 {
171  int ret;
172 
173  ret = hpcfmt_str_fwrite(nvp->name, fs);
174  if (ret != HPCFMT_OK) {
175  return HPCFMT_ERR;
176  }
177 
178  ret = hpcfmt_str_fwrite(nvp->val, fs);
179  if (ret != HPCFMT_OK) {
180  return HPCFMT_ERR;
181  }
182 
183  return HPCFMT_OK;
184 }
185 
186 
187 int
188 hpcfmt_nvpairs_vfwrite(FILE* out, va_list args)
189 {
190  va_list _tmp;
191  va_copy(_tmp, args);
192 
193  uint32_t len = 0;
194 
195  for (char* arg = va_arg(_tmp, char*); arg != NULL;
196  arg = va_arg(_tmp, char*)) {
197  arg = va_arg(_tmp, char*);
198  len++;
199  }
200  va_end(_tmp);
201 
202  hpcfmt_int4_fwrite(len, out);
203 
204  for (char* arg = va_arg(args, char*); arg != NULL;
205  arg = va_arg(args, char*)) {
206  hpcfmt_str_fwrite(arg, out); // write NAME
207  arg = va_arg(args, char*);
208  hpcfmt_str_fwrite(arg, out); // write VALUE
209  }
210  return HPCFMT_OK;
211 }
212 
213 
214 int
216 {
217  hpcfmt_str_fread(&(inp->name), infs, alloc);
218  hpcfmt_str_fread(&(inp->val), infs, alloc);
219 
220  return HPCFMT_OK;
221 }
222 
223 
224 int
225 hpcfmt_nvpair_fprint(hpcfmt_nvpair_t* nvp, FILE* fs, const char* pre)
226 {
227  fprintf(fs, "%s[nv-pair: '%s', '%s']\n", pre, nvp->name, nvp->val);
228  return HPCFMT_OK;
229 }
230 
231 
232 //***************************************************************************
233 //
234 //***************************************************************************
235 
236 int
238  FILE* infs, hpcfmt_alloc_fn alloc)
239 {
240  HPCFMT_ThrowIfError(hpcfmt_int4_fread(&(nvps->len), infs));
241  if (alloc != NULL) {
242  nvps->lst = (hpcfmt_nvpair_t*) alloc(nvps->len * sizeof(hpcfmt_nvpair_t));
243  }
244  for (uint32_t i = 0; i < nvps->len; i++) {
245  hpcfmt_nvpair_fread(&nvps->lst[i], infs, alloc);
246  }
247  return HPCFMT_OK;
248 }
249 
250 
251 int
253  FILE* fs, const char* pre)
254 {
255  for (uint32_t i = 0; i < nvps->len; ++i) {
256  hpcfmt_nvpair_fprint(&nvps->lst[i], fs, pre);
257  }
258  return HPCFMT_OK;
259 }
260 
261 
262 const char*
264  const char* name)
265 {
266  for (uint32_t i = 0; i < nvps->len; ++i) {
267  if (strcmp(nvps->lst[i].name, name) == 0) {
268  return nvps->lst[i].val;
269  }
270  }
271  return NULL;
272 }
273 
274 
275 void
277  hpcfmt_free_fn dealloc)
278 {
279  for (uint32_t i = 0; i < nvps->len; ++i) {
280  dealloc(nvps->lst[i].name);
281  dealloc(nvps->lst[i].val);
282  }
283  dealloc(nvps->lst);
284 }
void hpcfmt_free_fn(void *mem)
Definition: hpcfmt.h:137
int hpcfmt_nvpairList_fread(HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:237
int hpcfmt_fread(void *data, size_t size, FILE *infs)
Definition: hpcfmt.c:144
Definition: fmt.c:108
static int hpcfmt_int4_fread(uint32_t *val, FILE *infs)
Definition: hpcfmt.h:161
int hpcfmt_nvpair_fwrite(hpcfmt_nvpair_t *nvp, FILE *fs)
Definition: hpcfmt.c:169
static int hpcfmt_int4_fwrite(uint32_t val, FILE *outfs)
Definition: hpcfmt.h:217
const char * hpcfmt_nvpairList_search(const HPCFMT_List(hpcfmt_nvpair_t) *nvps, const char *name)
Definition: hpcfmt.c:263
int hpcfmt_nvpair_fread(hpcfmt_nvpair_t *inp, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:215
#define HPCFMT_List(t)
Definition: hpcfmt.h:115
int hpcfmt_nvpair_fprint(hpcfmt_nvpair_t *nvp, FILE *fs, const char *pre)
Definition: hpcfmt.c:225
void hpcfmt_nvpairList_free(HPCFMT_List(hpcfmt_nvpair_t) *nvps, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:276
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:226
int hpcfmt_str_fwrite(const char *str, FILE *outfs)
Definition: hpcfmt.c:115
int hpcfmt_nvpairs_vfwrite(FILE *out, va_list args)
Definition: hpcfmt.c:188
static char buf[32]
Definition: StrUtil.cpp:240
char * val
Definition: hpcfmt.h:296
#define HPCFMT_ThrowIfError(v)
Definition: hpcfmt.h:97
mem_alloc alloc
void hpcfmt_str_free(const char *str, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:132
int hpcfmt_nvpairList_fprint(const HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *fs, const char *pre)
Definition: hpcfmt.c:252
void * hpcfmt_alloc_fn(size_t nbytes)
Definition: hpcfmt.h:133
#define NULL
Definition: ElfHelper.cpp:85
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:260
int hpcfmt_fwrite(void *data, size_t size, FILE *outfs)
Definition: hpcfmt.c:155
char * name
Definition: hpcfmt.h:295
int hpcfmt_str_fread(char **str, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:87