static int setup_columns(struct get_entry_data *data, const char *column_info) { char buf[1024], *q; char *field, *header; struct field_name *f; while(strsep_copy(&column_info, ",", buf, sizeof(buf)) != -1) { q = buf; field = strsep(&q, "="); header = strsep(&q, "="); for(f = field_names; f->fieldname != NULL; f++) { if(strcasecmp(field, f->fieldname) == 0) { add_column(data, f, header); break; } } if(f->fieldname == NULL) { krb5_warnx(context, "unknown field name \"%s\"", field); free_columns(data); return -1; } } return 0; }
static int getit(struct get_options *opt, const char *name, int argc, char **argv) { int i; krb5_error_code ret; struct get_entry_data data; if(opt->long_flag == -1 && (opt->short_flag == 1 || opt->terse_flag == 1)) opt->long_flag = 0; if(opt->short_flag == -1 && (opt->long_flag == 1 || opt->terse_flag == 1)) opt->short_flag = 0; if(opt->terse_flag == -1 && (opt->long_flag == 1 || opt->short_flag == 1)) opt->terse_flag = 0; if(opt->long_flag == 0 && opt->short_flag == 0 && opt->terse_flag == 0) opt->short_flag = 1; if (opt->terse_flag) return listit(name, argc, argv); data.table = NULL; data.chead = NULL; data.ctail = &data.chead; data.mask = 0; data.extra_mask = 0; if(opt->short_flag) { data.table = rtbl_create(); rtbl_set_separator(data.table, " "); data.format = print_entry_short; } else data.format = print_entry_long; if(opt->column_info_string == NULL) { if(opt->long_flag) ret = setup_columns(&data, DEFAULT_COLUMNS_LONG); else ret = setup_columns(&data, DEFAULT_COLUMNS_SHORT); } else ret = setup_columns(&data, opt->column_info_string); if(ret != 0) { if(data.table != NULL) rtbl_destroy(data.table); return 0; } for(i = 0; i < argc; i++) ret = foreach_principal(argv[i], do_get_entry, name, &data); if(data.table != NULL) { rtbl_format(data.table, stdout); rtbl_destroy(data.table); } free_columns(&data); return ret != 0; }
void free_table( struct table *table ) { if (!table) return; clear_table( table ); if (table->flags & TABLE_FLAG_DYNAMIC) { TRACE("destroying %p\n", table); heap_free( (WCHAR *)table->name ); free_columns( (struct column *)table->columns, table->num_cols ); list_remove( &table->entry ); heap_free( table ); } }
/* * Release memory used by a result structure */ int free_result(db_res_t* _r) { if (!_r) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:free_result: Invalid parameter\n"); #endif return -1; } free_columns(_r); free_rows(_r); pkg_free(_r); return 0; }
static void eti_dispose (GObject *object) { GalA11yETableItem *a11y = GAL_A11Y_E_TABLE_ITEM (object); GalA11yETableItemPrivate *priv = GET_PRIVATE (a11y); if (priv->columns) { free_columns (priv->columns); priv->columns = NULL; } if (priv->item) { g_object_weak_unref (G_OBJECT (priv->item), item_finalized, a11y); priv->item = NULL; } if (parent_class->dispose) parent_class->dispose (object); }
/* * Fill the structure with data from database */ int convert_result(db_con_t* _h, db_res_t* _r) { if ((!_h) || (!_r)) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:convert_result: Invalid parameter\n"); #endif return -1; } if (get_columns(_h, _r) < 0) { LOG(L_ERR, "DBT:convert_result: Error while getting column names\n"); return -2; } if (convert_rows(_h, _r) < 0) { LOG(L_ERR, "DBT:convert_result: Error while converting rows\n"); free_columns(_r); return -3; } return 0; }
/* * 1. Check what actually happened: column reorder, remove or add * 2. Update cache * 3. Emit signals */ static void eti_header_structure_changed (ETableHeader *eth, AtkObject *a11y) { gboolean reorder_found = FALSE, added_found = FALSE, removed_found = FALSE; GalA11yETableItem * a11y_item; ETableCol ** cols, **prev_cols; GalA11yETableItemPrivate *priv; gint *state = NULL, *prev_state = NULL, *reorder = NULL; gint i,j,n_rows,n_cols, prev_n_cols; a11y_item = GAL_A11Y_E_TABLE_ITEM (a11y); priv = GET_PRIVATE (a11y_item); /* Assume rows do not changed. */ n_rows = priv->rows; prev_n_cols = priv->cols; prev_cols = priv->columns; cols = e_table_header_get_columns (eth); n_cols = eth->col_count; g_return_if_fail (cols && prev_cols && n_cols > 0); /* Init to ETI_HEADER_UNCHANGED. */ state = g_malloc0 (sizeof (gint) * (MAX (prev_n_cols, n_cols) + 1)); prev_state = g_malloc0 (sizeof (gint) * (MAX (prev_n_cols, n_cols) + 1)); reorder = g_malloc0 (sizeof (gint) * (MAX (prev_n_cols, n_cols) + 1)); /* Compare with previously saved column headers. */ for (i = 0; i < n_cols && cols[i]; i++) { for (j = 0; j < prev_n_cols && prev_cols[j]; j++) { if (prev_cols[j] == cols[i] && i != j) { reorder_found = TRUE; state[i] = ETI_HEADER_REORDERED; reorder[i] = j; break; } else if (prev_cols[j] == cols[i]) { /* OK, this column is not changed. */ break; } } /* cols[i] is new added column. */ if (j == prev_n_cols) { added_found = TRUE; state[i] = ETI_HEADER_NEW_ADDED; } } /* Now try to find if there are removed columns. */ for (i = 0; i < prev_n_cols && prev_cols[i]; i++) { for (j = 0; j < n_cols && cols[j]; j++) if (prev_cols[j] == cols[i]) break; /* Removed columns found. */ if (j == n_cols) { removed_found = TRUE; prev_state[j] = ETI_HEADER_REMOVED; } } /* If nothing interesting just return. */ if (!reorder_found && !added_found && !removed_found) return; /* Emit signals */ if (reorder_found) g_signal_emit_by_name (a11y_item, "column_reordered"); if (removed_found) { for (i = 0; i < prev_n_cols; i++) { if (prev_state[i] == ETI_HEADER_REMOVED) { g_signal_emit_by_name ( a11y_item, "column-deleted", i, 1); for (j = 0; j < n_rows; j++) g_signal_emit_by_name ( a11y_item, "children_changed::remove", ((j + 1) * prev_n_cols + i), NULL, NULL); } } } if (added_found) { for (i = 0; i < n_cols; i++) { if (state[i] == ETI_HEADER_NEW_ADDED) { g_signal_emit_by_name ( a11y_item, "column-inserted", i, 1); for (j = 0; j < n_rows; j++) g_signal_emit_by_name ( a11y_item, "children_changed::add", ((j + 1) * n_cols + i), NULL, NULL); } } } priv->cols = n_cols; g_free (state); g_free (reorder); g_free (prev_state); free_columns (priv->columns); priv->columns = cols; }
int main (int argc, char *argv[]) { /* the MAIN function simply perform arguments checking */ sqlite3 *handle; int i; int next_arg = ARG_NONE; const char *gml_path = NULL; const char *db_path = NULL; const char *table = NULL; int in_memory = 0; int error = 0; char Buff[BUFFSIZE]; int done = 0; int len; XML_Parser parser; FILE *xml_file; struct gml_params params; int ret; char *err_msg = NULL; void *cache; params.db_handle = NULL; params.stmt = NULL; params.geometry_type = GEOM_NONE; params.srid = INT_MIN; params.is_feature = 0; params.is_fid = 0; params.is_point = 0; params.is_linestring = 0; params.is_polygon = 0; params.is_multi_point = 0; params.is_multi_linestring = 0; params.is_multi_polygon = 0; params.first = NULL; params.last = NULL; params.geometry = NULL; params.polygon.exterior = NULL; params.polygon.first = NULL; params.polygon.last = NULL; params.CharDataStep = 65536; params.CharDataMax = params.CharDataStep; params.CharData = malloc (params.CharDataStep); for (i = 1; i < argc; i++) { /* parsing the invocation arguments */ if (next_arg != ARG_NONE) { switch (next_arg) { case ARG_GML_PATH: gml_path = argv[i]; break; case ARG_DB_PATH: db_path = argv[i]; break; case ARG_TABLE: table = argv[i]; break; }; next_arg = ARG_NONE; continue; } if (strcasecmp (argv[i], "--help") == 0 || strcmp (argv[i], "-h") == 0) { do_help (); return -1; } if (strcmp (argv[i], "-g") == 0) { next_arg = ARG_GML_PATH; continue; } if (strcasecmp (argv[i], "--gml-path") == 0) { next_arg = ARG_GML_PATH; continue; } if (strcmp (argv[i], "-d") == 0) { next_arg = ARG_DB_PATH; continue; } if (strcasecmp (argv[i], "--db-path") == 0) { next_arg = ARG_DB_PATH; continue; } if (strcmp (argv[i], "-t") == 0) { next_arg = ARG_TABLE; continue; } if (strcasecmp (argv[i], "--table-name") == 0) { next_arg = ARG_TABLE; continue; } if (strcasecmp (argv[i], "-m") == 0) { in_memory = 1; next_arg = ARG_NONE; continue; } if (strcasecmp (argv[i], "-in-memory") == 0) { in_memory = 1; next_arg = ARG_NONE; continue; } if (strcasecmp (argv[i], "-n") == 0) { next_arg = ARG_NONE; continue; } if (strcasecmp (argv[i], "-no-spatial-index") == 0) { next_arg = ARG_NONE; continue; } fprintf (stderr, "unknown argument: %s\n", argv[i]); error = 1; } if (error) { do_help (); return -1; } /* checking the arguments */ if (!gml_path) { fprintf (stderr, "did you forget setting the --gml-path argument ?\n"); error = 1; } if (!db_path) { fprintf (stderr, "did you forget setting the --db-path argument ?\n"); error = 1; } if (!table) { fprintf (stderr, "did you forget setting the --table-name argument ?\n"); error = 1; } if (error) { do_help (); return -1; } /* opening the DB */ cache = spatialite_alloc_connection (); open_db (db_path, &handle, cache); if (!handle) return -1; params.db_handle = handle; if (in_memory) { /* loading the DB in-memory */ sqlite3 *mem_db_handle; sqlite3_backup *backup; int ret; ret = sqlite3_open_v2 (":memory:", &mem_db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open 'MEMORY-DB': %s\n", sqlite3_errmsg (mem_db_handle)); sqlite3_close (mem_db_handle); return -1; } backup = sqlite3_backup_init (mem_db_handle, "main", handle, "main"); if (!backup) { fprintf (stderr, "cannot load 'MEMORY-DB'\n"); sqlite3_close (handle); sqlite3_close (mem_db_handle); return -1; } while (1) { ret = sqlite3_backup_step (backup, 1024); if (ret == SQLITE_DONE) break; } ret = sqlite3_backup_finish (backup); sqlite3_close (handle); handle = mem_db_handle; printf ("\nusing IN-MEMORY database\n"); spatialite_cleanup_ex (cache); cache = spatialite_alloc_connection (); spatialite_init_ex (handle, cache, 0); } /* XML parsing */ xml_file = fopen (gml_path, "rb"); if (!xml_file) { fprintf (stderr, "cannot open %s\n", gml_path); sqlite3_close (handle); return -1; } parser = XML_ParserCreate (NULL); if (!parser) { fprintf (stderr, "Couldn't allocate memory for parser\n"); sqlite3_close (handle); return -1; } XML_SetUserData (parser, ¶ms); /* XML parsing: pass I */ XML_SetElementHandler (parser, start1_tag, end1_tag); XML_SetCharacterDataHandler (parser, gmlCharData); while (!done) { len = fread (Buff, 1, BUFFSIZE, xml_file); if (ferror (xml_file)) { fprintf (stderr, "XML Read error\n"); sqlite3_close (handle); return -1; } done = feof (xml_file); if (!XML_Parse (parser, Buff, len, done)) { fprintf (stderr, "Parse error at line %d:\n%s\n", (int) XML_GetCurrentLineNumber (parser), XML_ErrorString (XML_GetErrorCode (parser))); sqlite3_close (handle); return -1; } } XML_ParserFree (parser); /* creating the DB table */ if (!create_table (¶ms, table)) goto no_table; /* XML parsing: pass II */ parser = XML_ParserCreate (NULL); if (!parser) { fprintf (stderr, "Couldn't allocate memory for parser\n"); sqlite3_close (handle); return -1; } XML_SetUserData (parser, ¶ms); XML_SetElementHandler (parser, start2_tag, end2_tag); XML_SetCharacterDataHandler (parser, gmlCharData); rewind (xml_file); params.is_feature = 0; params.is_fid = 0; params.is_point = 0; params.is_linestring = 0; params.is_polygon = 0; params.is_multi_point = 0; params.is_multi_linestring = 0; params.is_multi_polygon = 0; done = 0; while (!done) { len = fread (Buff, 1, BUFFSIZE, xml_file); if (ferror (xml_file)) { fprintf (stderr, "XML Read error\n"); sqlite3_close (handle); return -1; } done = feof (xml_file); if (!XML_Parse (parser, Buff, len, done)) { fprintf (stderr, "Parse error at line %d:\n%s\n", (int) XML_GetCurrentLineNumber (parser), XML_ErrorString (XML_GetErrorCode (parser))); sqlite3_close (handle); return -1; } } XML_ParserFree (parser); if (params.stmt != NULL) sqlite3_finalize (params.stmt); params.stmt = NULL; /* COMMITTing the still pending Transaction */ ret = sqlite3_exec (handle, "COMMIT", NULL, NULL, &err_msg); if (ret != SQLITE_OK) { fprintf (stderr, "COMMIT TRANSACTION error: %s\n", err_msg); sqlite3_free (err_msg); return 0; } no_table: free_columns (¶ms); fclose (xml_file); if (in_memory) { /* exporting the in-memory DB to filesystem */ sqlite3 *disk_db_handle; sqlite3_backup *backup; int ret; printf ("\nexporting IN_MEMORY database ... wait please ...\n"); ret = sqlite3_open_v2 (db_path, &disk_db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if (ret != SQLITE_OK) { fprintf (stderr, "cannot open '%s': %s\n", db_path, sqlite3_errmsg (disk_db_handle)); sqlite3_close (disk_db_handle); return -1; } backup = sqlite3_backup_init (disk_db_handle, "main", handle, "main"); if (!backup) { fprintf (stderr, "Backup failure: 'MEMORY-DB' wasn't saved\n"); sqlite3_close (handle); sqlite3_close (disk_db_handle); return -1; } while (1) { ret = sqlite3_backup_step (backup, 1024); if (ret == SQLITE_DONE) break; } ret = sqlite3_backup_finish (backup); sqlite3_close (handle); handle = disk_db_handle; printf ("\tIN_MEMORY database succesfully exported\n"); } sqlite3_close (handle); spatialite_cleanup_ex (cache); return 0; }