int parse_sql_update(const char *sql, char **table, column_info **cols, int *ncols, char **where) { int nprefix, n; int has_where; char *prefix; char *p, *w, *c, *t; char **token, **itoken; prefix = "UPDATE"; nprefix = strlen(prefix); if (G_strncasecmp(sql, prefix, nprefix) != 0) return 1; p = (char *) sql + nprefix; /* skip 'UPDATE' */ if (*p != ' ') return 1; p++; /* table */ t = strchr(p, ' '); n = t - p; *table = G_malloc(n + 1); strncpy(*table, p, n); (*table)[n] = '\0'; G_strip(*table); p += n; if (*p != ' ') return 1; p++; if (G_strncasecmp(p, "SET", 3) != 0) return 1; p += 3; /* skip 'SET' */ if (*p != ' ') return 1; p++; w = G_strcasestr(p, "WHERE"); if (!w) { has_where = FALSE; w = (char *)sql + strlen(sql); } else { has_where = TRUE; } /* process columns & values */ n = w - p; c = G_malloc(n + 1); strncpy(c, p, n); c[n] = '\0'; token = G_tokenize2(c, ",", "'"); *ncols = G_number_of_tokens(token); *cols = (column_info *)G_malloc(sizeof(column_info) * (*ncols)); for (n = 0; n < (*ncols); n++) { itoken = G_tokenize(token[n], "="); if (G_number_of_tokens(itoken) != 2) return FALSE; G_strip(itoken[0]); G_strip(itoken[1]); (*cols)[n].name = G_store(itoken[0]); (*cols)[n].value = G_store(itoken[1]); G_free_tokens(itoken); } G_free_tokens(token); G_free(c); if (!has_where) { *where = NULL; return 0; } /* where */ w += strlen("WHERE"); if (*w != ' ') return 1; w++; G_strip(w); *where = G_store(w); return 0; }
char *get_datasource_name(const char *opt_dsn, int use_ogr) { char *dsn; if (G_strncasecmp(opt_dsn, "PG:", 3) == 0) { /* PostgreSQL/PostGIS */ size_t i; char connect_str[DB_SQL_MAX], database[GNAME_MAX]; char *p, *pp; const char *user, *passwd, *host, *port; /* dbname is mandatory */ p = G_strcasestr(opt_dsn, "dbname"); if (!p) G_fatal_error(_("Invalid connection string (dbname missing)")); /* get dbname */ p += strlen("dbname="); for (i = 0, pp = p; *pp != ' ' && *pp != '\0'; pp++, i++) database[i] = *pp; database[i] = '\0'; /* build connection string */ sprintf(connect_str, "dbname=%s", database); /* add db.login settings (user, password, host, port) */ if (DB_OK == db_get_login2("pg", database, &user, &passwd, &host, &port)) { if (user) { if (!G_strcasestr(opt_dsn, "user="******" user="******"password="******" password="******"host=")) { strcat(connect_str, " host="); strcat(connect_str, host); } G_free((char *)host); } if (port) { if (!G_strcasestr(opt_dsn, "port=")) { strcat(connect_str, " port="); strcat(connect_str, port); } G_free((char *)port); } } if (!use_ogr) /* be friendly, ignored 'PG:' prefix for PostGIS links */ dsn = G_store(connect_str); else G_asprintf(&dsn, "PG:%s", connect_str); } else { /* other datasources */ dsn = G_store(opt_dsn); } G_debug(1, "dsn: %s", dsn); return dsn; }
// read, parse and set values of scanning variables when new input from stdin comes void read_new_input(char* &routput, double &zrange_min, double &zrange_max, double &clip_N, double &clip_S, double &clip_E, double &clip_W, double &trim_tolerance, double &rotate, double &zexag, char* &method, int &numscan, double &smooth, double &resolution, bool &use_equalized, struct Cell_head &window, double &offset, bool ®ion3D, char* &color_output, char* &voutput, char * &ply, char* &contours_output, double &contours_step, int &draw_type, int &draw_threshold, char* &draw_output, bool &paused) { char buf[200]; char **tokens; char **tokens2; int line; // TODO: this leaks for (line = 1;; line++) { if (G_getl2(buf, 200, stdin)) { if(buf[0] == '\0') break; tokens = G_tokenize(buf, "="); if (strcmp(tokens[0], "resolution") == 0) resolution = atof(tokens[1]); else if (strcmp(tokens[0], "smooth_radius") == 0) smooth = atof(tokens[1]); else if (strcmp(tokens[0], "output") == 0) { if (tokens[1][0] == '\0') routput = NULL; else routput = G_store(tokens[1]); } else if (strcmp(tokens[0], "zrange") == 0) { tokens2 = G_tokenize(tokens[1], ","); zrange_min = atof(tokens2[0])/100; zrange_max = atof(tokens2[1])/100; G_free_tokens(tokens2); } else if (strcmp(tokens[0], "trim") == 0) { tokens2 = G_tokenize(tokens[1], ","); clip_N = atof(tokens2[0])/100; clip_S = atof(tokens2[1])/100; clip_E = atof(tokens2[2])/100; clip_W = atof(tokens2[3])/100; G_free_tokens(tokens2); } else if (strcmp(tokens[0], "trim_tolerance") == 0) trim_tolerance = atof(tokens[1]); else if (strcmp(tokens[0], "rotate") == 0) rotate = pcl::deg2rad(atof(tokens[1]) + 180); else if (strcmp(tokens[0], "zexag") == 0) zexag = atof(tokens[1]); else if (strcmp(tokens[0], "method") == 0) method = G_store(tokens[1]); else if (strcmp(tokens[0], "numscan") == 0) numscan = atoi(tokens[1]); else if (strcmp(tokens[0], "region") == 0) update_input_region(NULL, tokens[1], window, offset, region3D); else if (strcmp(tokens[0], "raster") == 0) update_input_region(tokens[1], NULL, window, offset, region3D); // export else if (strcmp(tokens[0], "color_output") == 0) { if (tokens[1][0] == '\0') color_output = NULL; else color_output = G_store(tokens[1]); } else if (strcmp(tokens[0], "vector") == 0) { if (tokens[1][0] == '\0') voutput = NULL; else voutput = G_store(tokens[1]); } else if (strcmp(tokens[0], "ply") == 0) { if (tokens[1][0] == '\0') ply = NULL; else ply = G_store(tokens[1]); } else if (strcmp(tokens[0], "flags") == 0) { if (G_strcasestr(tokens[1], "e")) use_equalized = true; else use_equalized = false; } // contours else if (strcmp(tokens[0], "contours") == 0) { if (tokens[1][0] == '\0') contours_output = NULL; else contours_output = G_store(tokens[1]); } else if (strcmp(tokens[0], "contours_step") == 0) contours_step = atof(tokens[1]); // drawing else if (strcmp(tokens[0], "draw") == 0) get_draw_type(tokens[1], draw_type); else if (strcmp(tokens[0], "draw_threshold") == 0) draw_threshold = atoi(tokens[1]); else if (strcmp(tokens[0], "draw_output") == 0) { if (tokens[1][0] == '\0') draw_output = NULL; else draw_output = G_store(tokens[1]); } else if (strcmp(tokens[0], "pause") == 0) { paused = true; } else if (strcmp(tokens[0], "resume") == 0) { paused = false; } G_free_tokens(tokens); } else break; } }