ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvec_io1.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #if defined(__TURBOC__)
14  #include <iostream.h>
15  #include <strstrea.h>
16 #endif
17 #ifdef __NDPX__
18  #include <iostream.h>
19  #include <sstream.h>
20 #endif
21 
22 #include <string.h>
23 #include <ctype.h>
24 
25 const unsigned int MAX_LINE_LENGTH = 10000;
26 const int MAX_FIELD_LENGTH = 500;
27 const int MAX_NUMBER_COLUMNS = 6550;
28 const int MAX_NUMBER_ROWS = 6550;
29 
30 int get_non_blank_line(const ifstream& infile, char* & line,
31  const int& line_length);
32 
37  struct dvec_ptr_ptr
38  {
39  void ** m;
40  };
41 
46 dvector::dvector(char* filename, const int& column)
47 {
48  if (column < 1)
49  {
50  cerr << "Error[" << __FILE__ << ':' << __LINE__
51  << "]: column should be positive number.\n";
52  ad_exit(1);
53  }
54  ifstream infile(filename);
55  if (!infile)
56  {
57  cerr << "Error opening file " << filename << " in dmatrix constructor "
58  << "dmatrix::dmatrix(char * filename)\n";
59  ad_exit(1);
60  }
61  char* line = new char[MAX_LINE_LENGTH + 2];
62  char* field = new char[MAX_FIELD_LENGTH + 1];
63 
64  int i = 0;
65  ivector nc(1, MAX_NUMBER_ROWS);
66 
67  while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
68  {
69  strcat(line, " ");
70 
71  // increment row counter
72  if ( i++ > MAX_NUMBER_ROWS)
73  {
74  cerr << " MAX_NUMBER_ROWS exceeded in "
75  " dmatrix::dmatrix(char * filename)\n";
76  ad_exit(21);
77  }
78 
79  int j = 0; // j counts columns
80 #ifdef __ZTC__
81  while( sscanf(line,"%s",field)) // reads a field from line into field
82 #else
83  size_t index = 0;
84  size_t length = strlen(line);
85  while (index < length)
86 #endif
87  {
88  char c = line[index];
89  while (c != ' ' && c != ',')
90  {
91  ++index;
92  c = line[index];
93  }
94  ++j;
95  if (j > MAX_NUMBER_COLUMNS)
96  {
97  cerr << " MAX_NUMBER_COLUMNS exceeded in "
98  " dvector::dvector(char* filename)\n";
99  ad_exit(21);
100  }
101  while (c == ' ' || c == ',')
102  {
103  ++index;
104  if (index >= length) break;
105 
106  c = line[index];
107  }
108  }
109  // Need to check error status f
110  if (j < column)
111  {
112  cerr << "Error -- not enough columns in line " << i
113  << "\n in dvector::dvector(char * filename, const int& column) "
114  " in file: "
115  << filename << "\n";
116  ad_exit(1);
117  }
118  }
119  int nr = i;
120  if (nr == 0)
121  {
122  cerr << "Error in dvector constructor There doesn't seem to be any data\n"
123  << "in file: " << filename
124  << " called in dvector::dvector(char * filename,const const& column)\n";
125  ad_exit(1);
126  }
127  infile.clear();
128  infile.seekg(0,ios::beg);
129 
130  index_min = 1;
131  index_max = nr;
132 
133  if ((v = new double[(size_t) size()]) == 0)
134  {
135  cerr << " Error trying to allocate memory for dvector\n";
136  ad_exit(21);
137  }
138 #if defined(THREAD_SAFE)
139  if ( (shape=new ts_vector_shapex(1,nr,v)) == NULL)
140 #else
141  if ( (shape=new vector_shapex(1,nr,v)) == NULL)
142 #endif
143  {
144  cerr << "Error trying to allocate memory for dvector\n";
145  ad_exit(21);
146  }
147 
148 #ifdef DIAG
149  cout << "Created a ncopies with address " << _farptr_tolong(&(shape->ncopies))
150  <<"\n";
151  cout << "Created a dvector with address " << _farptr_tolong(v) <<"\n";
152  if (sizeof(int)==sizeof(char*))
153  {
154 #if (defined(__GNUC__) && defined(__i386)) || (defined(_MSC_VER) && defined(_M_IX86))
155  if ( (unsigned) v < indexmin() * sizeof(double) )
156 #else
157  if ((intptr_t)v < indexmin() * sizeof(double))
158 #endif
159  {
160  //cerr << "Pointer wrap in dvector(unsigned int ncl, unsigned int nch)\n";
161  //cerr << "pointer = "<< (unsigned int) v <<
162  //" indexmin() = "<<indexmin()<<"\n";
163  void denormalize_ptr(void * ptr, unsigned int byte_offset);
164  denormalize_ptr(&v, indexmin() * sizeof(double));
165  }
166  }
167 #endif
168 
169  v -= indexmin();
170 
171  i=0;
172  // while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
173  while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
174  {
175  strcat(line," ");
176  // increment row counter
177  i++;
178  int j=0; // j counts columns
179 #ifdef __ZTC__
180  while( sscanf(line,"%s",field)) // reads a field from line into field
181 #else
182  size_t index = 0;
183  size_t length = strlen(line);
184  while (index < length)
185 #endif
186  {
187  int field_index = 0;
188  char c = line[index];
189  while (c != ' ' && c != ',')
190  {
191  field[field_index] = c;
192  ++field_index;
193 
194  ++index;
195  c = line[index];
196  }
197  field[field_index] = '\0';
198  ++j;
199  if (j == column)
200  {
201  char* err_ptr;
202  elem(i)=strtod(field,&err_ptr); // increment column counter
203 
204  if (isalpha((unsigned char)err_ptr[0]))
205  {
206  cerr << "Error decoding field " << filename
207  << " in dvector::dvector(char* filename) " << "\n";
208  cerr << "Error occurred in line " << i << " at field " << j << "\n";
209  cerr << "Offending characters start with "
210  << err_ptr[0]
211  << err_ptr[1]
212  << err_ptr[2]
213  << err_ptr[3] << "\n";
214  ad_exit(1);
215  }
216  if (elem(j) == HUGE_VAL || elem(j) == -HUGE_VAL)
217  {
218  cerr << "Overflow Error decoding field " << filename
219  << " in dvector::dvector(char * filename) " << "\n";
220  cerr << "Error occurred in line " << i << " at field " << j << "\n";
221  cerr << "Offending characters start with "
222  << err_ptr[0]
223  << err_ptr[1]
224  << err_ptr[2]
225  << err_ptr[3] << "\n";
226  ad_exit(1);
227  }
228  }
229  if (j > MAX_NUMBER_COLUMNS)
230  {
231  cerr << " MAX_NUMBER_COLUMNS exceeded in "
232  " dmatrix::dmatrix(char * filename)\n";
233  ad_exit(1);
234  }
235  while (c == ' ' || c == ',')
236  {
237  ++index;
238  if (index >= length) break;
239 
240  c = line[index];
241  }
242  }
243  }
244 
245  delete [] line;
246  line = NULL;
247 
248  delete [] field;
249  field = NULL;
250 }
double * v
pointer to the data
Definition: dvector.h:53
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat.cpp:11
const int MAX_NUMBER_ROWS
Definition: dmat_io2.cpp:58
dvector()
Construct a dvector without allocating memory.
Definition: dvector.cpp:378
double & elem(int i)
Definition: dvector.h:152
const int MAX_LINE_LENGTH
Definition: dmat_io2.cpp:55
void ** m
Definition: dmat.cpp:13
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
const int MAX_FIELD_LENGTH
Definition: dmat_io2.cpp:56
exitptr ad_exit
Definition: gradstrc.cpp:53
void denormalize_ptr(void *ptr, unsigned int byte_offset)
Description not yet available.
Definition: denorm.cpp:16
int get_non_blank_line(const ifstream &infile, char *&line, const int &line_length)
Description not yet available.
Definition: dmat_io2.cpp:384
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int index_min
minimum valid subscript
Definition: dvector.h:54
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
vector_shapex * shape
pointer to vector &quot;shape&quot;
Definition: dvector.h:60
dvector column(const dmatrix &matrix, int j)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat6.cpp:13
int index_max
maximum valid subscript
Definition: dvector.h:55
const int MAX_NUMBER_COLUMNS
Definition: dmat_io2.cpp:57
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228