PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
int8.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool scanint8 (const char *str, bool errorOK, int64 *result)
 

Function Documentation

bool scanint8 ( const char *  str,
bool  errorOK,
int64 *  result 
)

Definition at line 55 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_INT64_MIN, and sign.

Referenced by int8in(), make_const(), and parse_output_parameters().

56 {
57  const char *ptr = str;
58  int64 tmp = 0;
59  int sign = 1;
60 
61  /*
62  * Do our own scan, rather than relying on sscanf which might be broken
63  * for long long.
64  */
65 
66  /* skip leading spaces */
67  while (*ptr && isspace((unsigned char) *ptr))
68  ptr++;
69 
70  /* handle sign */
71  if (*ptr == '-')
72  {
73  ptr++;
74 
75  /*
76  * Do an explicit check for INT64_MIN. Ugly though this is, it's
77  * cleaner than trying to get the loop below to handle it portably.
78  */
79  if (strncmp(ptr, "9223372036854775808", 19) == 0)
80  {
81  tmp = PG_INT64_MIN;
82  ptr += 19;
83  goto gotdigits;
84  }
85  sign = -1;
86  }
87  else if (*ptr == '+')
88  ptr++;
89 
90  /* require at least one digit */
91  if (!isdigit((unsigned char) *ptr))
92  {
93  if (errorOK)
94  return false;
95  else
96  ereport(ERROR,
97  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98  errmsg("invalid input syntax for %s: \"%s\"",
99  "integer", str)));
100  }
101 
102  /* process digits */
103  while (*ptr && isdigit((unsigned char) *ptr))
104  {
105  int64 newtmp = tmp * 10 + (*ptr++ - '0');
106 
107  if ((newtmp / 10) != tmp) /* overflow? */
108  {
109  if (errorOK)
110  return false;
111  else
112  ereport(ERROR,
113  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
114  errmsg("value \"%s\" is out of range for type %s",
115  str, "bigint")));
116  }
117  tmp = newtmp;
118  }
119 
120 gotdigits:
121 
122  /* allow trailing whitespace, but not other trailing chars */
123  while (*ptr != '\0' && isspace((unsigned char) *ptr))
124  ptr++;
125 
126  if (*ptr != '\0')
127  {
128  if (errorOK)
129  return false;
130  else
131  ereport(ERROR,
132  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
133  errmsg("invalid input syntax for %s: \"%s\"",
134  "integer", str)));
135  }
136 
137  *result = (sign < 0) ? -tmp : tmp;
138 
139  return true;
140 }
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define ERROR
Definition: elog.h:43
char sign
Definition: informix.c:693
#define PG_INT64_MIN
Definition: c.h:342
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797