ESTER
Evolution STEllaire en Rotation
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
parser.h
Go to the documentation of this file.
1 #ifndef _PARSER_H
2 #define _PARSER_H
3 
4 #include "ester-config.h"
5 #include <stdio.h>
6 #include <iostream>
7 #include <sstream>
8 #include <typeinfo>
9 #include <string.h>
10 #include "matrix.h"
11 
13  int argc,i;
14  char **argv;
15 public:
16  void open(int argc_in,char *argv_in[]);
17  int get(char *&arg,char *&val);
18  void ack(char *arg,char *val);
19  void close();
20 };
21 
22 class file_parser {
23  FILE *fp;
24  int iline;
25  char line[1024];
26 public:
27  int open(const char *file);
28  int get(char *&arg,char *&val);
29  void close();
30 };
31 
32 class OUTFILE {
33  FILE *fp;
34  char mode;
35  void write_tag(const char *tag,unsigned long n);
36 public:
37  int open(const char *name, char mode='b');
38  void write(const char *tag,const matrix *);
39  void write(const char *tag,const matrix_map *);
40  void write(const char *tag,matrix *m) {write(tag,(const matrix *) m);};
41  void write(const char *tag,matrix_map *m) {write(tag,(const matrix_map *) m);};
42  template <class T>
43  void write(const char *tag,T *x,unsigned long n=1) {
44  size_t size=sizeof(T);
45  if(mode=='t') {
46  if(typeid(x[0])==typeid(char)) write_tag(tag,1);
47  else write_tag(tag,n);
48  } else write_tag(tag,n*size);
49  if(mode=='t') {
50  std::stringstream temp;
51  std::string str;
52  temp.setf(std::ios::scientific, std::ios::floatfield);
53  for(unsigned long i=0;i<n;i++) {
54  temp.str("");
55  temp.precision(16);
56  temp<<x[i]<<std::endl;
57  temp>>str;
58  fprintf(fp,"%s",str.c_str());
59  if(typeid(x[i])!=typeid(char)||i==n-1) fprintf(fp,"\n");
60  }
61  } else fwrite(x,size,n,fp);
62  };
63  template <class T>
64  void write_fmt(const char *tag,const char *fmt,T *x,unsigned long n=1) {
65  write_tag(tag,n);
66  for(unsigned long i=0;i<n;i++) {
67  fprintf(fp,fmt,x[i]);
68  fprintf(fp,"\n");
69  }
70  };
71  void close();
72 };
73 
74 class INFILE {
75  FILE *fp;
76  char mode;
77  unsigned long seek(const char *tag);
78  char *getline(char *,int n);
79 public:
80  int open(const char *name, char mode='b');
81  unsigned long len(const char *tag);
82  int read(const char *tag,matrix *);
83  int read(const char *tag,matrix_map *);
84  template <class T>
85  int read(const char *tag,T *x) {
86  unsigned long n=seek(tag);
87  if(!n) return 1;
88  if(mode=='t') {
89  char str[512];
90  if(typeid(x[0])==typeid(char)) {
91  getline(str,512);
92  strcpy((char *)x,str);
93  } else {
94  for(unsigned long i=0;i<n;i++) {
95  getline(str,512);
96  std::stringstream temp(str);
97  temp>>x[i];
98  }
99  }
100  }
101  else if(n!=fread(x,1,n,fp)) return 1;
102  return 0;
103  };
104  template <class T>
105  int read_fmt(const char *tag,const char *fmt,T *x) {
106  unsigned long n=seek(tag);
107  if(!n) return 0;
108  for(unsigned long i=0;i<n;i++)
109  if(!fscanf(fp,fmt,&x[i])) return 0;
110  return 1;
111  };
112  void close();
113 };
114 
115 
116 
117 
118 #endif
119 
char mode
Definition: parser.h:34
Definition: matrix.h:287
char ** argv
Definition: parser.h:14
void close()
Definition: iofile.cpp:143
void write(const char *tag, T *x, unsigned long n=1)
Definition: parser.h:43
void write_tag(const char *tag, unsigned long n)
Definition: iofile.cpp:39
Definition: parser.h:22
int open(const char *name, char mode='b')
Definition: iofile.cpp:109
The matrix class is used to facilitate the work with regular dense matrices.
Definition: matrix.h:9
unsigned long seek(const char *tag)
Definition: iofile.cpp:165
FILE * fp
Definition: parser.h:23
char line[1024]
Definition: parser.h:25
void write(const char *tag, matrix_map *m)
Definition: parser.h:41
void write(const char *tag, const matrix *)
Definition: iofile.cpp:51
void write(const char *tag, matrix *m)
Definition: parser.h:40
Definition: parser.h:74
unsigned long len(const char *tag)
Definition: iofile.cpp:207
void close()
Definition: iofile.cpp:33
int read(const char *tag, T *x)
Definition: parser.h:85
Definition: parser.h:32
void close()
Definition: parser.cpp:57
FILE * fp
Definition: parser.h:75
char * getline(char *, int n)
Definition: iofile.cpp:149
void open(int argc_in, char *argv_in[])
Definition: parser.cpp:8
char mode
Definition: parser.h:76
void write_fmt(const char *tag, const char *fmt, T *x, unsigned long n=1)
Definition: parser.h:64
int iline
Definition: parser.h:24
void close()
Definition: parser.cpp:103
FILE * fp
Definition: parser.h:33
int open(const char *name, char mode='b')
Definition: iofile.cpp:10
int i
Definition: parser.h:13
int open(const char *file)
Definition: parser.cpp:60
Definition: parser.h:12
int read_fmt(const char *tag, const char *fmt, T *x)
Definition: parser.h:105
void ack(char *arg, char *val)
Definition: parser.cpp:50
int argc
Definition: parser.h:13
int read(const char *tag, matrix *)
Definition: iofile.cpp:213