X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-case-file.c;h=bb6f8b5545ca72efd83ff461cf5f5e03e9799fd3;hb=32884aab5da9ba8455d482dc1f52989ff7de4ee6;hp=7e16087eb93ae7bfe2057d72fdc49cb3d9e48136;hpb=0fa141762183890ebd139ccd9264f08db9011539;p=pspp-builds.git diff --git a/src/ui/gui/psppire-case-file.c b/src/ui/gui/psppire-case-file.c index 7e16087e..bb6f8b55 100644 --- a/src/ui/gui/psppire-case-file.c +++ b/src/ui/gui/psppire-case-file.c @@ -1,39 +1,37 @@ -/* - PSPPIRE --- A Graphical User Interface for PSPP - Copyright (C) 2006 Free Software Foundation - Written by John Darrington - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2006 Free Software Foundation + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ #include #include #include -#include "psppire-object.h" #include "psppire-case-file.h" #include #include -#include -#include #include +#include +#include #include #include +#include "xalloc.h" +#include "xmalloca.h" + /* --- prototypes --- */ static void psppire_case_file_class_init (PsppireCaseFileClass *class); static void psppire_case_file_init (PsppireCaseFile *case_file); @@ -43,12 +41,12 @@ static void psppire_case_file_finalize (GObject *object); /* --- variables --- */ static GObjectClass *parent_class = NULL; -enum {CASE_CHANGED, +enum {CASE_CHANGED, CASE_INSERTED, CASES_DELETED, n_SIGNALS}; -static guint signal[n_SIGNALS]; +static guint signals [n_SIGNALS]; /* --- functions --- */ @@ -75,7 +73,7 @@ psppire_case_file_get_type (void) (GInstanceInitFunc) psppire_case_file_init, }; - object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT, "PsppireCaseFile", + object_type = g_type_register_static (G_TYPE_OBJECT, "PsppireCaseFile", &object_info, 0); } @@ -92,38 +90,38 @@ psppire_case_file_class_init (PsppireCaseFileClass *class) object_class->finalize = psppire_case_file_finalize; - signal[CASE_CHANGED] = - g_signal_new ("case_changed", - G_TYPE_FROM_CLASS(class), + signals [CASE_CHANGED] = + g_signal_new ("case-changed", + G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__INT, - G_TYPE_NONE, + G_TYPE_NONE, 1, G_TYPE_INT); - signal[CASE_INSERTED] = - g_signal_new ("case_inserted", - G_TYPE_FROM_CLASS(class), + signals [CASE_INSERTED] = + g_signal_new ("case-inserted", + G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__INT, - G_TYPE_NONE, + G_TYPE_NONE, 1, G_TYPE_INT); - signal[CASES_DELETED] = - g_signal_new ("cases_deleted", - G_TYPE_FROM_CLASS(class), + signals [CASES_DELETED] = + g_signal_new ("cases-deleted", + G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, gtkextra_VOID__INT_INT, - G_TYPE_NONE, + G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT); } @@ -132,9 +130,9 @@ static void psppire_case_file_finalize (GObject *object) { PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object); - - if ( cf->flexifile) - casefile_destroy(cf->flexifile); + + if ( cf->accessible) + datasheet_destroy (cf->datasheet); G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -142,237 +140,260 @@ psppire_case_file_finalize (GObject *object) static void psppire_case_file_init (PsppireCaseFile *cf) { - cf->flexifile = 0; + cf->datasheet = NULL; + cf->accessible = FALSE; } + /** * psppire_case_file_new: * @returns: a new #PsppireCaseFile object - * - * Creates a new #PsppireCaseFile. + * + * Creates a new #PsppireCaseFile. */ PsppireCaseFile* -psppire_case_file_new (gint val_cnt) +psppire_case_file_new (const struct casereader *reader) { PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL); - cf->flexifile = flexifile_create (val_cnt); + cf->datasheet = datasheet_create (casereader_clone (reader)); + cf->accessible = TRUE; return cf; } gboolean -psppire_case_file_delete_cases(PsppireCaseFile *cf, gint n_cases, gint first) +psppire_case_file_delete_cases (PsppireCaseFile *cf, casenumber n_cases, casenumber first) { - int result; + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - g_return_val_if_fail(cf, FALSE); - g_return_val_if_fail(cf->flexifile, FALSE); + g_return_val_if_fail (first + n_cases <= + psppire_case_file_get_case_count (cf), FALSE); - result = flexifile_delete_cases(FLEXIFILE(cf->flexifile), n_cases, first); + datasheet_delete_rows (cf->datasheet, first, n_cases); - g_signal_emit(cf, signal[CASES_DELETED], 0, n_cases, first); + g_signal_emit (cf, signals [CASES_DELETED], 0, first, n_cases); - return result; + return TRUE; } /* Insert case CC into the case file before POSN */ gboolean -psppire_case_file_insert_case(PsppireCaseFile *cf, - struct ccase *cc, - gint posn) +psppire_case_file_insert_case (PsppireCaseFile *cf, + struct ccase *cc, + casenumber posn) { + struct ccase tmp; bool result ; - g_return_val_if_fail(cf, FALSE); - g_return_val_if_fail(cf->flexifile, FALSE); + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); + + case_clone (&tmp, cc); + result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1); - result = flexifile_insert_case(FLEXIFILE(cf->flexifile), cc, posn); - - if ( result ) - g_signal_emit(cf, signal[CASE_INSERTED], 0, posn); + if ( result ) + g_signal_emit (cf, signals [CASE_INSERTED], 0, posn); else - g_warning("Cannot insert case at position %d\n", posn); - + g_warning ("Cannot insert case at position %ld\n", posn); + return result; } /* Append a case to the case file */ -gboolean -psppire_case_file_append_case(PsppireCaseFile *cf, +static gboolean +psppire_case_file_append_case (PsppireCaseFile *cf, struct ccase *c) { + struct ccase tmp; bool result ; gint posn ; - g_return_val_if_fail(cf, FALSE); - g_return_val_if_fail(cf->flexifile, FALSE); + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); + + posn = datasheet_get_row_cnt (cf->datasheet); + + case_clone (&tmp, c); + result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1); - posn = casefile_get_case_cnt(cf->flexifile); + g_signal_emit (cf, signals [CASE_INSERTED], 0, posn); - result = casefile_append(cf->flexifile, c); - - g_signal_emit(cf, signal[CASE_INSERTED], 0, posn); - return result; } -inline gint -psppire_case_file_get_case_count(const PsppireCaseFile *cf) +inline casenumber +psppire_case_file_get_case_count (const PsppireCaseFile *cf) { - g_return_val_if_fail(cf, FALSE); - - if ( ! cf->flexifile) + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); + + if ( ! cf->datasheet) return 0; - return casefile_get_case_cnt(cf->flexifile); + return datasheet_get_row_cnt (cf->datasheet); } -/* Return the IDXth value from case CASENUM. - The return value must not be freed or written to - */ -const union value * -psppire_case_file_get_value(const PsppireCaseFile *cf, gint casenum, gint idx) +/* Copies the IDXth value from case CASENUM into VALUE. + If VALUE is null, then memory is allocated is allocated with + malloc. Returns the value if successful, NULL on failure. */ +union value * +psppire_case_file_get_value (const PsppireCaseFile *cf, + casenumber casenum, size_t idx, + union value *value, int width) { - const union value *v; - struct ccase c; - - g_return_val_if_fail(cf, NULL); - g_return_val_if_fail(cf->flexifile, NULL); - - g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), NULL); + bool allocated; - flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &c); + g_return_val_if_fail (cf, false); + g_return_val_if_fail (cf->datasheet, false); - v = case_data(&c, idx); + g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), false); - case_destroy(&c); - - return v; + if (value == NULL) + { + value = xnmalloc (value_cnt_from_width (width), sizeof *value); + allocated = true; + } + else + allocated = false; + if (!datasheet_get_value (cf->datasheet, casenum, idx, value, width)) + { + if (allocated) + free (value); + value = NULL; + } + return value; } void -psppire_case_file_clear(PsppireCaseFile *cf) +psppire_case_file_clear (PsppireCaseFile *cf) { - casefile_destroy(cf->flexifile); - cf->flexifile = 0; - g_signal_emit(cf, signal[CASES_DELETED], 0, 0, -1); + datasheet_destroy (cf->datasheet); + cf->datasheet = NULL; + g_signal_emit (cf, signals [CASES_DELETED], 0, 0, -1); } -/* Set the IDXth value of case C to SYSMIS/EMPTY */ +/* Set the IDXth value of case C to V. + Returns true if successful, false on I/O error. */ gboolean -psppire_case_file_set_value(PsppireCaseFile *cf, gint casenum, gint idx, +psppire_case_file_set_value (PsppireCaseFile *cf, casenumber casenum, gint idx, union value *v, gint width) { - struct ccase cc ; - int bytes; - - g_return_val_if_fail(cf, FALSE); - g_return_val_if_fail(cf->flexifile, FALSE); + bool ok; - g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE); - - if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) ) - return FALSE; - - if ( width == 0 ) - bytes = MAX_SHORT_STRING; - else - bytes = DIV_RND_UP(width, MAX_SHORT_STRING) * MAX_SHORT_STRING ; - - /* Cast away const in flagrant abuse of the casefile */ - memcpy((union value *)case_data(&cc, idx), v, bytes); + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); - g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum); + g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE); - return TRUE; + ok = datasheet_put_value (cf->datasheet, casenum, idx, v, width); + if (ok) + g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum); + return ok; } /* Set the IDXth value of case C using D_IN */ gboolean -psppire_case_file_data_in(PsppireCaseFile *cf, gint casenum, gint idx, +psppire_case_file_data_in (PsppireCaseFile *cf, casenumber casenum, gint idx, struct substring input, const struct fmt_spec *fmt) { - struct ccase cc ; + union value *value; + int width; + bool ok; - g_return_val_if_fail(cf, FALSE); - g_return_val_if_fail(cf->flexifile, FALSE); + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); - g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE); + g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE); - if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) ) - return FALSE; + width = fmt_var_width (fmt); + value = xmalloca (value_cnt_from_width (width) * sizeof *value); + ok = (datasheet_get_value (cf->datasheet, casenum, idx, value, width) + && data_in (input, fmt->type, 0, 0, value, width) + && datasheet_put_value (cf->datasheet, casenum, idx, value, width)); - /* Cast away const in flagrant abuse of the casefile */ - if (!data_in (input, fmt->type, 0, 0, - (union value *) case_data(&cc, idx), fmt_var_width (fmt))) - g_warning("Cant set value\n"); + if (ok) + g_signal_emit (cf, signals [CASE_CHANGED], 0, casenum); - g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum); + freea (value); return TRUE; } void -psppire_case_file_sort(PsppireCaseFile *cf, const struct sort_criteria *sc) +psppire_case_file_sort (PsppireCaseFile *cf, struct case_ordering *ordering) { - struct ccase cc; + struct casereader *sorted_data; gint c; - struct casefile *cfile; - struct casereader *reader = casefile_get_reader (cf->flexifile, NULL); - const int value_cnt = casefile_get_value_cnt(cf->flexifile); - - cfile = sort_execute(reader, sc); - - casefile_destroy(cf->flexifile); - /* Copy casefile into flexifile */ - - reader = casefile_get_destructive_reader(cfile); - cf->flexifile = flexifile_create(value_cnt); - while(casereader_read(reader, &cc)) - casefile_append(cf->flexifile, &cc); - + sorted_data = sort_execute (datasheet_make_reader (cf->datasheet), ordering); + cf->datasheet = datasheet_create (sorted_data); /* FIXME: Need to have a signal to change a range of cases, instead of calling a signal many times */ - for ( c = 0 ; c < casefile_get_case_cnt(cf->flexifile) ; ++c ) - g_signal_emit(cf, signal[CASE_CHANGED], 0, c); + for ( c = 0 ; c < datasheet_get_row_cnt (cf->datasheet) ; ++c ) + g_signal_emit (cf, signals [CASE_CHANGED], 0, c); } -/* Resize the cases in the casefile, by inserting N_VALUES into every - one of them. */ -gboolean -psppire_case_file_insert_values (PsppireCaseFile *cf, - gint n_values, gint before) +/* Resize the cases in the casefile, by inserting N_VALUES into every + one of them at the position immediately preceeding WHERE. +*/ +gboolean +psppire_case_file_insert_values (PsppireCaseFile *cf, + gint n_values, gint where) { - g_return_val_if_fail(cf, FALSE); + g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - if ( ! cf->flexifile ) - { - cf->flexifile = flexifile_create(n_values); - return TRUE; - } + if ( n_values == 0 ) + return FALSE; + + g_assert (n_values > 0); - return flexifile_resize (FLEXIFILE(cf->flexifile), n_values, before); + if ( ! cf->datasheet ) + cf->datasheet = datasheet_create (NULL); + + { + union value *values = xcalloc (n_values, sizeof *values); + datasheet_insert_columns (cf->datasheet, values, n_values, where); + free (values); + } + + return TRUE; } + /* Fills C with the CASENUMth case. Returns true on success, false otherwise. */ gboolean -psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum, +psppire_case_file_get_case (const PsppireCaseFile *cf, casenumber casenum, struct ccase *c) { g_return_val_if_fail (cf, FALSE); - g_return_val_if_fail (cf->flexifile, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); - return flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, c); + return datasheet_get_row (cf->datasheet, casenum, c); } + + + +struct casereader * +psppire_case_file_make_reader (PsppireCaseFile *cf) +{ + struct casereader *r = datasheet_make_reader (cf->datasheet); + cf->accessible = FALSE; + return r; +} +