root/library/bdm/base/loggers.cpp @ 477

Revision 477, 3.7 kB (checked in by mido, 15 years ago)

panove, vite, jak jsem peclivej na upravu kodu.. snad se vam bude libit:) konfigurace je v souboru /system/astylerc

  • Property svn:eol-style set to native
Line 
1
2#include "loggers.h"
3#include "../osutils.h"
4
5#include <fcntl.h>
6#ifdef WIN32
7#include <io.h>
8#endif
9
10namespace bdm {
11void memlog::itsave ( const char* fname ) {
12        it_file itf ( fname );
13        int i;
14        string vec_name = "";
15        int istart, iend;
16        for ( i = 0; i < entries.length(); i++ ) {
17                istart = 0;
18                for ( int j = 0; j < entries ( i ).length(); j++ ) { // same for as in add!!!
19                        vec_name = names ( i ) + entries ( i ).name ( j );
20                        iend = istart + entries ( i ).size ( j ) - 1;
21                        itf << Name ( vec_name ) << vectors ( i ).get_cols ( istart, iend );
22                        // move start after current end...
23                        istart = iend + 1;
24                }
25
26        }
27}
28
29void memlog::from_setting ( const Setting &set ) {
30        // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK?
31        string itfilename = ( const char* ) set["filename"];
32        maxlen = set["maxlen"];
33}
34
35void dirfilelog::init() {
36        int i, j, k;
37        int nsc = 0;
38        for ( i = 0; i < entries.length(); i++ ) {
39                nsc += entries ( i )._dsize();
40        }
41        ; //all entries!!
42
43        char filename[200];
44        char num[3];
45
46// Initialize directory
47        makedir ( dirname ); //
48
49// directory OK, or it can be overwritten
50
51//Create filenames
52        scalarnames.set_length ( nsc );
53        // For all entries
54        int ii = 0;
55        for ( i = 0; i < entries.length(); i++ ) { //for entries
56                for ( j = 0; j < entries ( i ).length(); j++ ) { //for RVs in entries
57                        int rvsize = entries ( i ).size ( j );
58                        // for non-empty names
59                        if ( names ( i ).length() > 0 ) {
60                                scalarnames ( ii ) = names ( i ) + "_";
61                        }
62                        // add name
63                        scalarnames ( ii ) +=  entries ( i ).name ( j );
64                        // add number when needed
65                        if ( rvsize > 1 ) {
66                                // copy the same name for th whole RV
67                                for ( k = 1; k < rvsize; k++ ) {
68                                        scalarnames ( ii + k ) = scalarnames ( ii );
69                                }
70                                // add numbers
71                                for ( k = 0; k < rvsize; k++ ) { //for all scalars in given RV
72                                        sprintf ( num, "%d", k );
73                                        scalarnames ( ii ) += ( std::string ) "_" + num;
74                                        ii++;
75                                }
76                        } else {
77                                ii++;
78                        }
79                }
80        }
81        //Create format
82        string frm = "format";
83        get_fname ( filename, dirname, frm );
84
85        FILE* fpf;
86        fpf = fopen ( filename, "w" );
87        for ( i = 0; i < ii; i++ ) {
88                fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() );
89        }
90        fclose ( fpf );
91
92        memlog::init();
93
94        //Delete old data
95        write_buffers ( 0 );
96}
97
98
99void dirfilelog::write_buffers ( int Len ) {
100        int fp;
101        int nen = entries.length(); //all entries!!
102        double *Dt;
103        int DtRows;
104        int i, j, k;
105        int ii; //index in scalarnames;
106        int jj; //index in vectors()
107        char filename[200];
108
109        it_assert_debug ( Len <= maxlen, "diffilelog" );
110
111        ii = 0;
112        for ( i = 0; i < nen; i++ ) { //for entries
113                Dt = vectors ( i )._data();
114                DtRows = vectors ( i ).rows();
115
116                jj = 0;
117                for ( j = 0; j < entries ( i ).length(); j++ ) { //for RVs in entries
118                        int rvsize = entries ( i ).size ( j );
119                        for ( k = 0; k < rvsize; k++ ) { { //for all scalars in given RV
120                                        get_fname ( filename, dirname, scalarnames ( ii ) );
121                                        if ( Len == 0 ) //initialization
122                                                fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 );
123                                        else
124                                                fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 );
125
126                                        write ( fp, &Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) );
127                                        close ( fp );
128
129                                        //next row in Dt
130                                        jj++;
131                                        //next scalarname
132                                        ii++;
133                                }
134                        }
135                }
136        }
137}
138
139void dirfilelog::step ( ) {
140
141        if ( ind == ( maxlen - 1 ) ) {
142                write_buffers ( ind + 1 );
143                ind = 0;
144        } else
145                ind++;
146
147}
148
149void dirfilelog::finalize ( ) {
150        if ( ind > 0 )
151                write_buffers ( ind ); //assuming here that i+1 was not filled
152}
153
154
155void dirfilelog::from_setting ( const Setting &set ) {
156        // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK?
157        dirname = ( const char* ) set["dirname"];
158        maxlen = set["maxlen"];
159        scalarnames.set_length ( 0 );
160}
161
162}
Note: See TracBrowser for help on using the browser.