X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-case-file.c;h=8b194528089d95f3642913bd3709e8f5ec2524ee;hb=d0371553a98cd169353bf6d211e375e5ffc3a3bd;hp=4c4bce8838ab739c46d769be0d0bf5f1fa15967c;hpb=4517b68e7248f22e7b7ed81f0d73179351a53047;p=pspp-builds.git diff --git a/src/ui/gui/psppire-case-file.c b/src/ui/gui/psppire-case-file.c index 4c4bce88..8b194528 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 +/* 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 2 of the License, or - (at your option) any later version. + 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. + 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. */ + 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 "flexifile-factory.h" -#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); @@ -48,7 +46,7 @@ enum {CASE_CHANGED, 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,8 +90,8 @@ psppire_case_file_class_init (PsppireCaseFileClass *class) object_class->finalize = psppire_case_file_finalize; - signal[CASE_CHANGED] = - g_signal_new ("case_changed", + signals [CASE_CHANGED] = + g_signal_new ("case-changed", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, @@ -104,8 +102,8 @@ psppire_case_file_class_init (PsppireCaseFileClass *class) G_TYPE_INT); - signal[CASE_INSERTED] = - g_signal_new ("case_inserted", + signals [CASE_INSERTED] = + g_signal_new ("case-inserted", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, @@ -116,8 +114,8 @@ psppire_case_file_class_init (PsppireCaseFileClass *class) G_TYPE_INT); - signal[CASES_DELETED] = - g_signal_new ("cases_deleted", + signals [CASES_DELETED] = + g_signal_new ("cases-deleted", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_FIRST, 0, @@ -133,8 +131,8 @@ 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,7 +140,8 @@ psppire_case_file_finalize (GObject *object) static void psppire_case_file_init (PsppireCaseFile *cf) { - cf->flexifile = 0; + cf->datasheet = NULL; + cf->accessible = FALSE; } @@ -153,232 +152,224 @@ psppire_case_file_init (PsppireCaseFile *cf) * Creates a new #PsppireCaseFile. */ PsppireCaseFile* -psppire_case_file_new (void) +psppire_case_file_new (const struct casereader *reader) { PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL); - cf->flexifile = flexifile_create (0); + cf->datasheet = datasheet_create (casereader_clone (reader)); + cf->accessible = TRUE; return cf; } -void -psppire_case_file_replace_flexifile (PsppireCaseFile *cf, struct flexifile *ff) -{ - cf->flexifile = (struct casefile *) ff; -} - - - 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->flexifile, FALSE); + g_return_val_if_fail (cf->datasheet, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - result = flexifile_delete_cases (FLEXIFILE (cf->flexifile), n_cases, first); + g_return_val_if_fail (first + n_cases <= + psppire_case_file_get_case_count (cf), FALSE); - g_signal_emit (cf, signal[CASES_DELETED], 0, n_cases, first); + datasheet_delete_rows (cf->datasheet, first, n_cases); - return result; + g_signal_emit (cf, signals [CASES_DELETED], 0, first, n_cases); + + return TRUE; } /* Insert case CC into the case file before POSN */ gboolean psppire_case_file_insert_case (PsppireCaseFile *cf, - struct ccase *cc, - gint posn) + 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->datasheet, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - result = flexifile_insert_case (FLEXIFILE (cf->flexifile), cc, posn); + case_clone (&tmp, cc); + result = datasheet_insert_rows (cf->datasheet, posn, &tmp, 1); if ( result ) - g_signal_emit (cf, signal[CASE_INSERTED], 0, posn); + 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, - struct ccase *c) -{ - bool result ; - gint posn ; - - g_return_val_if_fail (cf, FALSE); - g_return_val_if_fail (cf->flexifile, FALSE); - - posn = casefile_get_case_cnt (cf->flexifile); - - result = casefile_append (cf->flexifile, c); - - g_signal_emit (cf, signal[CASE_INSERTED], 0, posn); - - return result; -} - - -inline gint +inline casenumber psppire_case_file_get_case_count (const PsppireCaseFile *cf) { g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - if ( ! cf->flexifile) + 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); + bool allocated; - g_return_val_if_fail (idx < casefile_get_value_cnt (cf->flexifile), NULL); + g_return_val_if_fail (cf, false); + g_return_val_if_fail (cf->datasheet, false); - flexifile_get_case (FLEXIFILE (cf->flexifile), casenum, &c); + g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), false); - v = case_data_idx (&c, idx); - 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) { - 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; + bool ok; g_return_val_if_fail (cf, FALSE); - g_return_val_if_fail (cf->flexifile, FALSE); - - 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 ; + g_return_val_if_fail (cf->datasheet, FALSE); - /* Cast away const in flagrant abuse of the casefile */ - memcpy ((union value *)case_data_idx (&cc, idx), v, bytes); + g_return_val_if_fail (idx < datasheet_get_column_cnt (cf->datasheet), FALSE); - g_signal_emit (cf, signal[CASE_CHANGED], 0, casenum); - - 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->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, LEGACY_NATIVE, 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_idx (&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 casereader *sorted_data; gint c; - struct casereader *reader = casefile_get_reader (cf->flexifile, NULL); - struct casefile *cfile; - - struct casefile_factory *factory = flexifile_factory_create (); - - cfile = sort_execute (reader, sc, factory); - - casefile_destroy (cf->flexifile); - - cf->flexifile = cfile; + 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); - - flexifile_factory_destroy (factory); + 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. */ + one of them at the position immediately preceeding WHERE. +*/ gboolean psppire_case_file_insert_values (PsppireCaseFile *cf, - gint n_values, gint before) + gint n_values, gint where) { g_return_val_if_fail (cf, FALSE); + g_return_val_if_fail (cf->accessible, FALSE); - if ( ! cf->flexifile ) - { - cf->flexifile = flexifile_create (n_values); + if ( n_values == 0 ) + return FALSE; - return TRUE; - } + 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; +} +