ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat_io2.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 #define __USE_IOSTREAM__
12 
13 #include "fvar.hpp"
14 
15 #if defined(__TURBOC__)
16  #pragma hdrstop
17  #include <iostream.h>
18  #include <iomanip.h>
19  #include <fstream.h>
20  #include <strstrea.h>
21 #endif
22 
23 #ifdef __SUN__
24  #include <iostream.h>
25  #include <iomanip.h>
26  #include <fstream.h>
27  #include <limits.h>
28  #include <strstream.h>
29 #endif
30 
31 #ifdef __ZTC__
32  #include <iostream.hpp>
33  #if (__ZTC__ < 0x310)
34  #include <sstream.hpp>
35  #else
36  #include <strstream.h>
37  #endif
38 #endif
39 
40 #ifdef __NDPX__
41  #include <iostream.h>
42  #include <sstream.h>
43 #endif
44 
45 #include <string.h>
46 #include <ctype.h>
47 
48 #ifndef OPT_LIB
49  #include <cassert>
50  #include <climits>
51 #endif
52 
53 int mystrlen(const char* line);
54 
55 const int MAX_LINE_LENGTH = 10000;
56 const int MAX_FIELD_LENGTH = 500;
57 const int MAX_NUMBER_COLUMNS = 6550;
58 const int MAX_NUMBER_ROWS = 6550;
59 
60 int get_non_blank_line(const ifstream& infile,char * & line,
61  const int& line_length);
62 
67  struct dvec_ptr_ptr
68  {
69  void ** m;
70  };
71 
72 const int MAXROWS = 5050;
73 
79 {
80  #ifdef DIAG
81  myheapcheck("Entering dmatrix( char * s)" );
82  #endif
83 
84 #ifdef OPT_LIB
85  const int n = (int)strlen(s);
86 #else
87  const size_t len = strlen(s);
88  assert(len <= INT_MAX);
89  const int n = (int)len;
90 #endif
91  int braces = 0;
92  int nrow = 0;
93  int ncol = 0;
94 
95  ivector columns(1, MAXROWS);
96  ivector k1(1, MAXROWS);
97  ivector k2(1, MAXROWS);
98 
99  for (int k = 0; k < n; k++)
100  {
101  if (s[k] == '{')
102  {
103  braces ++;
104  if (braces != 1)
105  {
106  cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
107  cerr << "at character " << k << "\n";
108  ad_exit(1);
109  }
110  ncol = 1;
111  k1[nrow+1] = k;
112  }
113  else if (s[k] == '}')
114  {
115  braces --;
116  if (braces != 0)
117  {
118  cerr << "Unbalanced braces in dmatrix::dmatrix( char * s)\n";
119  cerr << "at character " << k << "\n";
120  ad_exit(1);
121  }
122  k2[nrow+1] = k;
123  nrow ++;
124  if (nrow > MAXROWS)
125  {
126  cerr << "Too many rows in dmatrix::dmatrix( char * s)\n";
127  ad_exit(1);
128  }
129  columns[nrow] = ncol;
130  }
131  else if (s[k] == ',')
132  {
133  if (braces != 0)
134  {
135  ncol++;
136  }
137  }
138  }
139 
140  if (braces != 0)
141  {
142  cerr << "Unbalanced braces in dmatrix::dmatrix(char * s)\n";
143  cerr << s << "\n";
144  ad_exit(1);
145  }
146 
147  if (nrow > 0)
148  {
149  ivector ub(1,nrow);
150  ivector lb(1,nrow);
151  for (int i=1; i<=nrow; i++)
152  {
153  ub[i] = columns[i];
154  lb[i] = 1;
155  }
156  index_min=1;
157  index_max=nrow;
158  if ( (m = new dvector[rowsize()]) == 0)
159  {
160  cerr << " Error allocating memory in dmatrix contructor\n";
161  ad_exit(21);
162  }
163  if ( (shape = new mat_shapex(m))== 0)
164  {
165  cerr << " Error allocating memory in dmatrix contructor\n";
166  ad_exit(21);
167  }
168 
169 
170  #ifdef DIAG
171  cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
172  #endif
173 
174  m -= rowmin();
175 
176  //char * t = (char*) new[strlen(s)+1];
177  char *t = new char[strlen(s)+1];
178  for (int i=rowmin(); i<=rowmax(); i++)
179  {
180  for (int k = k1[i]; k <= k2[i]; k++)
181  {
182  t[k-k1[i]] = s[k];
183  }
184  t[k2[i]-k1[i]+1] = '\0';
185 
186  m[i].allocate(t);
187  }
188  delete[] t;
189  t = 0;
190  }
191  else // no rows implies s is a file name
192  {
193  char * filename = s;
194  ifstream infile(filename);
195  if (!infile)
196  {
197  cerr << "Error opening file " << filename << " in dmatrix constructor "
198  << "dmatrix::dmatrix(char * filename)\n";
199  ad_exit(1);
200  }
201  char *line = new char [MAX_LINE_LENGTH+2];
202 
203  int i=0;
204  ivector nc(1,MAX_NUMBER_ROWS);
205 
206  //while ( (infile.getline(line,MAX_LINE_LENGTH)).good() )
207  while ( get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
208  {
209 #ifndef OPT_LIB
210  assert(strlen(line) > 0);
211 #endif
212  strcat(line, " ");
213  // increment row counter
214  if ( i++ > MAX_NUMBER_ROWS)
215  {
216  cerr << " MAX_NUMBER_ROWS exceeded in "
217  " dmatrix::dmatrix(char * filename)\n";
218  ad_exit(21);
219  }
220 
221  int j = 0;//counts columns
222  size_t index = 0;
223  size_t length = strlen(line);
224  while (index < length)
225  {
226  char c = line[index];
227  while (c != ' ' && c != ',')
228  {
229  ++index;
230  c = line[index];
231  }
232  ++j;
233  if (j > MAX_NUMBER_COLUMNS)
234  {
235  cerr << " MAX_NUMBER_COLUMNS exceeded in "
236  " dmatrix::dmatrix(char * filename)\n";
237  ad_exit(1);
238  }
239  while (c == ' ' || c == ',')
240  {
241  ++index;
242  if (index >= length) break;
243 
244  c = line[index];
245  }
246  }
247  // Need to check error status f
248  nc[i] = j;
249  }
250 
251  int nr=i;
252  if (nr == 0)
253  {
254  cerr << "Error in dmatrix constructor There doesn't seem to be any data\n"
255  << "in file " << filename
256  << " caled in dmatrix::dmatrix(char * filename)\n";
257  ad_exit(1);
258  }
259 
260  infile.clear();
261  infile.seekg(0,ios::beg);
262 
263  ivector index_up(1,nr);
264  ivector index_down(1,nr);
265  int One=1;
266  int Zero=0;
267  index_down.fill_seqadd(One,Zero);
268 
269  for (i=1;i<=nr;i++)
270  {
271  index_up[i]=nc[i];
272  }
273  index_min=1;
274  index_max=nr;
275 
276  if ( (m = new dvector[rowsize()]) == 0)
277  {
278  cerr << " Error allocating memory in dmatrix contructor\n";
279  ad_exit(21);
280  }
281  if ( (shape = new mat_shapex(m))== 0)
282  {
283  cerr << " Error allocating memory in dmatrix contructor\n";
284  ad_exit(21);
285  }
286 
287 #ifdef DIAG
288  cerr << "Created a dmatrix with adress "<< farptr_tolong(m)<<"\n";
289 #endif
290 
291  m -= rowmin();
292 
293  for (i=rowmin(); i<=rowmax(); i++)
294  {
295  m[i].allocate(index_down[i],index_up[i]);
296 #ifdef DIAG
297  cerr << "Created a dvector with address "<< farptr_tolong((void*)(m+i))<<"\n";
298 #endif
299  }
300 #ifdef DIAG
301  myheapcheck("Leaving dmatrix(nrl,nrh,ncl,nch)" );
302 #endif
303 
304  char* field = new char[MAX_FIELD_LENGTH + 1];
305 
306  i=0;
307  while (get_non_blank_line(infile,line,MAX_LINE_LENGTH) )
308  {
309  strcat(line," ");
310  // increment row counter
311  i++;
312 
313  int j=0; // j counts columns
314  size_t index = 0;
315  size_t length = strlen(line);
316  while (index < length)
317  {
318  int field_index = 0;
319  char c = line[index];
320  while (c != ' ' && c != ',')
321  {
322  field[field_index] = c;
323  ++field_index;
324 
325  ++index;
326  c = line[index];
327  }
328  field[field_index] = '\0';
329  ++j;
330  char * err_ptr;
331  elem(i,j)=strtod(field,&err_ptr); // increment column counter
332 
333  if (isalpha((unsigned char)err_ptr[0]))
334  {
335  cerr << "Error decoding field " << filename
336  << " in dmatrix::dmatrix(char * filename) " << "\n";
337  cerr << "Error occurred in line " << i << " at field " << j << "\n";
338  cerr << "Offending characters start with "
339  << err_ptr[0]
340  << err_ptr[1]
341  << err_ptr[2]
342  << err_ptr[3] << "\n";
343  ad_exit(1);
344  }
345  if (elem(i,j) == HUGE_VAL ||elem(i,j) == -HUGE_VAL)
346  {
347  cerr << "Overflow Error decoding field " << filename
348  << " in dmatrix::dmatrix(char * filename) " << "\n";
349  cerr << "Error occurred in line " << i << " at field " << j << "\n";
350  cerr << "Offending characters start with "
351  << err_ptr[0]
352  << err_ptr[1]
353  << err_ptr[2]
354  << err_ptr[3] << "\n";
355  ad_exit(1);
356  }
357  if (j > MAX_NUMBER_COLUMNS)
358  {
359  cerr << " MAX_NUMBER_COLUMNS exceeded in "
360  " dmatrix::dmatrix(char * filename)\n";
361  ad_exit(1);
362  }
363  while (c == ' ' || c == ',')
364  {
365  ++index;
366  if (index >= length) break;
367 
368  c = line[index];
369  }
370  }
371  }
372  delete[] field;
373  field = 0;
374 
375  delete[] line;
376  line = 0;
377  }
378 }
379 
385  const ifstream& _infile,
386  char* &line,
387  const int& line_length)
388 {
389  ifstream& infile = (ifstream&)_infile;
390 
391  int peek = infile.peek();
392  while (infile.good() && (iscntrl(peek) || isspace(peek)))
393  {
394  infile.get();
395  peek = infile.peek();
396  }
397 
398  infile.get(line, line_length);
399 
400  return infile.good() ? mystrlen(line) : 0;
401 }
402 
407 int mystrlen(const char* line)
408 {
409  int ii = 0;
410  while(ii < MAX_LINE_LENGTH)
411  {
412  if (line[ii]=='\0')
413  {
414  return ii;
415  }
416  ii++;
417  }
418  return -1;
419 }
int index_min
Definition: fvar.hpp:2822
void myheapcheck(char *msg)
Does nothing.
Definition: dvector.cpp:669
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
Description not yet available.
Definition: fvar.hpp:2030
const int MAX_LINE_LENGTH
Definition: dmat_io2.cpp:55
void ** m
Definition: dmat.cpp:13
dmatrix(void)
Default constructor.
Definition: dmat0.cpp:16
Vector of double precision numbers.
Definition: dvector.h:50
const int MAX_FIELD_LENGTH
Definition: dmat_io2.cpp:56
int mystrlen(const char *line)
Description not yet available.
Definition: dmat_io2.cpp:407
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
Definition: dvector.cpp:409
exitptr ad_exit
Definition: gradstrc.cpp:53
int index_max
Definition: fvar.hpp:2823
mat_shapex * shape
Definition: fvar.hpp:2825
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
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
int rowmax() const
Definition: fvar.hpp:2929
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvector * m
Definition: fvar.hpp:2824
dvector & elem(int i)
Definition: fvar.hpp:3011
const int MAXROWS
Definition: dmat_io2.cpp:72
unsigned int rowsize() const
Definition: fvar.hpp:2934
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
int rowmin() const
Definition: fvar.hpp:2925