File: write_csv.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (179 lines) | stat: -rw-r--r-- 4,015 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <string.h>
#ifdef USE_STRINGS_H
#include <strings.h>
#endif
#include <gtk/gtk.h>
#include "writedata.h"
#include "vars.h"
#include "vartable.h"
#include "externs.h"

gboolean
write_csv_header (gint *cols, gint ncols, FILE *f, GGobiData *d, ggobid *gg)
{
  gboolean ok = true;
  gint j, jcol, rval;

  fprintf (f, "\"\",");
  for (j=0; j<ncols; j++) {
    jcol = cols[j];
    rval = fprintf (f, "\"%s\"", 
             g_strstrip((gg->save.stage == TFORMDATA) ? ggobi_data_get_transformed_col_name(d, jcol) : ggobi_data_get_col_name(d, jcol)));
    if (rval < 0) {
      ok = false;
      break;
    }
    if (j < ncols-1)
      fprintf(f, ",");
  }
  fprintf (f, "\n");

  return ok;
}

void
write_csv_cell(gint i, gint j, FILE *f, GGobiData *d, ggobid *gg)
{
  vartabled *vt = vartable_element_get (j, d);
  gchar* value = ggobi_data_get_string_value(d, i, j, gg->save.stage == TFORMDATA);

  switch (vt->vartype) {
  case categorical:
    fprintf(f, "\"%s\"", value);
    break;
  default:
    fprintf(f, "%s", value);
  }
}

gboolean
write_csv_record (gint i, gint *cols, gint ncols, FILE *f, GGobiData *d, ggobid *gg)
{
  gboolean ok = true;
  gchar *gstr;
  gint j, jcol;

  /*-- row label if present; else index --*/
  if (d->rowlab && d->rowlab->data
      && (gstr = (gchar *) g_array_index (d->rowlab, gchar *, i))) 
  { 
    fprintf(f, "\"%s\",", g_strstrip(gstr));
  } else
    fprintf(f, "\"%d\",", i);

  /* Source and destination, as strings, if edges are present */
  if (gg->save.edges_p && d->edge.n) {
    fprintf(f, "\"%s\",", g_strstrip(d->edge.sym_endpoints->a));
    fprintf(f, "\"%s\",", g_strstrip(d->edge.sym_endpoints->b));
  }

  /* record */  
  for(j = 0; j < ncols; j++) {
    jcol = cols[j];
  
    /*-- if missing, figure out what to write --*/
    if (ggobi_data_is_missing(d, i, jcol) && gg->save.missing_ind != MISSINGSIMPUTED)
    {
      switch (gg->save.missing_ind) {
      case MISSINGSNA:
        fprintf (f, "NA");
        break;
      default: 
        fprintf (f, ".");
      }
    } else {  /*-- if not missing, just write the data --*/
      write_csv_cell (i, jcol, f, d, gg);
    }
    if (j < ncols-1 )
      fprintf(f, ",");
  }

  return ok;
}

gboolean
write_csv_records (gint *cols, gint ncols, FILE *f, GGobiData *d, ggobid *gg)
{
  gboolean ok = true;
  gint i, m;

  if (gg->save.row_ind == ALLROWS) {
    for (i = 0; i < d->nrows; i++) {
      write_csv_record (i, cols, ncols, f, d, gg);
      fprintf(f, "\n");
    }
  } else {  /*-- if displaying visible rows only --*/
    for (i=0; i<d->nrows_in_plot; i++) {
      m = d->rows_in_plot.els[i];
      write_csv_record (m, cols, ncols, f, d, gg);
      fprintf(f, "\n");
    }
  }

  return ok;
}



gboolean
write_csv_file (FILE *f, GGobiData *d, ggobid *gg)
{
  gboolean ok = false;
  gint j;
  gint *cols, ncols;

  ncols = 0;
  if (gg->save.column_ind == ALLCOLS) {
    cols = (gint *) g_malloc (d->ncols * sizeof(gint));
    for(j = 0; j < d->ncols; j++) {
      cols[j] = j;
      ncols++;
    }
  } else if (gg->save.column_ind == SELECTEDCOLS) {
    /*-- work out which columns to save --*/
    cols = (gint *) g_malloc (d->ncols * sizeof (gint));
    ncols = selected_cols_get (cols, d, gg);
    if (ncols == 0)  // backup source of column selection
      ncols = plotted_cols_get (cols, d, gg);
  }

  if (ncols) {
    if (write_csv_header (cols, ncols, f, d, gg))
      if (write_csv_records (cols, ncols, f, d, gg))
        ok = true;

    g_free (cols);
  }

  return ok;
}

gboolean
write_csv (const gchar *filename,  ggobid *gg)
{
  FILE *f;
  gboolean ok = false;
  GGobiData *d = NULL;
  gint nd = g_slist_length(gg->d);;

  /* By default, write only a single datad */
  if (nd > 0) {
    if (nd == 1)
      d = gg->d->data;
    else {
      if (gg->current_display->d != NULL)
        d = gg->current_display->d; 
    }
  }

  if (d) {
    f = fopen (filename, "w");
    if (f) {
      if (write_csv_file (f, d, gg))
        ok = true;

      fclose(f);
    }
  }
  return ok;
}