better utilize glib functions, fix memory leaks

This commit is contained in:
whydoubt
2004-05-30 07:19:22 +00:00
parent 59f221b2b2
commit a5afa5c9e4
9 changed files with 67 additions and 47 deletions

View File

@@ -5,6 +5,14 @@ Sun May 30 00:04:55 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
* src/libmdb/table.c: malloc to g_malloc * src/libmdb/table.c: malloc to g_malloc
* src/sql/main.c: * src/sql/main.c:
* src/sql/mdbsql.c: better utilize glib functions, fix memory leak * src/sql/mdbsql.c: better utilize glib functions, fix memory leak
* src/util/mdb-array.c:
* src/util/mdb-check.c:
* src/util/mdb-export.c:
* src/util/mdb-import.c:
* src/util/mdb-schema.c:
* src/util/mdb-sql.c:
* src/util/mdb-tables.c:
* src/util/sargtest.c: better utilize glib functions, fix memory leaks
Sat May 29 14:14:21 CDT 2004 Jeff Smith <whydoubt@yahoo.com> Sat May 29 14:14:21 CDT 2004 Jeff Smith <whydoubt@yahoo.com>
* src/libmdb/backend.c: * src/libmdb/backend.c:

View File

@@ -65,7 +65,7 @@ int started;
for (j = 0; j < table->num_cols; j++) for (j = 0; j < table->num_cols; j++)
{ {
bound_values [j] = (char *) malloc (MDB_BIND_SIZE); bound_values [j] = (char *) g_malloc (MDB_BIND_SIZE);
bound_values [j] [0] = '\0'; bound_values [j] [0] = '\0';
mdb_bind_column (table, j + 1, bound_values [j]); mdb_bind_column (table, j + 1, bound_values [j]);
} }
@@ -118,7 +118,7 @@ int started;
} }
for (j = 0; j < table->num_cols; j++) for (j = 0; j < table->num_cols; j++)
{ {
free (bound_values [j]); g_free (bound_values [j]);
} }
} }
} }

View File

@@ -55,8 +55,7 @@ int opt;
while ((opt=getopt(argc, argv, "T:"))!=-1) { while ((opt=getopt(argc, argv, "T:"))!=-1) {
switch (opt) { switch (opt) {
case 'T': case 'T':
tabname = (char *) malloc(strlen(optarg)+1); tabname = (char *) g_strdup(optarg);
strcpy(tabname, optarg);
break; break;
} }
} }
@@ -101,6 +100,7 @@ int opt;
} }
} }
g_free(tabname);
mdb_close (mdb); mdb_close (mdb);
mdb_exit(); mdb_exit();

View File

