summaryrefslogtreecommitdiffstats
path: root/conversion/csv.cc
blob: 7babcbe76193167731e8402fa5d7d3f8213ef291 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: csv.cc
 *
 * MODULE:  conversion
 *
 * CLASSES: r_Conv_CSV
 *
 * COMMENTS:
 *
 * Provides functions to convert data to CSV SD and back.
 *
*/

/* Added by Sorin Stancu-Mara. Definition clashed for type int8, define in both 
* /usr/include/csv.h and in /usr/include/tiff.h
* This will supress the tiff.h definition.
* Both definitions are similar
*/
#define HAVE_INT8

#include "conversion/csv.hh"
#include "raslib/error.hh"
#include "raslib/rminit.hh"
#include "raslib/parseparams.hh"
#include "raslib/primitivetype.hh"
#include <iostream>
#include <fstream>
#include <cstring>

#include "csv.hh"

#include <stdio.h>
#include <iostream>


r_Conv_CSV::r_Conv_CSV(const char *src, const r_Minterval &interv, const r_Type *tp) throw(r_Error) 
: r_Convertor(src, interv, tp, true)
{
  if (tp->isStructType())
  {
    RMInit::logOut << "r_Conv_CSV::r_Conv_CSV(): structured types not supported." << endl;
    throw r_Error(r_Error::r_Error_General);
  }
}



r_Conv_CSV::r_Conv_CSV(const char *src, const r_Minterval &interv, int tp) throw(r_Error)
: r_Convertor(src, interv, tp)
{
}


/*
int r_Conv_HDF::getTypeSize(int intType, char *format)
{

  switch (intType)
  {
    case ctype_int8: strcpy(format, "%c"); return 1; break;
    case ctype_uint8:
    case ctype_char:
    case ctype_bool: return 1; break;
    case ctype_int16: return 2; break;
    case ctype_uint16: return 2; break;
    case ctype_int32: return 4; break;
    case ctype_uint32: return 4; break;
    case ctype_int64: return 8; break;
    case ctype_uint64: return 8; break;
    case ctype_float32: return 4; break;
    case ctype_float64: return 8; break;
    default: return 1; break;
  }
  return 1;
}
*/

r_Conv_CSV::~r_Conv_CSV(void)
{
}

template <class baseType, class castType> 
void r_Conv_CSV::print(std::ofstream &f, baseType* val, int *dims, int dim) {
  if (dim==1) {
    for (int i=0; i<dims[0]-1; ++i, val++)
      f << (castType)val[0] << ",";
    f << (castType) val[dims[0]-1]; val++;
  } else {
    for (int i=0; i<dims[0]-1; ++i, val++) {
      f << "{";
      print<baseType, castType>(f, val, dims+1, dim-1);
      f << "},";
    }
      f << "{";
      print<baseType, castType>(f, val, dims+1, dim-1);
      f << "}";    
  }
}


r_convDesc &r_Conv_CSV::convertTo( const char *options ) throw(r_Error)
{
  char name[256];
  strncpy(name, tmpnam(NULL), 256);
  std::ofstream ftemp(name);
  //int size = getTypeSize(desc.baseType);
  int rank, i;
  int *dimsizes;
  rank = desc.srcInterv.dimension();
  const char *src = desc.src;

  char *t;

  dimsizes = new int[rank]; 

  for (i=0; i<rank; i++)
  {
    dimsizes[i] = desc.srcInterv[i].high() - desc.srcInterv[i].low() + 1;
  }

  switch (desc.baseType)
    {
    case ctype_int8: print<const r_Octet, int>(ftemp, (const r_Octet* &)src, dimsizes, rank); break;
    case ctype_uint8: 
    case ctype_char: 
    case ctype_bool: print<r_Char, int>(ftemp, (r_Char* &)src, dimsizes, rank); break;      
    case ctype_int16: print<r_Short, int>(ftemp, (r_Short* &)src, dimsizes, rank); break;
    case ctype_uint16: print<r_UShort, int>(ftemp, (r_UShort* &) src, dimsizes, rank); break;
    case ctype_int32: print<r_Long, int>(ftemp, (r_Long* &) src, dimsizes, rank);  break;
    case ctype_uint32: print<r_ULong, int>(ftemp, (r_ULong* &) src, dimsizes, rank);  break;
    case ctype_int64: print<long long, long long>(ftemp, (long long* &) src, dimsizes, rank); break;
    case ctype_uint64: print<unsigned long long, unsigned long long>(ftemp, (unsigned long long* &) src, dimsizes, rank); break;
    case ctype_float32: print<r_Float, float>(ftemp, (r_Float* &) src, dimsizes, rank); break;
    case ctype_float64: print<r_Double, float>(ftemp, (r_Double* &) src, dimsizes, rank); break;
    default: print<r_Char, int>(ftemp, (r_Char* &)src, dimsizes, rank);
  }

  delete [] dimsizes; dimsizes=NULL;
  ftemp.close();

  FILE *fp;
  int filesize;
  
  if ((fp = fopen(name, "rb")) == NULL)
  {
    RMInit::logOut << "r_Conv_CSV::convertTo(): unable to read back file." << endl;
    throw r_Error(r_Error::r_Error_General);
  }
  fseek(fp, 0, SEEK_END);
  filesize = ftell(fp);

  desc.destInterv = r_Minterval(1);
  desc.destInterv << r_Sinterval((r_Range)0, (r_Range)filesize - 1);

  if ((desc.dest = (char*)mystore.storage_alloc(filesize)) == NULL)
  {
    RMInit::logOut << "r_Conv_CSV::convertTo(): out of memory error" << endl;
    fclose(fp);
    throw r_Error(MEMMORYALLOCATIONERROR);
  }
  fseek(fp, 0, SEEK_SET);
  fread(desc.dest, 1, filesize, fp);

  fclose(fp);

  remove(name);

  // Result is just a bytestream
  desc.destType = r_Type::get_any_type("char");

  return desc;
}



r_convDesc &r_Conv_CSV::convertFrom(const char *options) throw(r_Error)
{
  RMInit::logOut << "importing CSV data not yet implemented" << endl;
  throw new r_Error(CONVERSIONFORMATNOTSUPPORTED);
  return desc;
}



const char *r_Conv_CSV::get_name( void ) const
{
  return "csv";
}


r_Data_Format r_Conv_CSV::get_data_format( void ) const
{
  return r_CSV;
}


r_Convertor *r_Conv_CSV::clone( void ) const
{
  return new r_Conv_CSV(desc.src, desc.srcInterv, desc.baseType);
}