128{
129
130 FILE* fin;
131 if( ( fin = fopen(filename, "r") ) )
132 {
133 char l=0;
134 while ( l != '{' ) {
135 l=fgetc(fin);
136 }
137 ungetc(' ', fin);
138 }
139 else
140 {
141 fprintf(stderr, "mixmax -> read_state: error reading file %s\n", filename);
142 throw std::runtime_error("Error in reading state file");
143 }
144
146
147 if (!fscanf(fin, "%llu", (unsigned long long*) &V[0]) )
148 {
149 fprintf(stderr, "mixmax -> read_state: error reading file %s\n", filename);
150 throw std::runtime_error("Error in reading state file");
151 }
152
153 for (int i = 1; i < rng_get_N(); ++i)
154 {
155 if (!fscanf(fin, ", %llu", (unsigned long long*) &vecVal) )
156 {
157 fprintf(stderr, "mixmax -> read_state: error reading vector component i=%d from file %s\n", i, filename);
158 throw std::runtime_error("Error in reading state file");
159 }
160 if( vecVal <= MixMaxRng::M61 )
161 {
162 V[i] = vecVal;
163 }
164 else
165 {
166 fprintf(stderr, "mixmax -> read_state: Invalid state vector value= %llu"
167 " ( must be less than %llu ) "
168 " obtained from reading file %s\n"
169 , (unsigned long long)vecVal, (unsigned long long)MixMaxRng::M61, filename);
170 }
171 }
172
173 int incounter;
174 if (!fscanf( fin, "}; counter=%i; ", &incounter))
175 {
176 fprintf(stderr, "mixmax -> read_state: error reading counter from file %s\n", filename);
177 throw std::runtime_error("Error in reading state file");
178 }
179 if( incounter <= rng_get_N() )
180 {
181 counter = incounter;
182 }
183 else
184 {
185 fprintf(stderr, "mixmax -> read_state: Invalid counter = %d"
186 " Must be 0 <= counter < %u\n" , counter, rng_get_N());
187 print_state();
188 throw std::runtime_error("Error in reading state counter");
189 }
190 precalc();
192 if (!fscanf( fin, "sumtot=%llu\n", (unsigned long long*) &insumtot))
193 {
194 fprintf(stderr, "mixmax -> read_state: error reading checksum from file %s\n", filename);
195 throw std::runtime_error("Error in reading state file");
196 }
197
198 if (sumtot != insumtot)
199 {
200 fprintf(stderr, "mixmax -> checksum error while reading state from file %s - corrupted?\n", filename);
201 throw std::runtime_error("Error in reading state checksum");
202 }
203 fclose(fin);
204}