@@ -57,8 +57,8 @@ main(int argc, char **argv)
/* doesn't handle tables > 256 columns. Can that happen? */ /* doesn't handle tables > 256 columns. Can that happen? */
char *bound_values[256]; char *bound_values[256];
int bound_lens[256]; int bound_lens[256];
char *delimiter = ","; char *delimiter = NULL;
char *row_delimiter = "\n"; char *row_delimiter = NULL;
char header_row = 1; char header_row = 1;
char quote_text = 1; char quote_text = 1;
char insert_statements = 0; char insert_statements = 0;
@@ -74,12 +74,10 @@ main(int argc, char **argv)
quote_text = 0; quote_text = 0;
break; break;
case 'd': case 'd':
delimiter = (char *) malloc(strlen(optarg)+1); delimiter = (char *) g_strdup(optarg);
strcpy(delimiter, optarg);
break; break;
case 'R': case 'R':
row_delimiter = (char *) malloc(strlen(optarg)+1); row_delimiter = (char *) g_strdup(optarg);
strcpy(row_delimiter, optarg);
break; break;
case 'I': case 'I':
insert_statements = 1; insert_statements = 1;
@@ -95,6 +93,12 @@ main(int argc, char **argv)
break; break;
} }
} }
if (!delimiter) {
delimiter = (char *) g_strdup(",");
}
if (!row_delimiter) {
row_delimiter = (char *) g_strdup("\n");
}
/* /*
** optind is now the position of the first non-option arg, ** optind is now the position of the first non-option arg,
@@ -133,7 +137,7 @@ main(int argc, char **argv)
mdb_rewind_table(table); mdb_rewind_table(table);
for (j=0;j<table->num_cols;j++) { for (j=0;j<table->num_cols;j++) {
bound_values[j] = (char *) malloc(MDB_BIND_SIZE); bound_values[j] = (char *) g_malloc(MDB_BIND_SIZE);
bound_values[j][0] = '\0'; bound_values[j][0] = '\0';
mdb_bind_column(table, j+1, bound_values[j]); mdb_bind_column(table, j+1, bound_values[j]);
mdb_bind_len(table, j+1, &bound_lens[j]); mdb_bind_len(table, j+1, &bound_lens[j]);
@@ -191,12 +195,14 @@ main(int argc, char **argv)
fprintf(stdout,"%s", row_delimiter); fprintf(stdout,"%s", row_delimiter);
} }
for (j=0;j<table->num_cols;j++) { for (j=0;j<table->num_cols;j++) {
free(bound_values[j]); g_free(bound_values[j]);
} }
mdb_free_tabledef(table); mdb_free_tabledef(table);
} }
} }
g_free (delimiter);
g_free (row_delimiter);
mdb_close(mdb); mdb_close(mdb);
mdb_exit(); mdb_exit();

View File

@@ -22,9 +22,6 @@
#define MAX_ROW_SIZE 4096 #define MAX_ROW_SIZE 4096
char *delimiter = ",";
char header_rows = 0;
void void
free_values(MdbField *fields, int num_fields) free_values(MdbField *fields, int num_fields)
{ {
@@ -107,7 +104,7 @@ convert_field(MdbColumn *col, char *s, MdbField *field)
return 0; return 0;
} }
int int
prep_row(MdbTableDef *table, unsigned char *line, MdbField *fields) prep_row(MdbTableDef *table, unsigned char *line, MdbField *fields, char *delim)
{ {
MdbColumn *col; MdbColumn *col;
char delims[20]; char delims[20];
@@ -117,7 +114,7 @@ prep_row(MdbTableDef *table, unsigned char *line, MdbField *fields)
/* /*
* pull apart the row and turn it into something respectable * pull apart the row and turn it into something respectable
*/ */
sprintf(delims, "%c\n", delimiter[0]); sprintf(delims, "%c\n", delim[0]);
s = strtok(line, delims); s = strtok(line, delims);
file_cols = 0; file_cols = 0;
do { do {
@@ -162,6 +159,8 @@ main(int argc, char **argv)
int opt; int opt;
char *s; char *s;
FILE *in; FILE *in;
char *delimiter = NULL;
char header_rows = 0;
while ((opt=getopt(argc, argv, "H:d:"))!=-1) { while ((opt=getopt(argc, argv, "H:d:"))!=-1) {
switch (opt) { switch (opt) {
@@ -169,13 +168,15 @@ main(int argc, char **argv)
header_rows = atol(optarg); header_rows = atol(optarg);
break; break;
case 'd': case 'd':
delimiter = (char *) malloc(strlen(optarg)+1); delimiter = (char *) g_strdup(optarg);
strcpy(delimiter, optarg);
break; break;
default: default:
break; break;
} }
} }
if (!delimiter) {
delimiter = (char *) g_strdup(",");
}
/* /*
** optind is now the position of the first non-option arg, ** optind is now the position of the first non-option arg,
@@ -225,7 +226,7 @@ main(int argc, char **argv)
row = 1; row = 1;
while (fgets(line, MAX_ROW_SIZE, in)) { while (fgets(line, MAX_ROW_SIZE, in)) {
num_fields = prep_row(table, line, fields); num_fields = prep_row(table, line, fields, delimiter);
if (!num_fields) { if (!num_fields) {
fprintf(stderr, "Aborting import at row %d\n", row); fprintf(stderr, "Aborting import at row %d\n", row);
exit(1); exit(1);
@@ -236,6 +237,7 @@ main(int argc, char **argv)
mdb_insert_row(table, num_fields, fields); mdb_insert_row(table, num_fields, fields);
} }
g_free(delimiter);
fclose(in); fclose(in);
mdb_close(mdb); mdb_close(mdb);
mdb_exit(); mdb_exit();

View File

@@ -35,7 +35,7 @@ main (int argc, char **argv)
MdbCatalogEntry *entry; MdbCatalogEntry *entry;
char *the_relation; char *the_relation;
char *tabname = NULL; char *tabname = NULL;
char *namespace = ""; char *namespace = NULL;
int s = 0; int s = 0;
int opt; int opt;
@@ -51,18 +51,19 @@ main (int argc, char **argv)
while ((opt=getopt(argc, argv, "T:N:S:"))!=-1) { while ((opt=getopt(argc, argv, "T:N:S:"))!=-1) {
switch (opt) { switch (opt) {
case 'T': case 'T':
tabname = (char *) malloc(strlen(optarg)+1); tabname = (char *) g_strdup(optarg);
strcpy(tabname, optarg);
break; break;
case 'N': case 'N':
namespace = (char *) malloc(strlen(optarg)+1); namespace = (char *) g_strdup(optarg);
strcpy(namespace, optarg);
break; break;
case 'S': case 'S':
s = 1; s = 1;
break; break;
} }
} }
if (!namespace) {
namespace = (char *) g_strdup("");
}
mdb_init(); mdb_init();
@@ -119,7 +120,8 @@ main (int argc, char **argv)
the_relation=mdb_get_relationships(mdb); the_relation=mdb_get_relationships(mdb);
} }
g_free(namespace);
g_free(tabname);
mdb_close (mdb); mdb_close (mdb);
mdb_exit(); mdb_exit();

View File

@@ -331,7 +331,7 @@ dump_results_pp(MdbSQL *sql)
/* clean up */ /* clean up */
for (j=0;j<sql->num_columns;j++) { for (j=0;j<sql->num_columns;j++) {
if (sql->bound_values[j]) free(sql->bound_values[j]); g_free(sql->bound_values[j]);
} }
/* the column and table names are no good now */ /* the column and table names are no good now */
@@ -340,7 +340,7 @@ dump_results_pp(MdbSQL *sql)
void myexit(int r) void myexit(int r)
{ {
free(delimiter); g_free(delimiter);
exit(r); exit(r);
} }
int int
@@ -360,10 +360,9 @@ char *histpath;
if (home) { if (home) {
histpath = (char *)malloc(strlen(home) + strlen(HISTFILE) + 2); histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
sprintf(histpath,"%s/%s",home,HISTFILE);
read_history(histpath); read_history(histpath);
free(histpath); g_free(histpath);
} }
if (!isatty(fileno(stdin))) { if (!isatty(fileno(stdin))) {
in = stdin; in = stdin;
@@ -371,8 +370,7 @@ char *histpath;
while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) { while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
delimiter = malloc(strlen(optarg)+1); delimiter = (char *) g_strdup(optarg);
strcpy(delimiter, optarg);
break; break;
case 'p': case 'p':
pretty_print=0; pretty_print=0;
@@ -403,7 +401,7 @@ char *histpath;
} }
} }
if (!delimiter) { if (!delimiter) {
delimiter = strdup("\t"); delimiter = (char *) g_strdup("\t");
} }
/* initialize the SQL engine */ /* initialize the SQL engine */
sql = mdb_sql_init(); sql = mdb_sql_init();
@@ -413,12 +411,16 @@ char *histpath;
/* give the buffer an initial size */ /* give the buffer an initial size */
bufsz = 4096; bufsz = 4096;
mybuf = (char *) malloc(bufsz); mybuf = (char *) g_malloc(bufsz);
mybuf[0]='\0'; mybuf[0]='\0';
if (in) { if (in) {
s=malloc(256); s=malloc(256);
if (!fgets(s, 256, in)) myexit(0); if (!fgets(s, 256, in)) {
if (s) free (s);
g_free (mybuf);
myexit(0);
}
} else { } else {
sprintf(prompt,"1 => "); sprintf(prompt,"1 => ");
s=readline(prompt); s=readline(prompt);
@@ -457,6 +459,8 @@ char *histpath;
/* if we have something in the buffer, run it */ /* if we have something in the buffer, run it */
if (strlen(mybuf)) if (strlen(mybuf))
run_query(sql, mybuf); run_query(sql, mybuf);
if (s) free (s);
g_free (mybuf);
myexit(0); myexit(0);
} }
if (s[strlen(s)-1]=='\n') s[strlen(s)-1]=0; if (s[strlen(s)-1]=='\n') s[strlen(s)-1]=0;
@@ -471,14 +475,13 @@ char *histpath;
} }
mdb_sql_exit(sql); mdb_sql_exit(sql);
free(mybuf); g_free(mybuf);
if (s) free(s); if (s) free(s);
if (home) { if (home) {
histpath = (char *)malloc(strlen(home) + strlen(HISTFILE) + 2); histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
sprintf(histpath,"%s/%s",home,HISTFILE);
write_history(histpath); write_history(histpath);
free(histpath); g_free(histpath);
} }
myexit(0); myexit(0);

