summaryrefslogtreecommitdiffstats
path: root/rasodmg/test/defutil.hh
blob: b30a8a50cf5d4456c8f9feb34984f3a8052b1e27 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* 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>.
/
/*
 *  defutil.hh
 *
 *	COMMENTS
 *  Common functions used by defxxx programs
*/


#include <cstdio>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;

#include "raslib/mddtypes.hh"
#include "raslib/structuretype.hh"
#include "raslib/odmgtypes.hh"
#include "raslib/minterval.hh"
#include "raslib/parseparams.hh"
#include "conversion/convertor.hh"
#include "conversion/convfactory.hh"

//--I/O file
int readFile(const char* fileName, char **data, r_ULong& dataSize)
{
 FILE *pFile=NULL;

 if(fileName==NULL)
 {
   cout << "readFile(...) fileName is null" << endl;
   return EXIT_FAILURE;
 }

 if(*data!=NULL)
 {
  cout << "readFile(...) data is not null" << endl;
  return EXIT_FAILURE;
 }
 
 pFile=fopen(fileName, "rb");
 if(pFile==NULL)
 {
  cout << "readFile(...) unable to open file " << fileName << endl;
  return EXIT_FAILURE;
 }
 
 fseek(pFile, 0, SEEK_END);
 dataSize=ftell(pFile);
 
 *data=new char[dataSize];
 if(*data==NULL)
 {
  cout << "readFile(...) unable to claim memory for file " << fileName << endl;
  fclose(pFile);
  return EXIT_FAILURE;
 }
 
 fseek(pFile, 0, SEEK_SET);
 fread(*data, 1, dataSize, pFile);

 fclose(pFile);
 return EXIT_SUCCESS;
}

