1 #ifndef _GNM_STF_PARSE_H_ 2 # define _GNM_STF_PARSE_H_ 3 4 #include <glib.h> 5 #include <gnumeric.h> 6 7 G_BEGIN_DECLS 8 9 typedef enum { 10 PARSE_TYPE_NOTSET = 1 << 0, 11 PARSE_TYPE_CSV = 1 << 1, 12 PARSE_TYPE_FIXED = 1 << 2 13 } StfParseType_t; 14 15 /* Additive. */ 16 typedef enum { 17 TRIM_TYPE_NEVER = 0, 18 TRIM_TYPE_LEFT = 1 << 0, 19 TRIM_TYPE_RIGHT = 1 << 1 20 } StfTrimType_t; 21 22 typedef struct { 23 StfParseType_t parsetype; /* The type of import to do */ 24 StfTrimType_t trim_spaces; /* Trim spaces in fields? */ 25 26 GSList * terminator; /* Line terminators */ 27 char * locale; 28 29 struct _StfCompiledTerminator { 30 guchar min, max; 31 } compiled_terminator; 32 33 /* CSV related */ 34 struct _StfSeparator { 35 GSList *str; 36 char *chr; 37 gboolean duplicates; /* See two text separators as one? */ 38 } sep; 39 gunichar stringindicator; /* String indicator */ 40 gboolean indicator_2x_is_single;/* 2 quote chars form a single non-terminating quote */ 41 gboolean trim_seps; /* Ignore initial seps. */ 42 43 /* Fixed width related */ 44 GArray *splitpositions; /* Positions where text will be split vertically */ 45 46 gboolean *col_autofit_array; /* 0/1 array indicating */ 47 /* which col widths to autofit */ 48 gboolean *col_import_array; /* 0/1 array indicating */ 49 /* which cols to import */ 50 unsigned int col_import_array_len; 51 52 GPtrArray *formats; /* Contains GOFormat *s */ 53 GPtrArray *formats_decimal; /* Contains GString *s */ 54 GPtrArray *formats_thousand; /* Contains GString *s */ 55 GPtrArray *formats_curr; /* Contains GString *s */ 56 57 gboolean cols_exceeded; /* This is set to TRUE if */ 58 /* we tried to import more than */ 59 /* SHEET_MAX_COLS columns */ 60 gboolean rows_exceeded; /* Ditto rows. */ 61 unsigned ref_count; /* Boxed type */ 62 } StfParseOptions_t; 63 64 /* CREATION/DESTRUCTION of stf options struct */ 65 66 GType stf_parse_options_get_type (void); 67 void stf_parse_options_free (StfParseOptions_t *parseoptions); 68 69 StfParseOptions_t *stf_parse_options_guess (char const *data); 70 StfParseOptions_t *stf_parse_options_guess_csv (char const *data); 71 void stf_parse_options_guess_formats (StfParseOptions_t *po, 72 char const *data); 73 74 /* MANIPULATION of stf options struct */ 75 76 void stf_parse_options_set_type (StfParseOptions_t *parseoptions, 77 StfParseType_t const parsetype); 78 void stf_parse_options_clear_line_terminator (StfParseOptions_t *parseoptions); 79 void stf_parse_options_add_line_terminator (StfParseOptions_t *parseoptions, 80 char const *terminator); 81 void stf_parse_options_set_trim_spaces (StfParseOptions_t *parseoptions, 82 StfTrimType_t const trim_spaces); 83 void stf_parse_options_csv_set_separators (StfParseOptions_t *parseoptions, 84 char const *character, GSList const *seps); 85 void stf_parse_options_csv_set_stringindicator (StfParseOptions_t *parseoptions, 86 gunichar const stringindicator); 87 void stf_parse_options_csv_set_indicator_2x_is_single (StfParseOptions_t *parseoptions, 88 gboolean const indic_2x); 89 void stf_parse_options_csv_set_duplicates (StfParseOptions_t *parseoptions, 90 gboolean const duplicates); 91 void stf_parse_options_csv_set_trim_seps (StfParseOptions_t *parseoptions, 92 gboolean const trim_seps); 93 void stf_parse_options_fixed_splitpositions_clear (StfParseOptions_t *parseoptions); 94 void stf_parse_options_fixed_splitpositions_add (StfParseOptions_t *parseoptions, 95 int position); 96 void stf_parse_options_fixed_splitpositions_remove (StfParseOptions_t *parseoptions, 97 int position); 98 int stf_parse_options_fixed_splitpositions_count (StfParseOptions_t *parseoptions); 99 int stf_parse_options_fixed_splitpositions_nth (StfParseOptions_t *parseoptions, int n); 100 101 /* USING the stf structs to actually do some parsing, these are the lower-level functions and utility functions */ 102 103 GPtrArray *stf_parse_general (StfParseOptions_t *parseoptions, 104 GStringChunk *lines_chunk, 105 char const *data, 106 char const *data_end); 107 void stf_parse_general_free (GPtrArray *lines); 108 GPtrArray *stf_parse_lines (StfParseOptions_t *parseoptions, 109 GStringChunk *lines_chunk, 110 char const *data, 111 int maxlines, 112 gboolean with_lineno); 113 114 void stf_parse_options_fixed_autodiscover (StfParseOptions_t *parseoptions, 115 char const *data, 116 char const *data_end); 117 118 char const *stf_parse_find_line (StfParseOptions_t *parseoptions, 119 char const *data, 120 int line); 121 122 /* Higher level functions, can be used for directly parsing into an application specific data container */ 123 gboolean stf_parse_sheet (StfParseOptions_t *parseoptions, 124 char const *data, char const *data_end, 125 Sheet *sheet, 126 int start_col, int start_row); 127 128 GnmCellRegion *stf_parse_region (StfParseOptions_t *parseoptions, 129 char const *data, char const *data_end, 130 Workbook const *wb); 131 132 G_END_DECLS 133 134 #endif /* _GNM_STF_PARSE_H_ */ 135