View File

@@ -110,8 +110,7 @@ main (int argc, char **argv)
} }
break; break;
case 'd': case 'd':
delimiter = (char *) malloc(strlen(optarg)+1); delimiter = (char *) g_strdup(optarg);
strcpy(delimiter, optarg);
break; break;
} }
} }
@@ -155,7 +154,7 @@ main (int argc, char **argv)
mdb_close(mdb); mdb_close(mdb);
mdb_exit(); mdb_exit();
if (delimiter) free(delimiter); g_free(delimiter);
exit(0); exit(0);
} }

View File

@@ -76,7 +76,7 @@ MdbSarg sarg;
mdb_rewind_table(table); mdb_rewind_table(table);
for (j=0;j<table->num_cols;j++) { for (j=0;j<table->num_cols;j++) {
bound_values[j] = (char *) malloc(MDB_BIND_SIZE); bound_values[j] = (char *) g_malloc(MDB_BIND_SIZE);
bound_values[j][0] = '\0'; bound_values[j][0] = '\0';
mdb_bind_column(table, j+1, bound_values[j]); mdb_bind_column(table, j+1, bound_values[j]);
} }
@@ -102,6 +102,6 @@ MdbSarg sarg;
/* clean up */ /* clean up */
for (j=0;j<table->num_cols;j++) { for (j=0;j<table->num_cols;j++) {
free(bound_values[j]); g_free(bound_values[j]);
} }
} }