COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/ssinsert/ssinsert_pass1.c
Go to the documentation of this file.
00001 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
00002 
00003 /*
00004 
00005  Based on ssconvert.c from gnumeric, which is copyrighted as below,
00006  and used here under the GPL
00007 
00008  The modifications here are to overlay data from a 
00009  csv file onto an excel/openoffice/... spreadsheet.
00010  This is a compromise to allow some control over presentation
00011  of data in Coopy, while not requiring arbitrary spreadsheet
00012  parsing code in the revision control system.
00013 
00014  Modifications are currently OK (see new -L option), but have
00015  the classic CSV issue with interpreting integers etc (zip
00016  codes like 02139 lose the leading 0).  For coopy use, CSV
00017  should be uninterpreted until the point of overlay.  This
00018  probably won't fit under ssconvert's mission.
00019 
00020 */
00021 
00022 /*
00023  * ssconvert.c: A wrapper application to convert spreadsheet formats
00024  *
00025  * Author:
00026  *   Jon Kåre Hellan <hellan@acm.org>
00027  *   Morten Welinder <terra@gnome.org>
00028  *   Jody Goldberg <jody@gnome.org>
00029  *
00030  * Copyright (C) 2002-2003 Jody Goldberg
00031  * Copyright (C) 2006-2009 Morten Welinder (terra@gnome.org)
00032  */
00033 #include <gnumeric-config.h>
00034 #include <glib/gi18n.h>
00035 #include "gnumeric.h"
00036 #include "position.h"
00037 #include "parse-util.h"
00038 #include "workbook.h"
00039 #include "workbook-priv.h"
00040 #include "workbook-control.h"
00041 #include "sheet.h"
00042 #include "dependent.h"
00043 #include "expr-name.h"
00044 #include "libgnumeric.h"
00045 #include "gutils.h"
00046 #include "gnumeric-paths.h"
00047 #include "gnm-plugin.h"
00048 #include "command-context.h"
00049 #include "command-context-stderr.h"
00050 #include "workbook-view.h"
00051 #include "cell.h"
00052 #include "value.h"
00053 #include <dialogs/dialogs.h>
00054 #include <goffice/goffice.h>
00055 #include <gsf/gsf-utils.h>
00056 #include <string.h>
00057 #ifdef HAVE_SYS_RESOURCE_H
00058 #include <sys/resource.h>
00059 #endif
00060 
00061 static gboolean ssconvert_show_version = FALSE;
00062 static gboolean ssconvert_list_exporters = FALSE;
00063 static gboolean ssconvert_list_importers = FALSE;
00064 static gboolean ssconvert_one_file_per_sheet = FALSE;
00065 static gboolean ssconvert_recalc = FALSE;
00066 static gboolean ssconvert_solve = FALSE;
00067 static char *ssconvert_range = NULL;
00068 static char *ssconvert_import_encoding = NULL;
00069 static char *ssconvert_import_id = NULL;
00070 static char *ssconvert_export_id = NULL;
00071 static char *ssconvert_export_options = NULL;
00072 static char *ssconvert_merge_target = NULL;
00073 static char *ssconvert_overlay_target = NULL;
00074 static char **ssconvert_goal_seek = NULL;
00075 
00076 #ifdef CUSTOM_GNUMERIC
00077 extern int hack_csv;
00078 extern void stf_cell_set_text (GnmCell *cell, char const *text);
00079 #endif
00080 
00081 //#define OLD_GNUMERIC
00082 
00083 #ifdef OLD_GNUMERIC
00084 #define GOIOContext IOContext
00085 #define GO_FILE_SAVE_WORKBOOK FILE_SAVE_WORKBOOK
00086 #define GOErrorInfo ErrorInfo
00087 #define GNM_VERSION_FULL GNUMERIC_VERSION
00088 #define go_get_file_savers get_file_savers
00089 #define go_get_file_openers get_file_openers
00090 //#define go_ IOContext
00091 #endif
00092 
00093 static const GOptionEntry ssconvert_options [] = {
00094         {
00095                 "version", 'v',
00096                 0, G_OPTION_ARG_NONE, &ssconvert_show_version,
00097                 N_("Display program version"),
00098                 NULL
00099         },
00100 
00101         /* ---------------------------------------- */
00102 
00103         {
00104                 "import-encoding", 'E',
00105                 0, G_OPTION_ARG_STRING, &ssconvert_import_encoding,
00106                 N_("Optionally specify an encoding for imported content"),
00107                 N_("ENCODING")
00108         },
00109 
00110         {
00111                 "import-type", 'I',
00112                 0, G_OPTION_ARG_STRING, &ssconvert_import_id,
00113                 N_("Optionally specify which importer to use"),
00114                 N_("ID")
00115         },
00116 
00117         {
00118                 "list-importers", 0,
00119                 0, G_OPTION_ARG_NONE, &ssconvert_list_importers,
00120                 N_("List the available importers"),
00121                 NULL
00122         },
00123 
00124         /* ---------------------------------------- */
00125 
00126         {
00127                 "merge-to", 'M',
00128                 0, G_OPTION_ARG_STRING, &ssconvert_merge_target,
00129                 N_("Merge listed files (all same format) to make this file"),
00130                 N_("merge files")
00131         },
00132 
00133         {
00134                 "overlay-to", 'L',
00135                 0, G_OPTION_ARG_STRING, &ssconvert_overlay_target,
00136                 N_("Overlay files to make this file"),
00137                 N_("overlay files")
00138         },
00139 
00140         {
00141                 "export-type", 'T',
00142                 0, G_OPTION_ARG_STRING, &ssconvert_export_id,
00143                 N_("Optionally specify which exporter to use"),
00144                 N_("ID")
00145         },
00146 
00147         {
00148                 "export-options", 'O',
00149                 0, G_OPTION_ARG_STRING, &ssconvert_export_options,
00150                 N_("Detailed instructions for the chosen exporter"),
00151                 N_("string")
00152         },
00153 
00154         {
00155                 "list-exporters", 0,
00156                 0, G_OPTION_ARG_NONE, &ssconvert_list_exporters,
00157                 N_("List the available exporters"),
00158                 NULL
00159         },
00160 
00161         {
00162                 "export-file-per-sheet", 'S',
00163                 0, G_OPTION_ARG_NONE, &ssconvert_one_file_per_sheet,
00164                 N_("Export a file for each sheet if the exporter only supports one sheet at a time"),
00165                 NULL
00166         },
00167 
00168         {
00169                 "recalc", 0,
00170                 0, G_OPTION_ARG_NONE, &ssconvert_recalc,
00171                 N_("Recalculate all cells before writing the result"),
00172                 NULL
00173         },
00174 
00175 
00176         /* ---------------------------------------- */
00177 
00178         /* For now these are for INTERNAL GNUMERIC USE ONLY.  */
00179         {
00180                 "export-range", 0,
00181                 G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &ssconvert_range,
00182                 N_("The range to export"),
00183                 NULL
00184         },
00185 
00186         {
00187                 "goal-seek", 0,
00188                 G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &ssconvert_goal_seek,
00189                 N_("Goal seek areas"),
00190                 NULL
00191         },
00192 
00193         {
00194                 "solve", 0,
00195                 G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &ssconvert_solve,
00196                 N_("Run the solver"),
00197                 NULL
00198         },
00199 
00200         /* ---------------------------------------- */
00201 
00202         { NULL }
00203 };
00204 
00205 
00206 #ifndef OLD_GNUMERIC
00207 static int
00208 handle_export_options (GOFileSaver *fs, GODoc *doc)
00209 {
00210         guint sig = g_signal_lookup ("set-export-options",
00211                                      G_TYPE_FROM_INSTANCE (fs));
00212 
00213         if (!ssconvert_export_options)
00214                 return 0;
00215 
00216         if (g_signal_handler_find (fs, G_SIGNAL_MATCH_ID,
00217                                    sig, 0, NULL, NULL, NULL)) {
00218                 GError *err = NULL;
00219                 gboolean fail =
00220                         go_file_saver_set_export_options
00221                         (fs, doc,
00222                          ssconvert_export_options,
00223                          &err);
00224 
00225                 if (fail) {
00226                         g_printerr ("ssconvert: %s\n", err
00227                                     ? err->message
00228                                     : _("Cannot parse export options."));
00229                         return 1;
00230                 }
00231 
00232                 return 0;
00233         } else {
00234                 g_printerr (_("The file saver does not take options"));
00235                 return 1;
00236         }
00237 }
00238 #endif
00239 
00240 typedef GList *(*get_them_f)(void);
00241 typedef gchar const *(*get_desc_f)(void *);
00242 
00243 static void
00244 list_them (get_them_f get_them,
00245            get_desc_f get_his_id,
00246            get_desc_f get_his_description)
00247 {
00248         GList *ptr;
00249         int len = 0;
00250 
00251         for (ptr = (*get_them) (); ptr ; ptr = ptr->next) {
00252                 char const *id = (*get_his_id) (ptr->data);
00253                 int tmp = strlen (id);
00254                 if (len < tmp)
00255                         len = tmp;
00256         }
00257 
00258         g_printerr ("%-*s | %s\n", len,
00259                     /* Translate these? */
00260                     "ID",
00261                     "Description");
00262         for (ptr = (*get_them) (); ptr ; ptr = ptr->next) {
00263                 char const *id = (*get_his_id) (ptr->data);
00264                 g_printerr ("%-*s | %s\n", len,
00265                             id,
00266                             (*get_his_description) (ptr->data));
00267         }
00268 }
00269 
00270 /*
00271  * Read the files we're going to merge and return a list of Workbooks.
00272  */
00273 static GSList *
00274 read_files_to_merge (const char *inputs[], GOFileOpener *fo,
00275                      GOIOContext *io_context, GOCmdContext *cc)
00276 {
00277         GSList *wbs = NULL;
00278 
00279         while (*inputs) {
00280                 const char *fname = *inputs;
00281                 char *uri = go_shell_arg_to_uri (fname);
00282                 WorkbookView *wbv =
00283                         wb_view_new_from_uri (uri, fo, io_context,
00284                                               ssconvert_import_encoding);
00285                 g_free (uri);
00286                 inputs++;
00287 
00288                 if (go_io_error_occurred (io_context)) {
00289                         go_slist_free_custom (wbs, g_object_unref);
00290                         return NULL;
00291                 }
00292 
00293 #ifdef CUSTOM_GNUMERIC
00294                 hack_csv = 1;
00295 #endif
00296 
00297                 if (!wbv)
00298                         continue;
00299 
00300                 wbs = g_slist_prepend (wbs, wb_view_get_workbook (wbv));
00301         }
00302 
00303         return g_slist_reverse (wbs);
00304 }
00305 
00306 
00307 void process_cell (gpointer key,
00308                    gpointer value,
00309                    gpointer user_data) {
00310         printf("Looking at a cell\n");
00311 }
00312 
00313 /*
00314  * Look at a set of workbooks, and pick a sheet size that would
00315  * be good for sheets in a workbook merging them all.
00316  */
00317 static void
00318 suggest_size (GSList *wbs, int *csuggest, int *rsuggest)
00319 {
00320         GSList *l;
00321         int rmax = 0;
00322         int cmax = 0;
00323 
00324         for (l = wbs; l; l = l->next) {
00325                 Workbook *wb = l->data;
00326 
00327                 WORKBOOK_FOREACH_SHEET (wb, sheet, {
00328                         int r = gnm_sheet_get_max_rows (sheet);
00329                         int c = gnm_sheet_get_max_cols (sheet);
00330                         if (r > rmax) rmax = r;
00331                         if (c > cmax) cmax = c;
00332                 });
00333         }
00334 
00335         gnm_sheet_suggest_size (&cmax, &rmax);
00336         *csuggest = cmax;
00337         *rsuggest = rmax;
00338 }
00339 
00340 static void
00341 cb_fixup_name_wb (const char *name, GnmNamedExpr *nexpr, Workbook *wb)
00342 {
00343         if (nexpr->pos.wb)
00344                 nexpr->pos.wb = wb;
00345 }
00346 
00347 
00348 /* Append the sheets of workbook wb2 to workbook wb.  Resize sheets
00349    if necessary.  Fix workbook links in sheet if necessary.
00350    Merge names in workbook scope (conflicts result in an error). */
00351 static gboolean
00352 merge_single (Workbook *wb, Workbook *wb2,
00353               int cmax, int rmax,
00354               GOCmdContext *cc)
00355 {
00356         /* Move names with workbook scope in wb2 over to wb */
00357         GSList *names = g_slist_sort (gnm_named_expr_collection_list (wb2->names),
00358                                       (GCompareFunc)expr_name_cmp_by_name);
00359         GSList *p;
00360         GnmParsePos pp;
00361 
00362         parse_pos_init (&pp, wb, NULL, 0, 0);
00363 
00364         for (p = names; p; p = p->next) {
00365                 GnmNamedExpr *nexpr = p->data;
00366                 const char *name = expr_name_name (nexpr);
00367                 GnmNamedExpr *nexpr2;
00368                 Sheet *sheet;
00369 
00370                 if (!nexpr->active)
00371                         continue;
00372 
00373                 if (nexpr->pos.wb == NULL || nexpr->pos.sheet != NULL)
00374                         continue;
00375 
00376                 /* Check for clash with existing name */
00377 
00378                 nexpr2 = expr_name_lookup (&pp, name);
00379                 if (nexpr2 != NULL) {
00380                         g_printerr (_("Name conflict during merge: '%s' appears twice at workbook scope.\n"),
00381                                     name);
00382                         g_slist_free (names);
00383                         return TRUE;
00384                 }
00385 
00386                 /* Move name scope to workbook wb */
00387                 sheet = workbook_sheet_by_index (wb2, 0);
00388                 expr_name_set_scope (nexpr, sheet);
00389                 nexpr->pos.wb = wb;
00390                 expr_name_set_scope (nexpr, NULL);
00391         }
00392         g_slist_free (names);
00393 
00394         while (workbook_sheet_count (wb2) > 0) {
00395                 /* Remove sheet from incoming workbook */
00396                 Sheet *sheet = workbook_sheet_by_index (wb2, 0);
00397                 int loc = workbook_sheet_count (wb);
00398 #ifndef OLD_GNUMERIC
00399                 GOUndo *undo;
00400 #endif
00401                 char *sheet_name;
00402                 gboolean err;
00403 
00404                 g_object_ref (sheet);
00405                 workbook_sheet_delete (sheet);
00406                 sheet->workbook = wb;
00407 
00408                 /* Fix names that reference the old workbook */
00409                 gnm_sheet_foreach_name (sheet, (GHFunc)cb_fixup_name_wb, wb);
00410 
00411 #ifndef OLD_GNUMERIC
00412                 undo = gnm_sheet_resize (sheet, cmax, rmax, cc, &err);
00413                 if (undo)
00414                         g_object_unref (undo);
00415 #endif
00416 
00417                 /* normal merge */
00418                 
00419                 /* Pick a free sheet name */
00420                 sheet_name = workbook_sheet_get_free_name
00421                         (wb, sheet->name_unquoted, FALSE, TRUE);
00422                 g_object_set (sheet, "name", sheet_name, NULL);
00423                 g_free (sheet_name);
00424                 
00425                 /* Insert and revive the sheet */
00426                 workbook_sheet_attach_at_pos (wb, sheet, loc);
00427                 dependents_revive_sheet (sheet);
00428                 g_object_unref (sheet);
00429 
00430 
00431 #if 0
00432                         /* overlay mode */
00433                         printf("Overlay sheet %d\n", *overlay);
00434                         Sheet *s2 = workbook_sheet_by_index(wb, *overlay);
00435                         GPtrArray *cells = sheet_cells(sheet,FALSE);
00436                         GPtrArray *cells2 = sheet_cells(s2,FALSE);
00437                         unsigned int i;
00438                         for (i = 0; i < cells2->len; i++) {
00439                                 GnmEvalPos *pos = (GnmEvalPos*)
00440                                         g_ptr_array_index(cells2, i);
00441                                 GnmCell *cell = sheet_cell_get(sheet,
00442                                                                pos->eval.col,
00443                                                                pos->eval.row);
00444                                 GnmCell *cell2 = sheet_cell_get(s2,
00445                                                                 pos->eval.col,
00446                                                                 pos->eval.row);
00447                                 if (cell==NULL) {
00448                                         sheet_cell_remove(s2,
00449                                                           cell2,
00450                                                           FALSE,
00451                                                           FALSE);
00452                                 }
00453                         }
00454 
00455                         for (i = 0; i < cells->len; i++) {
00456                                 GnmEvalPos *pos = (GnmEvalPos*)
00457                                         g_ptr_array_index(cells, i);
00458                                 GnmCell *cell = sheet_cell_get(sheet,
00459                                                                pos->eval.col,
00460                                                                pos->eval.row);
00461                                 GnmCell *cell2 = sheet_cell_get(s2,
00462                                                                 pos->eval.col,
00463                                                                 pos->eval.row);
00464                                 if (cell2==NULL) {
00465                                         cell2 = sheet_cell_create(s2,
00466                                                                   pos->eval.col,
00467                                                                   pos->eval.row);
00468                                 }
00469 #ifdef CUSTOM_GNUMERIC
00470                                 printf("Looking at %s\n",
00471                                        value_peek_string(cell->value));
00472                                 hack_csv = 0;
00473                                 stf_cell_set_text(cell2,
00474                                                   value_peek_string(cell->value));
00475 #else
00476                                 GnmValue *val = value_dup(cell->value);
00477                                 sheet_cell_set_value(cell2, val);
00478 #endif
00479                                                      
00480                         }
00481                         for (i = 0; i < cells->len; i++) {
00482                                 g_free (g_ptr_array_index (cells, i));
00483                         }
00484                         g_ptr_array_free (cells, TRUE);
00485                         for (i = 0; i < cells2->len; i++) {
00486                                 g_free (g_ptr_array_index (cells2, i));
00487                         }
00488                         g_ptr_array_free (cells2, TRUE);
00489 
00490                         g_object_unref (sheet);
00491 
00492                         (*overlay)++;
00493 #endif
00494         }
00495 
00496         return FALSE;
00497 }
00498 
00499 
00500 /* Overlay data in a spreadsheet.  Resize sheets if necessary. */
00501 static gboolean
00502 overlay_single (Workbook *wb, Workbook *wb2,
00503                 int cmax, int rmax,
00504                 GOCmdContext *cc) {
00505         return TRUE;
00506 }
00507 
00508 
00509 /* Merge a collection of workbooks into one. */
00510 static gboolean
00511 merge (Workbook *wb, char const *inputs[],
00512        GOFileOpener *fo, GOIOContext *io_context, GOCmdContext *cc)
00513 {
00514         GSList *wbs, *l;
00515         int result = 0;
00516         int cmax, rmax;
00517 
00518         wbs = read_files_to_merge (inputs, fo, io_context, cc);
00519         if (go_io_error_occurred (io_context)) {
00520                 go_io_error_display (io_context);
00521                 return TRUE;
00522         }
00523 
00524         suggest_size (wbs, &cmax, &rmax);
00525 
00526         for (l = wbs; l; l = l->next) {
00527                 Workbook *wb2 = l->data;
00528                 const char *uri = go_doc_get_uri (GO_DOC (wb2));
00529 
00530                 g_printerr ("Adding sheets from %s\n", uri);
00531 
00532                 result = merge_single (wb, wb2, cmax, rmax, cc);
00533                 if (result)
00534                         break;
00535         }
00536 
00537         go_slist_free_custom (wbs, g_object_unref);
00538         return result;
00539 }
00540 
00541 /* Overlay data on a workbook. */
00542 static gboolean
00543 overlay (Workbook *wb, char const *inputs[],
00544          GOFileOpener *fo, GOIOContext *io_context, GOCmdContext *cc)
00545 {
00546         int cmax, rmax;
00547 
00548         while (*inputs) {
00549                 inputs++;
00550                 if (!(*inputs)) {
00551                         break;
00552                 }
00553                 printf("Overlay from %s\n", *inputs);
00554         }
00555 
00556         return FALSE;
00557 }
00558 
00559 
00560 static int
00561 convert (char const *inarg, char const *outarg, char const *mergeargs[],
00562          char const *overlayargs[],
00563          GOCmdContext *cc)
00564 {
00565         int res = 0;
00566         GOFileSaver *fs = NULL;
00567         GOFileOpener *fo = NULL;
00568         char *infile = go_shell_arg_to_uri (inarg);
00569         char *outfile = outarg ? go_shell_arg_to_uri (outarg) : NULL;
00570 
00571         if (ssconvert_export_id != NULL) {
00572                 fs = go_file_saver_for_id (ssconvert_export_id);
00573                 if (fs == NULL) {
00574                         res = 1;
00575                         g_printerr (_("Unknown exporter '%s'.\n"
00576                                       "Try --list-exporters to see a list of possibilities.\n"),
00577                                     ssconvert_export_id);
00578                         goto out;
00579                 } else if (outfile == NULL &&
00580                            go_file_saver_get_extension (fs) != NULL) {
00581                         char const *ext = gsf_extension_pointer (infile);
00582                         if (*infile) {
00583                                 GString *res = g_string_new (NULL);
00584                                 g_string_append_len (res, infile, ext - infile);
00585                                 g_string_append (res, go_file_saver_get_extension(fs));
00586                                 outfile = g_string_free (res, FALSE);
00587                         }
00588                 }
00589         } else {
00590                 if (outfile != NULL) {
00591                         fs = go_file_saver_for_file_name (outfile);
00592                         if (fs == NULL) {
00593                                 res = 2;
00594                                 g_printerr (_("Unable to guess exporter to use for '%s'.\n"
00595                                               "Try --list-exporters to see a list of possibilities.\n"),
00596                                             outfile);
00597                                 goto out;
00598                         }
00599                         g_print ("Using exporter %s\n",
00600                                  go_file_saver_get_id (fs));
00601                 }
00602         }
00603 
00604         if (outfile == NULL) {
00605                 g_printerr (_("An output file name or an explicit export type is required.\n"
00606                               "Try --list-exporters to see a list of possibilities.\n"));
00607                 res = 1;
00608                 goto out;
00609         }
00610 
00611         if (ssconvert_import_id != NULL) {
00612                 fo = go_file_opener_for_id (ssconvert_import_id);
00613                 if (fo == NULL) {
00614                         res = 1;
00615                         g_printerr (_("Unknown importer '%s'.\n"
00616                                       "Try --list-importers to see a list of possibilities.\n"),
00617                                     ssconvert_import_id);
00618                         goto out;
00619                 }
00620         }
00621 
00622         if (fs != NULL) {
00623                 GOIOContext *io_context = go_io_context_new (cc);
00624                 WorkbookView *wbv;
00625                 if (mergeargs==NULL) {
00626                         wbv = wb_view_new_from_uri (infile, fo,
00627                                                     io_context,
00628                                                     ssconvert_import_encoding);
00629                 } else {
00630                         wbv = workbook_view_new (NULL);
00631                 }
00632 
00633                 if (wbv == NULL || go_io_error_occurred (io_context)) {
00634                         go_io_error_display (io_context);
00635                         res = 1;
00636                 } else {
00637                         Workbook *wb = wb_view_get_workbook (wbv);
00638                         Sheet *sheet = wb_view_cur_sheet (wbv);
00639 
00640 #ifndef OLD_GNUMERIC
00641                         res = handle_export_options (fs, GO_DOC (wb));
00642                         if (res) {
00643                                 g_object_unref (wb);
00644                                 goto out;
00645                         }
00646 #endif
00647 
00648                         if (mergeargs!=NULL) {
00649                                 if (merge (wb, mergeargs, fo, io_context, cc))
00650                                         goto out;
00651                         }
00652 
00653                         if (overlayargs!=NULL) {
00654                                 if (overlay (wb, overlayargs, fo, io_context, cc))
00655                                         goto out;
00656                         }
00657 
00658                         else if (workbook_sheet_count (wb) > 1 &&
00659                                  go_file_saver_get_save_scope (fs) != GO_FILE_SAVE_WORKBOOK) {
00660                                 if (ssconvert_one_file_per_sheet) {
00661                                         GSList *ptr;
00662                                         GString *s;
00663                                         char *tmpfile;
00664                                         int idx = 0;
00665                                         res = 0;
00666 
00667                                         for (ptr = workbook_sheets(wb); ptr && !res; ptr = ptr->next, idx++) {
00668                                                 wb_view_sheet_focus(wbv, (Sheet *)ptr->data);
00669                                                 s = g_string_new (outfile);
00670                                                 g_string_append_printf(s, ".%d", idx);
00671                                                 tmpfile = g_string_free (s, FALSE);
00672                                                 res = !wb_view_save_as (wbv, fs, tmpfile, cc);
00673                                                 g_free(tmpfile);
00674                                         }
00675                                         goto printing_done;
00676                                 } else
00677                                         g_printerr (_("Selected exporter (%s) does not support saving multiple sheets in one file.\n"
00678                                                       "Only the current sheet will be saved.\n"),
00679                                                     go_file_saver_get_id (fs));
00680                         }
00681                         res = !wb_view_save_as (wbv, fs, outfile, cc);
00682                 printing_done:
00683                         g_object_unref (wb);
00684                 }
00685                 g_object_unref (io_context);
00686         }
00687 
00688  out:
00689         g_free (infile);
00690         g_free (outfile);
00691 
00692         return res;
00693 }
00694 
00695 int
00696 main (int argc, char const **argv)
00697 {
00698         GOErrorInfo     *plugin_errs;
00699         int              res = 0;
00700         GOCmdContext    *cc;
00701         GOptionContext *ocontext;
00702         GError *error = NULL;
00703 
00704         /* No code before here, we need to init threads */
00705 #ifdef OLD_GNUMERIC
00706         gchar const **args = go_shell_argv_to_glib_encoding (argc, argv);
00707         gnm_pre_parse_init (args[0]);
00708 #else
00709         argv = gnm_pre_parse_init (argc, argv);
00710 #endif
00711 
00712         ocontext = g_option_context_new (_("INFILE [OUTFILE]"));
00713         g_option_context_add_main_entries (ocontext, ssconvert_options, GETTEXT_PACKAGE);
00714         g_option_context_add_group        (ocontext, gnm_get_option_group ());
00715         g_option_context_parse (ocontext, &argc, (char ***)&argv, &error);
00716         g_option_context_free (ocontext);
00717 
00718         if (error) {
00719                 g_printerr (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
00720                             error->message, argv[0]);
00721                 g_error_free (error);
00722                 return 1;
00723         }
00724 
00725         if (ssconvert_show_version) {
00726                 g_print (_("ssconvert version '%s'\ndatadir := '%s'\nlibdir := '%s'\n"),
00727                          GNM_VERSION_FULL, gnm_sys_data_dir (), gnm_sys_lib_dir ());
00728                 return 0;
00729         }
00730 
00731         gnm_init ();
00732 
00733         cc = cmd_context_stderr_new ();
00734         gnm_plugins_init (GO_CMD_CONTEXT (cc));
00735         go_plugin_db_activate_plugin_list (
00736                 go_plugins_get_available_plugins (), &plugin_errs);
00737         if (plugin_errs) {
00738                 /* FIXME: What do we want to do here? */
00739                 go_error_info_free (plugin_errs);
00740         }
00741 
00742         if (ssconvert_list_exporters)
00743                 list_them (&go_get_file_savers,
00744                            (get_desc_f) &go_file_saver_get_id,
00745                            (get_desc_f) &go_file_saver_get_description);
00746         else if (ssconvert_list_importers)
00747                 list_them (&go_get_file_openers,
00748                            (get_desc_f) &go_file_opener_get_id,
00749                            (get_desc_f) &go_file_opener_get_description);
00750         else if (ssconvert_merge_target!=NULL && argc>=3) {
00751                 res = convert (argv[1], ssconvert_merge_target, argv+1, 
00752                                NULL, cc);
00753         } else if (ssconvert_overlay_target!=NULL && argc>=3) {
00754                 res = convert (argv[1], ssconvert_overlay_target, NULL,
00755                                argv+1, cc);
00756         } else if (argc == 2 || argc == 3) {
00757                 res = convert (argv[1], argv[2], NULL, NULL, cc);
00758         } else {
00759                 g_printerr (_("Usage: %s [OPTION...] %s\n"),
00760                             g_get_prgname (),
00761                             _("INFILE [OUTFILE]"));
00762                 res = 1;
00763         }
00764 
00765         g_object_unref (cc);
00766         gnm_shutdown ();
00767         gnm_pre_parse_shutdown ();
00768 
00769         return res;
00770 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines