MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / get_val

Function get_val

tools/cdb2sql/cdb2sql.cpp:671–772  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

669}
670
671void *get_val(const char **sqlstr, int type, int *vallen)
672{
673 while (isspace(**sqlstr))
674 (*sqlstr)++;
675 const char *str = *sqlstr;
676 const char *end = str;
677 while (*end)
678 end++; // till \0
679 int len = end - str;
680 if (len < 1 || !(*str)) {
681 return NULL;
682 }
683 if (type == CDB2_INTEGER) {
684 int64_t i = atol(str);
685 int64_t *val = (int64_t *) malloc(sizeof(int64_t));
686 *val = i;
687 *vallen = sizeof(*val);
688 return val;
689 } else if (type == CDB2_REAL) {
690 double d = atof(str);
691 double *val = (double *) malloc(sizeof(double));
692 *val = d;
693 *vallen = sizeof(*val);
694 return val;
695 } else if (type == CDB2_CSTRING) {
696 char *val = strndup(str, end - str);
697 *vallen = len;
698 return val;
699 } else if (type == CDB2_DATETIME) {
700 cdb2_client_datetime_t *dt =
701 (cdb2_client_datetime_t *) calloc(sizeof(cdb2_client_datetime_t),
702 1);
703 int rc =
704 sscanf(str, "%04d-%02d-%02dT%02d:%02d:%02d.%03d", &dt->tm.tm_year,
705 &dt->tm.tm_mon, &dt->tm.tm_mday, &dt->tm.tm_hour,
706 &dt->tm.tm_min, &dt->tm.tm_sec, &dt->msec);
707 /* timezone not supported for now */
708 if (rc != 6 && rc != 7) {
709 fprintf(stderr,
710 "Invalid datetime (need format yyyy-mm-ddTHH:MM:SS[.fff], "
711 "rc=%d)\n",
712 rc);
713 return NULL;
714 }
715 dt->tzname[0] = 0;
716 dt->tm.tm_year -= 1900;
717 dt->tm.tm_mon--;
718
719 *vallen = sizeof(*dt);
720 return dt;
721 } else if (type == CDB2_DATETIMEUS) {
722 cdb2_client_datetimeus_t *dt = (cdb2_client_datetimeus_t *)calloc(
723 sizeof(cdb2_client_datetimeus_t), 1);
724 int rc =
725 sscanf(str, "%04d-%02d-%02dT%02d:%02d:%02d.%06d", &dt->tm.tm_year,
726 &dt->tm.tm_mon, &dt->tm.tm_mday, &dt->tm.tm_hour,
727 &dt->tm.tm_min, &dt->tm.tm_sec, &dt->usec);
728 /* timezone not supported for now */

Callers 1

process_bindFunction · 0.70

Calls 4

mallocFunction · 0.85
callocFunction · 0.85
fromhexFunction · 0.70
cdb2_type_strFunction · 0.70

Tested by

no test coverage detected