summaryrefslogtreecommitdiffstats
path: root/conversion/test/test_vff.cc
blob: e3d6016895ff7c62a19892928bfcaf859dfb392e (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>

#ifdef EARLY_TEMPLATE
#define __EXECUTABLE__
#endif

#include "conversion/vff.hh"
#include "raslib/rminit.hh"
#include "raslib/basetype.hh"

#ifdef __GNUG__
#include "rasodmg/transaction.hh"
#include "raslib/template_inst.hh"
#else
RMINITGLOBALS('C')
#endif



static void PrintUsage(const char *name)
{
  cout << "Usage: " << name << " [-o <vffoutfile> -p <params> -h] <vffinfile>" << endl;
}


int main(int argc, char *argv[])
{
  const char *infile=NULL;
  const char *outfile=NULL;
  const char *params=NULL;
  int i;

  i=1;
  while (i<argc)
  {
    if (argv[i][0] == '-')
    {
      switch (argv[i][1])
      {
	case 'o':
	  outfile = argv[++i]; break;
	case 'p':
	  params = argv[++i]; break;
	case 'h':
	  PrintUsage(argv[0]); exit(0);
	default:
	  cerr << "Unknown switch " << argv[i] << endl;
	  break;
      }
    }
    else
    {
      if (infile != NULL)
	cerr << "More than one input file, ignored" << endl;
      else
	infile = argv[i];
    }
    i++;
  }

  if (infile == NULL)
  {
    PrintUsage(argv[0]);
    exit(-1);
  }

  FILE *fp;
  if ((fp = fopen(infile, "rb")) == NULL)
  {
    cerr << "Unable to open input file " << infile << endl;
    return -1;
  }

  size_t fsize;
  char *data;

  fseek(fp, 0, SEEK_END);
  fsize = ftell(fp);
  
  if ((data = new char[fsize]) == NULL)
  {
    cerr << "Unable to claim memory for file" << endl;
    fclose(fp);
    return -1;
  }
  fseek(fp, 0, SEEK_SET);
  fread(data, 1, fsize, fp);
  fclose(fp);

  r_Minterval interv(1);
  interv << r_Sinterval((r_Range)0, (r_Range)fsize-1);
  r_Conv_VFF conv(data, interv, r_Convertor::ctype_char);
  r_convDesc desc;

  cout << "Convert from VFF... " << flush;
  try
  {
    desc = conv.convertFrom(params);
    cout << "OK" << endl;

    r_Conv_VFF convb(desc.dest, desc.destInterv, desc.destType);
    r_convDesc descb;

    cout << "Convert back to VFF... " << flush;
    try
    {
      descb = convb.convertTo(params);
      cout << "OK" << endl;

      if (outfile != NULL)
      {
	if ((fp = fopen(outfile, "wb")) == NULL)
	{
	  cerr << "Unable to write to file " << outfile << endl;
	}
	else
	{
	  fsize = (size_t)(descb.destInterv[0].high() - descb.destInterv[0].low() + 1);
	  fwrite(descb.dest, 1, fsize, fp);
	  fclose(fp);
	}
      }

      // endianness-safe comparison of binary data:
      // convert the just created VFF data back to an MDD and compare the
      // result with the first MDD thus converted.
      r_Conv_VFF convc(descb.dest, descb.destInterv, descb.destType);
      r_convDesc descc;

      cout << "Compare binary data... " << flush;
      try
      {
	descc = convc.convertFrom(params);
	if (memcmp(descc.dest, desc.dest, desc.destInterv.cell_count() * ((r_Base_Type*)desc.destType)->size()) == 0)
	  cout << "identical" << endl;
	else
	  cout << "differs!!!" << endl;

	delete descc.destType;
	free(descc.dest);
      }
      catch (r_Error &err)
      {
	cerr << "failed: " << err.what() << endl;
      }

      delete descb.destType;
      free(descb.dest);
    }
    catch (r_Error &err)
    {
      cerr << "failed: " << err.what() << endl;
    }
    delete desc.destType;
    free(desc.dest);
  }
  catch (r_Error &err)
  {
    cerr << "failed: " << err.what() << endl;
  }

  delete [] data;

  return 0;
}