static struct ccase *gnm_file_casereader_read (struct casereader *, void *);
+
static const struct casereader_class gnm_file_casereader_class =
{
gnm_file_casereader_read,
};
+void
+gnumeric_destroy (struct spreadsheet *s)
+{
+ struct gnumeric_reader *r = s;
+ int i;
+
+ for (i = 0; i < s->n_sheets; ++i)
+ {
+ xmlFree (r->sheets[i].name);
+ }
+
+ free (r->sheets);
+
+ free (r);
+}
+
+
const char *
gnumeric_get_sheet_name (struct spreadsheet *s, int n)
{
static void
gnm_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
{
- int i;
struct gnumeric_reader *r = r_;
if ( r == NULL)
return ;
case_unref (r->first_case);
caseproto_unref (r->proto);
-
- for (i = 0; i < r->spreadsheet.n_sheets; ++i)
- {
- xmlFree (r->sheets[i].name);
- }
-
- free (r->sheets);
-
-
- free (r);
}
+
static void
process_node (struct gnumeric_reader *r)
{
};
-void
-gnumeric_destroy (struct spreadsheet *s)
-{
- gnm_file_casereader_destroy (NULL, s);
-}
-
-
static void
gnumeric_error_handler (void *ctx, const char *mesg,
UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
const char * gnumeric_get_sheet_name (struct spreadsheet *s, int n);
char * gnumeric_get_sheet_range (struct spreadsheet *s, int n);
-void gnumeric_destroy (struct spreadsheet *);
-
struct casereader * gnumeric_make_reader (struct spreadsheet *spreadsheet,
const struct spreadsheet_read_options *opts);
+void gnumeric_destroy (struct spreadsheet *r);
+
#endif
#include "gl/xalloc.h"
static void ods_file_casereader_destroy (struct casereader *, void *);
-
static struct ccase *ods_file_casereader_read (struct casereader *, void *);
+
static const struct casereader_class ods_file_casereader_class =
{
ods_file_casereader_read,
struct spreadsheet spreadsheet;
struct zip_reader *zreader;
xmlTextReaderPtr xtr;
+ int ref_cnt;
enum reader_state state;
int row;
int current_sheet;
xmlChar *current_sheet_name;
- const xmlChar *target_sheet_name;
+ xmlChar *target_sheet_name;
int target_sheet_index;
struct string ods_errs;
};
+void
+ods_destroy (struct spreadsheet *s)
+{
+ struct ods_reader *r = s;
+
+ if (--r->ref_cnt == 0)
+ {
+ int i;
+
+ for (i = 0; i < r->n_allocated_sheets; ++i)
+ {
+ xmlFree (r->sheets[i].name);
+ }
+
+ zip_reader_destroy (r->zreader);
+ free (r->sheets);
+ free (r);
+ }
+}
+
+
static bool
reading_target_sheet (const struct ods_reader *r)
static void
ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
{
- int i;
struct ods_reader *r = r_;
if ( r == NULL)
return ;
caseproto_unref (r->proto);
xmlFree (r->current_sheet_name);
+ xmlFree (r->target_sheet_name);
+
+ ods_destroy (r);
+}
+
- for (i = 0; i < r->n_allocated_sheets; ++i)
- {
- xmlFree (r->sheets[i].name);
- }
- free (r->sheets);
- free (r);
-}
static void
process_node (struct ods_reader *r)
return -1;
mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
- (xmlInputCloseCallback) zip_member_finish,
+ (xmlInputCloseCallback) NULL,
meta, NULL, NULL, 0);
while (1 == xmlTextReaderRead (mxtr))
if ( content == NULL)
return false;
- zip_member_ref (content);
-
if (r->xtr)
xmlFreeTextReader (r->xtr);
+ zip_member_ref (content);
xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
(xmlInputCloseCallback) zip_member_finish,
content, NULL, NULL,
r = xzalloc (sizeof *r);
r->zreader = zr;
+ r->ref_cnt = 1;
if (! init_reader (r, report_errors))
{
assert (r);
r->read_names = opts->read_names;
ds_init_empty (&r->ods_errs);
-
+ ++r->ref_cnt;
if ( !init_reader (r, true))
goto error;
- if ( opts->cell_range )
+ if (opts->cell_range)
{
if ( ! convert_cell_ref (opts->cell_range,
&r->start_col, &r->start_row,
}
r->state = STATE_INIT;
- r->target_sheet_name = BAD_CAST opts->sheet_name;
+ r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
r->target_sheet_index = opts->sheet_index;
r->row = r->col = 0;
break;
}
- // zip_reader_destroy (zreader);
for ( i = 0 ; i < n_var_specs ; ++i )
{
error:
- //zip_reader_destroy (zreader);
-
for ( i = 0 ; i < n_var_specs ; ++i )
{
free (var_spec[i].firstval.type);
r->spreadsheet.dict = NULL;
ods_file_casereader_destroy (NULL, r);
-
return NULL;
}
struct casereader * ods_make_reader (struct spreadsheet *spreadsheet,
const struct spreadsheet_read_options *opts);
+void ods_destroy (struct spreadsheet *s);
+
#endif
#include "spreadsheet-reader.h"
+#include <libpspp/assertion.h>
#include "gnumeric-reader.h"
#include "ods-reader.h"
#include <gl/c-xvasprintf.h>
#include <stdlib.h>
+
void
-spreadsheet_close (UNUSED struct spreadsheet *spreadsheet)
+spreadsheet_destroy (struct spreadsheet *s)
{
+ switch (s->type)
+ {
+ case SPREADSHEET_ODS:
+ ods_destroy (s);
+ break;
+ case SPREADSHEET_GNUMERIC:
+ gnumeric_destroy (s);
+ break;
+ default:
+ NOT_REACHED ();
+ break;
+ }
}
*/
struct spreadsheet_read_options
{
- const char *sheet_name ; /* The name of the sheet to open (in UTF-8) */
+ char *sheet_name ; /* The name of the sheet to open (in UTF-8) */
int sheet_index ; /* The index of the sheet to open (only used if sheet_name is NULL) */
- const char *cell_range ; /* The cell range (in UTF-8) */
+ char *cell_range ; /* The cell range (in UTF-8) */
bool read_names ; /* True if the first row is to be used as the names of the variables */
int asw ; /* The width of string variables in the created dictionary */
};
char *create_cell_ref (int col0, int row0, int coli, int rowi);
-void spreadsheet_close (struct spreadsheet *);
+void spreadsheet_destroy (struct spreadsheet *);
int
cmd_get_data (struct lexer *lexer, struct dataset *ds)
{
+ struct spreadsheet_read_options opts;
char *tok = NULL;
lex_force_match (lexer, T_SLASH);
if (!lex_force_match_id (lexer, "TYPE"))
- return CMD_FAILURE;
+ goto error;
lex_force_match (lexer, T_EQUALS);
char *filename = NULL;
struct casereader *reader = NULL;
struct dictionary *dict = NULL;
- struct spreadsheet_read_options opts;
+
if (!parse_spreadsheet (lexer, &filename, &opts))
goto error;
goto error;
reader = ods_make_reader (spreadsheet, &opts);
dict = spreadsheet->dict;
+ ods_destroy (spreadsheet);
}
free (filename);
destroy_spreadsheet_read_info (&opts);
return CMD_SUCCESS;
}
- destroy_spreadsheet_read_info (&opts);
}
else
msg (SE, _("Unsupported TYPE %s."), tok);
-
-
error:
+ destroy_spreadsheet_read_info (&opts);
free (tok);
return CMD_FAILURE;
}
destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
{
free (opts->cell_range);
+ free (opts->sheet_name);
}