int writeFile(const char* fileName, const char **data, r_ULong& dataSize)
{
 FILE *pFile=NULL;

 if(fileName==NULL)
 {
  cout << "writeFile(...) fileName is null" << endl;
  return EXIT_FAILURE; 
 }

 if(*data==NULL)
 {
  cout << "writeFile(...) data is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(!dataSize)
 {
  cout << "writeFile(...) dataSize is zero" << endl;
  return EXIT_FAILURE;
 } 

 pFile=fopen(fileName, "wb");
 if(pFile==NULL)
 {
  cout << "writeFile(...) unable to open file" << fileName << endl;
  return EXIT_FAILURE;
 }
 
 fwrite(*data, 1, dataSize, pFile);

 fclose(pFile);
 pFile=NULL;
 
 return EXIT_SUCCESS;
}

//ConvertFrom/To r_Array data
int convertFrom(r_Data_Format fmt, const char* fmtParams,
		const char* data, const r_Minterval& dataDom, 
		const r_Type* dataType, r_convDesc& desc)
{
 r_Storage_Man_CPP mySM;
 r_Convertor *conv=NULL;
 
 if(data==NULL)
 {
  cout << "convertFrom(...) data is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(dataType==NULL)
 {
  cout << "convertFrom(...) dataType is null" << endl;
  return EXIT_FAILURE;
 }
 
 try
 {
  conv=r_Convertor_Factory::create(fmt, data, dataDom, dataType);
  conv->set_storage_handler(mySM);
 }
 catch(r_Error& err)
 {
  cout << "convertFrom(...) request for convertor " << fmt << " failed" << endl
  	   << "Error " << err.get_errorno() << " : "  << err.what() << endl;
  return EXIT_FAILURE;
 }
 
 try
 {
  desc = conv->convertFrom(fmtParams);
 }
 catch(r_Error &err)
 {
  cout << "convertFrom(...) conversion from " << fmt << " format to r_Array format failed! " << endl 
  	   << "Error " << err.get_errorno() << " : "  << err.what() << endl;
  if(conv)
  {
    delete conv;
    conv=NULL;
  }
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}

int convertTo(r_Data_Format fmt, const char* fmtParams,
	      const char* data, const r_Minterval& dataDom, 
	      const r_Type* dataType, r_convDesc& desc)
{
 r_Storage_Man_CPP mySM;
 r_Convertor *conv=NULL;
 
 if(data==NULL)
 {
  cout << "convertTo(...) data is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(dataType==NULL)
 {
  cout << "convertTo(...) dataType is null" << endl;
  return EXIT_FAILURE;
 }
 
 try
 {
  conv=r_Convertor_Factory::create(fmt, data, dataDom, dataType);
  conv->set_storage_handler(mySM);
 }
 catch(r_Error& err)
 {
  cout << "convertTo(...) request for convertor " << fmt << " failed! " << endl
  	   << "Error " << err.get_errorno() << " : "  << err.what() << endl;
  return EXIT_FAILURE;
 }
 
 try
 {
  desc = conv->convertTo(fmtParams);
 }
 catch(r_Error &err)
 {
  cout << "convertTo(...) conversion from " << fmt << " format to r_Array format failed! " << endl 
  	   << "Error " << err.get_errorno() << " : "  << err.what() << endl;
  if(conv)
  {
    delete conv;
    conv=NULL;
  }
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}


//init/deinit ConvDesc
void cleanConvDesc(r_convDesc& desc)
{
 desc.src=NULL;
 desc.srcType=NULL;
  if(desc.dest!=NULL)
 {
  delete []desc.dest;
  desc.dest=NULL;
 }
 if(desc.destType!=NULL)
 {
  delete desc.destType;
  desc.destType=NULL;
 }
 desc.baseType = r_Convertor::ctype_void; 
}

void initConvDesc(r_convDesc& desc)
{
 desc.dest=NULL;
 desc.src=NULL;
 desc.srcType=NULL;
 desc.destType=NULL; 
 desc.baseType = r_Convertor::ctype_void; 
}

//decode a minterval from a string
int decodeMinterval(const char* src, r_Minterval& srcIv)
{
 if(!src)
 {
  cout << "decodeMinterval(...) src is null" << endl;
  return EXIT_FAILURE;
 }
 
 try
 {
  srcIv=r_Minterval(src);
 }
 catch(r_Error& err)
 {
  cout << "decodeMinterval(...) error while constructing the minterval from " << src << endl;
  cout << "Error " << err.get_errorno() << ":" << err.what() << endl; 
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}

//decode a type from a string
int decodeType(const char* src, r_Type*& srcType)
{
 if(!src)
 {
  cout << "decodeType(...) src is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(srcType)
 {
  cout << "decodeType(...) src is not null" << endl;
  return EXIT_FAILURE;
 } 
 
 try
 {
  srcType=r_Type::get_any_type(src);
 }
 catch(r_Error& err)
 {
  cout << "decodeType(...) error while constructing the type from " << src << endl;
  cout << "Error " << err.get_errorno() << ":" << err.what() << endl; 
  return EXIT_FAILURE;
 }
 
 if(!srcType)
 {
  cout << "decodeType(...) the type retrived with r_Type::get_any_type(...) is null" << endl;
  return EXIT_FAILURE;
 }
 
 if(!srcType->isBaseType())
 {
  cout << "decodeType(...) the type retrived (" << srcType->type_id() << ") is not a base type" << endl;
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}

//parse r_Array params 'domain=<domain_str>,type=<type_str>'
int parseArrayParams(const char* formatparams,
		     const char* fpDomain,
		     const char* fpType,
		     char*& domain,
		     char*& type)
{
 const int fpNo=2;
 r_Parse_Params params(fpNo);
 
 //parameter validation
 if(formatparams==NULL) {
  cout << "parseArrayParams(...) formatparams is null" << endl;
  return EXIT_FAILURE;
 }
 if(fpDomain==NULL){
  cout << "parseArrayParams(...) fpDomain is null" << endl;
  return EXIT_FAILURE;
 } 
 if(fpType==NULL){
  cout << "parseArrayParams(...) fpType is null" << endl;
  return EXIT_FAILURE;
 } 
 if(domain!=NULL){
  cout << "parseArrayParams(...) domain is not null" << endl;
  return EXIT_FAILURE;
 } 
 if(type!=NULL){
  cout << "parseArrayParams(...) type is not null" << endl;
  return EXIT_FAILURE;
 } 
 
 params.add(fpDomain, &domain, r_Parse_Params::param_type_string);
 params.add(fpType, &type, r_Parse_Params::param_type_string); 
 
 if(params.process(formatparams) != fpNo)
 {
  cout << "parseArrayParams(...) error parsing " << formatparams << endl;
  return EXIT_FAILURE;
 }
 
 return EXIT_SUCCESS;
}		  

//compare 2 structure type
int compareStructure(r_Structure_Type* src, r_Structure_Type* dest)
{
 r_Structure_Type::attribute_iterator iterSrc, iterDest;
 
 if(src==NULL) 
 {
   cout << "compareStructure(...) src is null !"<< endl;
   return EXIT_FAILURE;
 }
 
 if(dest==NULL)
 {
   cout << "compareStructure(...) dest is null !"<< endl;
   return EXIT_FAILURE;  
 }

 iterSrc=src->defines_attribute_begin();
 iterDest=dest->defines_attribute_begin();
 while( iterSrc!=src->defines_attribute_end() || iterDest!=dest->defines_attribute_end())
 {
   r_Type::r_Type_Id typeSrcId=r_Type::UNKNOWNTYPE, typeDestId=r_Type::UNKNOWNTYPE;
   typeSrcId=(*iterSrc).type_of().type_id();
   typeDestId=(*iterDest).type_of().type_id();
   
   if(typeSrcId!=typeDestId)
   {
     cout << "comparaStructure(...) typeSrcId(" << typeSrcId 
          << ") != typeDestId(" << typeDestId << ") !"<< endl;
     return EXIT_FAILURE;
   }
     
   if((typeSrcId== r_Type::STRUCTURETYPE) && 
   	  (compareStructure((r_Structure_Type*)(&((*iterSrc).type_of())), 
		                (r_Structure_Type*)(&((*iterDest).type_of()))) != EXIT_SUCCESS))
     return EXIT_FAILURE;
     
   iterSrc++;
   iterDest++;
 }
 if((iterSrc==src->defines_attribute_end()) &&
    (iterDest==dest->defines_attribute_end()))
   return EXIT_SUCCESS;
 else
 {
   if(iterSrc!=src->defines_attribute_end())
    cout << "compareStructure(...) src has more atributes then dest!" << endl;
   else
    cout << "compareStructure(...) dest has more atributes then src!" << endl;    
   return EXIT_FAILURE; 
 }
}