PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
common.c
Go to the documentation of this file.
1 /* src/interfaces/ecpg/pgtypeslib/common.c */
2 
3 #include "postgres_fe.h"
4 
5 #include "extern.h"
6 
7 /* Return value is zero-filled. */
8 char *
9 pgtypes_alloc(long size)
10 {
11  char *new = (char *) calloc(1L, size);
12 
13  if (!new)
14  errno = ENOMEM;
15  return (new);
16 }
17 
18 char *
19 pgtypes_strdup(const char *str)
20 {
21  char *new = (char *) strdup(str);
22 
23  if (!new)
24  errno = ENOMEM;
25  return (new);
26 }
27 
28 int
29 pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char **output, int *pstr_len)
30 {
31  /*
32  * general purpose variable, set to 0 in order to fix compiler warning
33  */
34  int i = 0;
35 
36  switch (replace_type)
37  {
39  break;
42  i = strlen(replace_val.str_val);
43  if (i + 1 <= *pstr_len)
44  {
45  /*
46  * copy over i + 1 bytes, that includes the tailing terminator
47  */
48  strncpy(*output, replace_val.str_val, i + 1);
49  *pstr_len -= i;
50  *output += i;
51  if (replace_type == PGTYPES_TYPE_STRING_MALLOCED)
52  free(replace_val.str_val);
53  return 0;
54  }
55  else
56  return -1;
57  break;
58  case PGTYPES_TYPE_CHAR:
59  if (*pstr_len >= 2)
60  {
61  (*output)[0] = replace_val.char_val;
62  (*output)[1] = '\0';
63  (*pstr_len)--;
64  (*output)++;
65  return 0;
66  }
67  else
68  return -1;
69  break;
71  case PGTYPES_TYPE_INT64:
72  case PGTYPES_TYPE_UINT:
77  {
79 
80  if (!t)
81  return ENOMEM;
82  switch (replace_type)
83  {
86  "%0.0g", replace_val.double_val);
87  break;
88  case PGTYPES_TYPE_INT64:
90  INT64_FORMAT, replace_val.int64_val);
91  break;
92  case PGTYPES_TYPE_UINT:
94  "%u", replace_val.uint_val);
95  break;
98  "%02u", replace_val.uint_val);
99  break;
102  "%2u", replace_val.uint_val);
103  break;
106  "%03u", replace_val.uint_val);
107  break;
110  "%04u", replace_val.uint_val);
111  break;
112  }
113 
114  if (i < 0)
115  {
116  free(t);
117  return -1;
118  }
119  i = strlen(t);
120  *pstr_len -= i;
121 
122  /*
123  * if *pstr_len == 0, we don't have enough space for the
124  * terminator and the conversion fails
125  */
126  if (*pstr_len <= 0)
127  {
128  free(t);
129  return -1;
130  }
131  strcpy(*output, t);
132  *output += i;
133  free(t);
134  }
135  break;
136  default:
137  break;
138  }
139  return 0;
140 }
#define calloc(a, b)
Definition: header.h:55
#define PGTYPES_TYPE_STRING_MALLOCED
Definition: extern.h:11
#define PGTYPES_TYPE_CHAR
Definition: extern.h:13
static void output(uint64 loop_count)
#define PGTYPES_TYPE_DOUBLE_NF
Definition: extern.h:14
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PGTYPES_FMT_NUM_MAX_DIGITS
Definition: extern.h:25
#define PGTYPES_TYPE_INT64
Definition: extern.h:15
char * pgtypes_strdup(const char *str)
Definition: common.c:19
char * pgtypes_alloc(long size)
Definition: common.c:9
double double_val
Definition: extern.h:33
char char_val
Definition: extern.h:31
#define PGTYPES_TYPE_UINT_4_LZ
Definition: extern.h:22
int64 int64_val
Definition: extern.h:34
#define PGTYPES_TYPE_UINT_3_LZ
Definition: extern.h:21
#define PGTYPES_TYPE_NOTHING
Definition: extern.h:10
#define free(a)
Definition: header.h:65
char * str_val
Definition: extern.h:29
#define PGTYPES_TYPE_UINT_2_LS
Definition: extern.h:19
#define PGTYPES_TYPE_UINT_2_LZ
Definition: extern.h:17
#define PGTYPES_TYPE_STRING_CONSTANT
Definition: extern.h:12
int pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char **output, int *pstr_len)
Definition: common.c:29
#define INT64_FORMAT
Definition: c.h:315
int i
#define PGTYPES_TYPE_UINT
Definition: extern.h:16
unsigned int uint_val
Definition: extern.h:30