Mercury
mercury_proc_string.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Argonne National Laboratory, Department of Energy,
3  *                    UChicago Argonne, LLC and The HDF Group.
4  * All rights reserved.
5  *
6  * The full copyright notice, including terms governing use, modification,
7  * and redistribution, is contained in the COPYING file that can be
8  * found at the root of the source code distribution tree.
9  */
10 
11 #ifndef MERCURY_PROC_STRING_H
12 #define MERCURY_PROC_STRING_H
13 
14 #include "mercury_proc.h"
15 #include "mercury_string_object.h"
16 
17 #include <string.h>
18 
19 typedef const char * hg_const_string_t;
20 typedef char * hg_string_t;
21 
22 #ifndef HG_PROC_STRING_INLINE
23  #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
24  #define HG_PROC_STRING_INLINE extern HG_INLINE
25  #else
26  #define HG_PROC_STRING_INLINE HG_INLINE
27  #endif
28 #endif
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
38  hg_proc_t proc, hg_const_string_t *data);
40  hg_proc_t proc, hg_string_t *data);
42  hg_proc_t proc, hg_string_object_t *data);
43 
54 {
55  hg_string_object_t string;
56  hg_return_t ret = HG_FAIL;
57 
58  switch (hg_proc_get_op(proc)) {
59  case HG_ENCODE:
60  hg_string_object_init_const_char(&string, *data, 0);
61  ret = hg_proc_hg_string_object_t(proc, &string);
62  if (ret != HG_SUCCESS) {
63  HG_ERROR_DEFAULT("Proc error");
64  ret = HG_FAIL;
65  }
66  hg_string_object_free(&string);
67  break;
68  case HG_DECODE:
69  ret = hg_proc_hg_string_object_t(proc, &string);
70  if (ret != HG_SUCCESS) {
71  HG_ERROR_DEFAULT("Proc error");
72  ret = HG_FAIL;
73  }
74  *data = hg_string_object_swap(&string, 0);
75  hg_string_object_free(&string);
76  break;
77  case HG_FREE:
78  hg_string_object_init_const_char(&string, *data, 1);
79  ret = hg_proc_hg_string_object_t(proc, &string);
80  if (ret != HG_SUCCESS) {
81  HG_ERROR_DEFAULT("Proc error");
82  ret = HG_FAIL;
83  }
84  break;
85  default:
86  break;
87  }
88 
89  return ret;
90 }
91 
102 {
103  hg_string_object_t string;
104  hg_return_t ret = HG_FAIL;
105 
106  switch (hg_proc_get_op(proc)) {
107  case HG_ENCODE:
108  hg_string_object_init_char(&string, *data, 0);
109  ret = hg_proc_hg_string_object_t(proc, &string);
110  if (ret != HG_SUCCESS) {
111  HG_ERROR_DEFAULT("Proc error");
112  ret = HG_FAIL;
113  }
114  hg_string_object_free(&string);
115  break;
116  case HG_DECODE:
117  ret = hg_proc_hg_string_object_t(proc, &string);
118  if (ret != HG_SUCCESS) {
119  HG_ERROR_DEFAULT("Proc error");
120  ret = HG_FAIL;
121  }
122  *data = hg_string_object_swap(&string, 0);
123  hg_string_object_free(&string);
124  break;
125  case HG_FREE:
126  hg_string_object_init_char(&string, *data, 1);
127  ret = hg_proc_hg_string_object_t(proc, &string);
128  if (ret != HG_SUCCESS) {
129  HG_ERROR_DEFAULT("Proc error");
130  ret = HG_FAIL;
131  }
132  break;
133  default:
134  break;
135  }
136 
137  return ret;
138 }
139 
150 {
151  hg_uint64_t string_len = 0;
152  hg_return_t ret = HG_FAIL;
153 
154  switch (hg_proc_get_op(proc)) {
155  case HG_ENCODE:
156  string_len = (string->data) ? strlen(string->data) + 1 : 0;
157  ret = hg_proc_uint64_t(proc, &string_len);
158  if (ret != HG_SUCCESS) {
159  HG_ERROR_DEFAULT("Proc error");
160  ret = HG_FAIL;
161  return ret;
162  }
163  if (string_len) {
164  ret = hg_proc_raw(proc, string->data, string_len);
165  if (ret != HG_SUCCESS) {
166  HG_ERROR_DEFAULT("Proc error");
167  ret = HG_FAIL;
168  return ret;
169  }
170  ret = hg_proc_hg_uint8_t(proc, (hg_uint8_t*) &string->is_const);
171  if (ret != HG_SUCCESS) {
172  HG_ERROR_DEFAULT("Proc error");
173  ret = HG_FAIL;
174  return ret;
175  }
176  ret = hg_proc_hg_uint8_t(proc, (hg_uint8_t*) &string->is_owned);
177  if (ret != HG_SUCCESS) {
178  HG_ERROR_DEFAULT("Proc error");
179  ret = HG_FAIL;
180  return ret;
181  }
182  }
183  break;
184  case HG_DECODE:
185  ret = hg_proc_uint64_t(proc, &string_len);
186  if (ret != HG_SUCCESS) {
187  HG_ERROR_DEFAULT("Proc error");
188  ret = HG_FAIL;
189  return ret;
190  }
191  if (string_len) {
192  string->data = (char*) malloc(string_len);
193  ret = hg_proc_raw(proc, string->data, string_len);
194  if (ret != HG_SUCCESS) {
195  HG_ERROR_DEFAULT("Proc error");
196  ret = HG_FAIL;
197  return ret;
198  }
199  ret = hg_proc_hg_uint8_t(proc, (hg_uint8_t*) &string->is_const);
200  if (ret != HG_SUCCESS) {
201  HG_ERROR_DEFAULT("Proc error");
202  ret = HG_FAIL;
203  return ret;
204  }
205  ret = hg_proc_hg_uint8_t(proc, (hg_uint8_t*) &string->is_owned);
206  if (ret != HG_SUCCESS) {
207  HG_ERROR_DEFAULT("Proc error");
208  ret = HG_FAIL;
209  return ret;
210  }
211  } else {
212  string->data = NULL;
213  }
214  break;
215  case HG_FREE:
216  ret = hg_string_object_free(string);
217  if (ret != HG_SUCCESS) {
218  HG_ERROR_DEFAULT("Could not free string object");
219  ret = HG_FAIL;
220  }
221  break;
222  default:
223  break;
224  }
225 
226  return ret;
227 }
228 
229 #ifdef __cplusplus
230 }
231 #endif
232 
233 #endif /* MERCURY_PROC_STRING_H */
HG_EXPORT hg_proc_op_t hg_proc_get_op(hg_proc_t proc)
Get the operation type associated to the processor.
HG_EXPORT char * hg_string_object_swap(hg_string_object_t *string, char *s)
Exchange the content of the string structure by the content of s.
const char * hg_const_string_t
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_raw(hg_proc_t proc, void *buf, size_t buf_size)
Generic processing routine.
Definition: mercury_proc.h:463
#define HG_ERROR_DEFAULT
Definition: mercury_error.h:55
char * hg_string_t
#define HG_PROC_STRING_INLINE
HG_EXPORT hg_return_t hg_string_object_free(hg_string_object_t *string)
Free a string object.
#define hg_proc_uint64_t
Definition: mercury_proc.h:286
HG_EXPORT hg_return_t hg_string_object_init_const_char(hg_string_object_t *string, const char *s, hg_bool_t is_owned)
Initialize a string object from the const string pointed to by s.
void * hg_proc_t
Definition: mercury_types.h:21
HG_EXPORT HG_PROC_STRING_INLINE hg_return_t hg_proc_hg_const_string_t(hg_proc_t proc, hg_const_string_t *data)
Inline prototypes (do not remove)
HG_EXPORT HG_PROC_STRING_INLINE hg_return_t hg_proc_hg_string_object_t(hg_proc_t proc, hg_string_object_t *data)
Generic processing routine.
HG_EXPORT hg_return_t hg_string_object_init_char(hg_string_object_t *string, char *s, hg_bool_t is_owned)
Initialize a string object from the string pointed to by s.
HG_EXPORT HG_PROC_STRING_INLINE hg_return_t hg_proc_hg_string_t(hg_proc_t proc, hg_string_t *data)
Generic processing routine.
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_uint8_t(hg_proc_t proc, hg_uint8_t *data)
Generic processing routine.
Definition: mercury_proc.h:322
enum hg_return hg_return_t