1 | #include <itpp/itbase.h> |
---|
2 | #include "loggers.h" |
---|
3 | |
---|
4 | #include <sys/types.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | #include <fcntl.h> |
---|
7 | #include <errno.h> |
---|
8 | |
---|
9 | extern int errno; |
---|
10 | |
---|
11 | void get_fname ( char* filename, string &dirname, string &f ) { |
---|
12 | #ifdef WIN32 |
---|
13 | sprintf ( filename,"%s\\%s",dirname.c_str(),f.c_str() ); |
---|
14 | #else |
---|
15 | sprintf ( filename,"%s/%s",dirname.c_str() ,f.c_str() ); |
---|
16 | #endif |
---|
17 | } |
---|
18 | |
---|
19 | void dirfilelog::init() { |
---|
20 | int i,j,k; |
---|
21 | int nsc=0; |
---|
22 | for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i ).count();} |
---|
23 | ; //all entries!! |
---|
24 | |
---|
25 | char filename[200]; |
---|
26 | char num[3]; |
---|
27 | |
---|
28 | // Initialize directory |
---|
29 | if ( mkdir ( dirname.c_str(),00755 ) <0 ) { |
---|
30 | if ( errno==EEXIST ) it_warning ( "rewriting directory" ); |
---|
31 | else it_error ( "dirfilelog:: cannot create directory" ); |
---|
32 | } |
---|
33 | |
---|
34 | // directory OK, or it can be overwritten |
---|
35 | |
---|
36 | //Create filenames |
---|
37 | scalarnames.set_length ( nsc ); |
---|
38 | // For all entries |
---|
39 | int ii=0; |
---|
40 | for ( i=0;i<entries.length();i++ ) { //for entries |
---|
41 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
42 | int rvsize = entries ( i ).size ( j ); |
---|
43 | if ( rvsize ==1 ) { |
---|
44 | scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ); |
---|
45 | ii++; |
---|
46 | } else { |
---|
47 | for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV |
---|
48 | sprintf ( num,"%d",k ); |
---|
49 | scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ) + "_" + num; |
---|
50 | ii++; |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | //Create format |
---|
56 | string frm="format"; |
---|
57 | get_fname ( filename, dirname, frm ); |
---|
58 | |
---|
59 | FILE* fpf; |
---|
60 | fpf = fopen ( filename, "w" ); |
---|
61 | for ( i=0;i<ii;i++ ) { |
---|
62 | fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() ); |
---|
63 | } |
---|
64 | fclose ( fpf ); |
---|
65 | |
---|
66 | memlog::init(); |
---|
67 | |
---|
68 | write_buffers ( 0 ); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void dirfilelog::write_buffers ( int Len ) { |
---|
73 | int fp; |
---|
74 | int nen=entries.length(); //all entries!! |
---|
75 | double *Dt; |
---|
76 | int DtRows; |
---|
77 | int i,j,k; |
---|
78 | int ii; //index in scalarnames; |
---|
79 | int jj; //index in vectors() |
---|
80 | char filename[200]; |
---|
81 | |
---|
82 | it_assert_debug ( Len<=maxlen,"diffilelog" ); |
---|
83 | |
---|
84 | ii = 0; |
---|
85 | for ( i=0;i<nen;i++ ) { //for entries |
---|
86 | Dt = vectors ( i )._data(); |
---|
87 | DtRows = vectors(i).rows(); |
---|
88 | |
---|
89 | jj=0; |
---|
90 | for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries |
---|
91 | int rvsize = entries ( i ).size ( j ); |
---|
92 | for ( k=0;k<rvsize;k++ ) { { //for all scalars in given RV |
---|
93 | get_fname ( filename,dirname,scalarnames ( ii ) ); |
---|
94 | if ( Len==0 ) //initialization |
---|
95 | fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 ); |
---|
96 | else |
---|
97 | fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); |
---|
98 | |
---|
99 | write ( fp,&Dt[jj*(DtRows)],(Len)*sizeof ( double ) ); |
---|
100 | close ( fp ); |
---|
101 | |
---|
102 | //next row in Dt |
---|
103 | jj++; |
---|
104 | //next scalarname |
---|
105 | ii++; |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | void dirfilelog::step ( bool final ) { |
---|
113 | |
---|
114 | if ( final ) { |
---|
115 | if ( ind>0 ) |
---|
116 | write_buffers ( ind ); //assuming here that i+1 was not filled |
---|
117 | } else |
---|
118 | if ( ind== ( maxlen -1 ) ) { |
---|
119 | write_buffers ( ind+1 ); |
---|
120 | ind = 0; |
---|
121 | } else |
---|
122 | ind++; |
---|
123 | |
---|
124 | } |
---|