| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rwmatrix.cpp
Go to the documentation of this file.
1
9 #include "rwmatrix.h"
10 #include "linked_list.h"
11 #include "utilities.h"
12 #include <fstream>
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
31 Bmatrix read_matrix(Bstring& filename)
32 {
33 Bmatrix matrix;
34
35 ifstream fmat(filename.c_str());
36 if ( fmat.fail() ) return matrix;
37
38 char aline[1024];
39 unsigned long i, j, k, n;
40 float* temp = NULL;
41
42 while ( fmat.getline(aline, 1024) && strncmp(aline, "matrix_", 7) ) ;
43
44 fmat.getline(aline, 1024);
45
46 Bstring sline = aline, alabel;
47
48 if ( verbose & VERB_DEBUG )
49 cout << "DEBUG read_matrix: aline = \"" << aline << "\"" << endl;
50
51 int row_label_flag(0);
52 Bstring* labels = sline.split();
53 int cols = count_list((char *)labels);
54 if ( *labels == "#" ) {
55 cols--;
56 row_label_flag = 1;
57 labels = labels->next;
58 }
59
60 if ( cols < 1 ) {
61 cerr << "Error: No matrix columns found in " << filename << endl;
62 fmat.close();
63 bexit(-1);
64 }
65
66 i = 0;
67 while ( fmat.getline(aline, 1024) && strlen(aline) > cols ) {
68 if ( verbose & VERB_DEBUG )
69 cout << "DEBUG read_matrix: aline = \"" << aline << "\"" << endl;
70 sline = aline;
71 sline = sline.substr(0, sline.length() - 1);
72 temp = sline.split_into_floats(0, n);
73 delete[] temp;
74 if ( n < cols ) break;
75 else i++;
76 }
77
78 int rows = i;
79
80 matrix = Bmatrix(cols, rows);
81 matrix.column_labels(labels);
82 if ( !row_label_flag && matrix.rows() == matrix.columns() ) matrix.row_labels(labels);
83
84 string_kill(labels);
85
86 if ( verbose & VERB_PROCESS )
87 cout << "Reading file " << filename << " with a matrix of size " <<
88 matrix.columns() << "x" << matrix.rows() << endl << endl;
89
90 fmat.seekg(0);
91
92 while ( fmat.getline(aline, 1024) && strncmp(aline, "matrix_", 7) ) ;
93
94 fmat.getline(aline, 1024);
95
96 i = 0;
97 while ( fmat.getline(aline, 1024) && i < rows ) {
98 sline = aline;
99 temp = sline.split_into_floats(0, n);
100 k = 0;
101 if ( row_label_flag ) {
102 alabel = sline.pre(' ');
103 matrix.row_add_label(alabel);
104 n--;
105 k = 1;
106 }
107 if ( n < cols )
108 cerr << "Row " << i+1 << " contains only " << n << " values in stead of " << cols << " values!" << endl;
109 for ( j=0; j<cols; j++, k++ ) matrix[i*cols+j] = temp[k];
110 delete[] temp;
111 i++;
112 }
113 if ( i < rows )
114 cerr << "The matrix contains only " << i << " rows in stead of " << rows << " rows!" << endl;
115
116 fmat.close();
117
118 return matrix;
119 }
120
132 int write_matrix(Bstring& filename, Bmatrix matrix)
133 {
134 ofstream fmat(filename.c_str());
135 if ( fmat.fail() ) return -1;
136
137 if ( verbose & VERB_PROCESS )
138 cout << "Writing file " << filename << " with a matrix of size " <<
139 matrix.columns() << "x" << matrix.rows() << endl << endl;
140
141 int i, j, k;
142 Bstring* label = NULL;
143 fmat << "# Matrix written by Bsoft\n\ndata_\n\nmatrix_" << endl;
144 if ( matrix.column_labels() ) {
145 if ( matrix.row_labels() ) fmat << "#";
146 for ( label=matrix.column_labels(); label; label=label->next )
147 fmat << " " << *label;
148 fmat << endl;
149 }
150 for ( i=k=0, label=matrix.row_labels(); i<matrix.rows(); i++, label=(label)?label->next:NULL ) {
151 if ( label ) fmat << *label;
152 for ( j=0; j<matrix.columns(); j++, k++ )
153 fmat << " " << matrix[k];
154 fmat << endl;
155 }
156 fmat << endl;
157
158 fmat.close();
159
160 return 0;
161 }
162
Bmatrix
Definition: Bmatrix.h:28
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:359
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:105
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:205
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:100
read_matrix
Bmatrix read_matrix(Bstring &filename)
Reads a matrix file.
Definition: rwmatrix.cpp:31
Bmatrix::column_labels
Bstring * column_labels()
Definition: Bmatrix.h:80
linked_list.h
Header file for generalized linked list functions.
rwmatrix.h
Header file for reading (and writing) matrices.
Bstring::next
Bstring * next
Definition: Bstring.h:27
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:407
Bstring::split_into_floats
float * split_into_floats(const Bstring &delim, unsigned long &n)
Definition: Bstring.cpp:693
Bstring::c_str
const char * c_str()
Definition: Bstring.h:61
Bmatrix::rows
long rows()
Definition: Bmatrix.h:78
Bstring
Definition: Bstring.h:25
Bmatrix::row_add_label
void row_add_label(Bstring &s)
Definition: Bmatrix.h:93
Bmatrix::row_labels
Bstring * row_labels()
Definition: Bmatrix.h:81
verbose
int verbose
Bstring::length
long length() const
Definition: Bstring.cpp:304
Bmatrix::columns
long columns()
Definition: Bmatrix.h:77
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:260
write_matrix
int write_matrix(Bstring &filename, Bmatrix matrix)
Writes a matrix file.
Definition: rwmatrix.cpp:132
Bstring::split
Bstring * split()
Definition: Bstring.cpp:623
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:782