trim(const char *input) {
char *rtn;
char *ptr;
+ uint32_t offset = 0;
if (!input)
return NULL;
/* trim right */
ptr = ((char *) input) + strlen(input);
- while (isspace(*--ptr));
- *(++ptr) = '\0';
+ while (isspace(*--ptr))
+ offset++;
- rtn = rtalloc(sizeof(char) * (strlen(input) + 1));
+ rtn = rtalloc(sizeof(char) * (strlen(input) - offset + 1));
if (NULL == rtn) {
fprintf(stderr, _("Not enough memory\n"));
return NULL;
}
- strcpy(rtn, input);
+ strncpy(rtn, input, strlen(input) - offset);
+ rtn[strlen(input) - offset] = '\0';
return rtn;
}
chartrim(const char *input, char *remove) {
char *rtn = NULL;
char *ptr = NULL;
+ uint32_t offset = 0;
if (!input)
return NULL;
/* trim right */
ptr = ((char *) input) + strlen(input);
- while (strchr(remove, *--ptr) != NULL);
- *(++ptr) = '\0';
+ while (strchr(remove, *--ptr) != NULL)
+ offset++;
- rtn = rtalloc(sizeof(char) * (strlen(input) + 1));
+ rtn = rtalloc(sizeof(char) * (strlen(input) - offset + 1));
if (NULL == rtn) {
fprintf(stderr, _("Not enough memory\n"));
return NULL;
}
- strcpy(rtn, input);
+ strncpy(rtn, input, strlen(input) - offset);
+ rtn[strlen(input) - offset] = '\0';
return rtn;
}
) {
char *sql = NULL;
uint32_t len = 0;
+ char *_table = NULL;
+ char *_column = NULL;
assert(table != NULL);
assert(column != NULL);
+ _table = chartrim(table, "\"");
+ _column = chartrim(column, "\"");
+
/* create index */
- len = strlen("CREATE INDEX ON USING gist (st_convexhull());") + 1;
+ len = strlen("CREATE INDEX \"__gist\" ON USING gist (st_convexhull());") + 1;
if (schema != NULL)
len += strlen(schema);
+ len += strlen(_table);
+ len += strlen(_column);
len += strlen(table);
len += strlen(column);
if (tablespace != NULL)
sql = rtalloc(sizeof(char) * len);
if (sql == NULL) {
fprintf(stderr, _("Could not allocate memory for CREATE INDEX statement\n"));
+ rtdealloc(_table);
+ rtdealloc(_column);
return 0;
}
- sprintf(sql, "CREATE INDEX ON %s%s USING gist (st_convexhull(%s))%s%s;",
+ sprintf(sql, "CREATE INDEX \"%s_%s_gist\" ON %s%s USING gist (st_convexhull(%s))%s%s;",
+ _table,
+ _column,
(schema != NULL ? schema : ""),
table,
column,
(tablespace != NULL ? " TABLESPACE " : ""),
(tablespace != NULL ? tablespace : "")
);
+ rtdealloc(_table);
+ rtdealloc(_column);
append_sql_to_buffer(buffer, sql);
rtdealloc(sql);
rtpg_chartrim(const char *input, char *remove) {
char *rtn = NULL;
char *ptr = NULL;
+ uint32_t offset = 0;
if (!input)
return NULL;
/* trim right */
ptr = ((char *) input) + strlen(input);
- while (strchr(remove, *--ptr) != NULL);
- *(++ptr) = '\0';
+ while (strchr(remove, *--ptr) != NULL)
+ offset++;
- rtn = palloc(sizeof(char) * (strlen(input) + 1));
+ rtn = palloc(sizeof(char) * (strlen(input) - offset + 1));
if (rtn == NULL) {
fprintf(stderr, "Not enough memory\n");
return NULL;
}
- strcpy(rtn, input);
+ strncpy(rtn, input, strlen(input) - offset);
+ rtn[strlen(input) - offset] = '\0';
return rtn;
}
rtpg_trim(const char *input) {
char *rtn;
char *ptr;
+ uint32_t offset = 0;
if (!input)
return NULL;
/* trim right */
ptr = ((char *) input) + strlen(input);
- while (isspace(*--ptr));
- *(++ptr) = '\0';
+ while (isspace(*--ptr))
+ offset++;
- rtn = palloc(sizeof(char) * (strlen(input) + 1));
+ rtn = palloc(sizeof(char) * (strlen(input) - offset + 1));
if (rtn == NULL) {
fprintf(stderr, "Not enough memory\n");
return NULL;
}
- strcpy(rtn, input);
+ strncpy(rtn, input, strlen(input) - offset);
+ rtn[strlen(input) - offset] = '\0';
return rtn;
}