PostgreSQL Source Code  git master
common.c File Reference
#include "postgres_fe.h"
#include "extern.h"
Include dependency graph for common.c:

Go to the source code of this file.

Functions

char * pgtypes_alloc (long size)
 
char * pgtypes_strdup (const char *str)
 
int pgtypes_fmt_replace (union un_fmt_comb replace_val, int replace_type, char **output, int *pstr_len)
 

Function Documentation

◆ pgtypes_alloc()

char* pgtypes_alloc ( long  size)

Definition at line 9 of file common.c.

References calloc.

Referenced by get_str_from_var(), pgtypes_fmt_replace(), PGTYPESdate_defmt_asc(), PGTYPESdate_fmt_asc(), PGTYPESdate_new(), PGTYPESdecimal_new(), PGTYPESinterval_from_asc(), PGTYPESinterval_new(), PGTYPESnumeric_from_asc(), PGTYPESnumeric_new(), and PGTYPEStimestamp_defmt_scan().

10 {
11  char *new = (char *) calloc(1L, size);
12 
13  if (!new)
14  errno = ENOMEM;
15  return new;
16 }
#define calloc(a, b)
Definition: header.h:55

◆ pgtypes_fmt_replace()

int pgtypes_fmt_replace ( union un_fmt_comb  replace_val,
int  replace_type,
char **  output,
int *  pstr_len 
)

Definition at line 29 of file common.c.

References un_fmt_comb::char_val, un_fmt_comb::double_val, free, i, INT64_FORMAT, un_fmt_comb::int64_val, pgtypes_alloc(), PGTYPES_FMT_NUM_MAX_DIGITS, PGTYPES_TYPE_CHAR, PGTYPES_TYPE_DOUBLE_NF, PGTYPES_TYPE_INT64, PGTYPES_TYPE_NOTHING, PGTYPES_TYPE_STRING_CONSTANT, PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, PGTYPES_TYPE_UINT_2_LS, PGTYPES_TYPE_UINT_2_LZ, PGTYPES_TYPE_UINT_3_LZ, PGTYPES_TYPE_UINT_4_LZ, snprintf(), un_fmt_comb::str_val, and un_fmt_comb::uint_val.

Referenced by dttofmtasc_replace().

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 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:24
#define PGTYPES_TYPE_INT64
Definition: extern.h:15
char * pgtypes_alloc(long size)
Definition: common.c:9
double double_val
Definition: extern.h:32
char char_val
Definition: extern.h:30
#define PGTYPES_TYPE_UINT_4_LZ
Definition: extern.h:21
int64 int64_val
Definition: extern.h:33
#define PGTYPES_TYPE_UINT_3_LZ
Definition: extern.h:20
#define PGTYPES_TYPE_NOTHING
Definition: extern.h:10
#define free(a)
Definition: header.h:65
char * str_val
Definition: extern.h:28
#define PGTYPES_TYPE_UINT_2_LS
Definition: extern.h:18
#define PGTYPES_TYPE_UINT_2_LZ
Definition: extern.h:17
#define PGTYPES_TYPE_STRING_CONSTANT
Definition: extern.h:12
#define INT64_FORMAT
Definition: c.h:338
int i
#define PGTYPES_TYPE_UINT
Definition: extern.h:16
unsigned int uint_val
Definition: extern.h:29

◆ pgtypes_strdup()

char* pgtypes_strdup ( const char *  str)

Definition at line 19 of file common.c.

Referenced by pgtypes_defmt_scan(), PGTYPESdate_defmt_asc(), PGTYPESdate_to_asc(), PGTYPESinterval_to_asc(), PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_to_asc().

20 {
21  char *new = (char *) strdup(str);
22 
23  if (!new)
24  errno = ENOMEM;
25  return new;
26 }