PostgreSQL Source Code  git master
common.c File Reference
#include "postgres_fe.h"
#include "pgtypes.h"
#include "pgtypeslib_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)
 
void PGTYPESchar_free (char *ptr)
 

Function Documentation

◆ pgtypes_alloc()

char* pgtypes_alloc ( long  size)

Definition at line 10 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().

11 {
12  char *new = (char *) calloc(1L, size);
13 
14  if (!new)
15  errno = ENOMEM;
16  return new;
17 }
#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 30 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().

31 {
32  /*
33  * general purpose variable, set to 0 in order to fix compiler warning
34  */
35  int i = 0;
36 
37  switch (replace_type)
38  {
40  break;
43  i = strlen(replace_val.str_val);
44  if (i + 1 <= *pstr_len)
45  {
46  /* include trailing terminator in what we copy */
47  memcpy(*output, replace_val.str_val, i + 1);
48  *pstr_len -= i;
49  *output += i;
50  if (replace_type == PGTYPES_TYPE_STRING_MALLOCED)
51  free(replace_val.str_val);
52  return 0;
53  }
54  else
55  return -1;
56  break;
57  case PGTYPES_TYPE_CHAR:
58  if (*pstr_len >= 2)
59  {
60  (*output)[0] = replace_val.char_val;
61  (*output)[1] = '\0';
62  (*pstr_len)--;
63  (*output)++;
64  return 0;
65  }
66  else
67  return -1;
68  break;
70  case PGTYPES_TYPE_INT64:
71  case PGTYPES_TYPE_UINT:
76  {
78 
79  if (!t)
80  return ENOMEM;
81  switch (replace_type)
82  {
85  "%0.0g", replace_val.double_val);
86  break;
87  case PGTYPES_TYPE_INT64:
89  INT64_FORMAT, replace_val.int64_val);
90  break;
91  case PGTYPES_TYPE_UINT:
93  "%u", replace_val.uint_val);
94  break;
97  "%02u", replace_val.uint_val);
98  break;
101  "%2u", replace_val.uint_val);
102  break;
105  "%03u", replace_val.uint_val);
106  break;
109  "%04u", replace_val.uint_val);
110  break;
111  }
112 
113  if (i < 0 || i >= PGTYPES_FMT_NUM_MAX_DIGITS)
114  {
115  free(t);
116  return -1;
117  }
118  i = strlen(t);
119  *pstr_len -= i;
120 
121  /*
122  * if *pstr_len == 0, we don't have enough space for the
123  * terminator and the conversion fails
124  */
125  if (*pstr_len <= 0)
126  {
127  free(t);
128  return -1;
129  }
130  strcpy(*output, t);
131  *output += i;
132  free(t);
133  }
134  break;
135  default:
136  break;
137  }
138  return 0;
139 }
#define PGTYPES_TYPE_NOTHING
#define PGTYPES_TYPE_UINT_4_LZ
static void output(uint64 loop_count)
#define PGTYPES_TYPE_UINT_3_LZ
#define PGTYPES_TYPE_UINT_2_LS
#define PGTYPES_TYPE_UINT_2_LZ
#define PGTYPES_TYPE_STRING_CONSTANT
#define PGTYPES_TYPE_UINT
char * pgtypes_alloc(long size)
Definition: common.c:10
#define PGTYPES_TYPE_CHAR
#define PGTYPES_TYPE_STRING_MALLOCED
#define PGTYPES_TYPE_DOUBLE_NF
#define free(a)
Definition: header.h:65
#define PGTYPES_FMT_NUM_MAX_DIGITS
#define INT64_FORMAT
Definition: c.h:401
#define PGTYPES_TYPE_INT64
int i
#define snprintf
Definition: port.h:192
unsigned int uint_val

◆ pgtypes_strdup()

char* pgtypes_strdup ( const char *  str)

Definition at line 20 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().

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

◆ PGTYPESchar_free()

void PGTYPESchar_free ( char *  ptr)

Definition at line 145 of file common.c.

References free.

Referenced by dump_sqlda(), and main().

146 {
147  free(ptr);
148 }
#define free(a)
Definition: header.h:65