Loading...
Searching...
No Matches
GenericIO.tpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "bitpit_operators.hpp"
26
27namespace bitpit {
28
29namespace genericIO {
30
37template<class data_T>
38void flushASCII(std::fstream &str, const data_T &data)
39{
40 std::ios::fmtflags streamFlags(str.flags());
41
42 str << std::setprecision(8) << std::scientific;
43 str << data << " ";
44
45 str.flags(streamFlags);
46}
47
54template<class data_T>
55void flushASCII(std::fstream &str, const std::vector<data_T> &data)
56{
57 flushASCII(str, data.size(), data);
58}
59
67template<class data_T>
68void flushASCII(std::fstream &str, int elements_per_line, const std::vector<data_T> &data)
69{
70 int i(0), j(0), k(0);
71 int nr;
72
73 int lines, this_line;
74
75 bool next(true);
76
77 nr = data.size();
78 lines = (nr - 1) /elements_per_line + 1;
79
80 std::ios::fmtflags streamFlags(str.flags());
81
82 str << std::setprecision(8) << std::scientific;
83
84 while (next) {
85 this_line = std::min(elements_per_line, nr - k);
86
87 for (j=0; j<this_line; j++) {
88 flushASCII(str, data[k]);
89 ++k;
90 }
91
92 i++;
93 if (i<lines) {
94 str << std::endl;
95 } else {
96 next = false;
97 }
98 }
99
100 str.flags(streamFlags);
101}
102
110template< class data_T, size_t d >
111void flushASCII(std::fstream &str, const std::array<data_T,d> &data)
112{
113 flushASCII(str, d, data);
114}
115
124template< class data_T, size_t d >
125void flushASCII(std::fstream &str, int elements_per_line, const std::array<data_T,d> &data)
126{
127 int i(0), j(0), k(0);
128 int nr;
129
130 int lines, this_line;
131
132 bool next(true);
133
134 nr = d;
135 lines = nr /elements_per_line;
136
137 std::ios::fmtflags streamFlags(str.flags());
138
139 str << std::setprecision(8) << std::scientific;
140
141 while (next) {
142 this_line = std::min(elements_per_line, nr - k);
143 for (j=0; j<this_line; j++) {
144 flushASCII(str, data[k]);
145 ++k;
146 }
147
148 i++;
149 if (i<lines) {
150 str << std::endl;
151 } else {
152 next = false;
153 }
154 }
155
156 str.flags(streamFlags);
157}
158
167template<class data_T>
168void flushASCII(std::fstream &str, int elements_per_line, const data_T *data, int nr)
169{
170 int i(0), j(0), k(0);
171
172 int lines, this_line;
173
174 bool next(true);
175
176 lines = nr /elements_per_line;
177
178 std::ios::fmtflags streamFlags(str.flags());
179
180 str << std::setprecision(8) << std::scientific;
181
182 while (next) {
183 this_line = std::min(elements_per_line, nr - k);
184 for (j = 0; j < this_line; j++) {
185 flushASCII(str, data[k]);
186 ++k;
187 }
188
189 i++;
190 if (i<lines) {
191 str << std::endl;
192 } else {
193 next = false;
194 }
195 }
196
197 str.flags(streamFlags);
198}
199
209template<class data_T>
210void flushASCII(std::fstream &str, int elements_per_line, const PiercedVector<data_T> &data, bool writeIndex)
211{
213
214 int i(0), j(0), k(0);
215 int nr;
216
217 int lines, this_line;
218
219 bool next(true);
220
221 nr = data.size();
222 lines = (nr-1) /elements_per_line + 1;
223
224 std::ios::fmtflags streamFlags(str.flags());
225
226 str << std::setprecision(8) << std::scientific;
227
228 while (next) {
229
230 this_line = std::min(elements_per_line, nr - k);
231
232 for (j=0; j<this_line; j++) {
233 if (writeIndex) {
234 flushASCII(str, dataItr.getId());
235 }
236 flushASCII(str, *dataItr);
237 ++k;
238 ++dataItr;
239 }
240
241 i++;
242 if (i<lines) {
243 str << std::endl;
244 } else {
245 next = false;
246 }
247 }
248
249 str.flags(streamFlags);
250}
251
259template<class data_T>
260void flushBINARY(std::fstream &str, const data_T &data)
261{
262 int nbytes;
263 nbytes = sizeof(data_T);
264
265 str.write(reinterpret_cast<const char*>(&data), nbytes);
266}
267
275template<class data_T>
276void flushBINARY(std::fstream &str, const std::vector<data_T> &data)
277{
278 int nbytes, nr;
279 nr = data.size();
280 nbytes = sizeof(data_T) * nr;
281
282 str.write(reinterpret_cast<const char*>(&data[0]), nbytes);
283}
284
291template<class data_T>
292void flushBINARY(std::fstream &str, const std::vector<std::vector<data_T>> &data)
293{
294 for (const auto &item : data) {
295 flushBINARY(str, item);
296 }
297}
298
307template< class data_T, size_t d >
308void flushBINARY(std::fstream &str, const std::vector<std::array<data_T,d> > &data)
309{
310 int nbytes, nr;
311 nr = data.size();
312 nbytes = sizeof(data_T) * nr * d;
313
314 str.write(reinterpret_cast<const char*>(&data[0]), nbytes);
315}
316
324template< class data_T, size_t d >
325void flushBINARY(std::fstream &str, const std::array<data_T,d> &data)
326{
327 int nbytes;
328 nbytes = sizeof(data_T)*d;
329
330 str.write(reinterpret_cast<const char*>(&data[0]), nbytes);
331}
332
340template<class data_T>
341void flushBINARY(std::fstream &str, const data_T *data, int nr)
342{
343 int nbytes;
344 nbytes = sizeof(data_T) *nr;
345
346 str.write(reinterpret_cast<const char*>(data), nbytes);
347}
348
357template<class data_T>
358void flushBINARY(std::fstream &str, const PiercedVector<data_T> &data, bool writeIndex)
359{
360 typename PiercedVector<data_T>::const_iterator dataItr, dataEnd = data.end();
361
362 if (writeIndex) {
363 for (dataItr = data.begin(); dataItr != dataEnd; ++dataItr) {
364 flushBINARY(str, dataItr.getId());
365 flushBINARY(str, *dataItr);
366 }
367 } else {
368 for (dataItr = data.begin(); dataItr != dataEnd; ++dataItr) {
369 flushBINARY(str, *dataItr);
370 }
371 }
372}
373
382template<class data_T>
383void lineStream(std::fstream &str, data_T &data)
384{
385 std::vector<data_T> temp;
386 data_T x_;
387 std::string line;
388 int expected, read(0);
389
390 expected = 1;
391
392 getline(str, line);
394
395 std::stringstream ss(line);
396
397 while (ss.good()) {
398 ss >> x_;
399 temp.push_back(x_);
400 read++;
401 }
402
403 if (read != expected) {
404 log::cout() << " Not expected nr of element in line" << std::endl;
405 log::cout() << " Expected number: "<< expected << std::endl;
406 log::cout() << " Actual number: "<< read << std::endl;
407 } else {
408 data=temp[0];
409 }
410}
411
422template<class data_T>
423void lineStream(std::fstream &str, std::vector<data_T> &data)
424{
425 std::string line;
426 int expected(data.size());
427
428 getline(str, line);
430
431 std::stringstream ss(line);
432
433 if (expected == 0) {
434 data_T x_;
435 while (ss.good()) {
436 ss >> x_;
437 data.push_back(x_);
438 }
439 } else {
440 int read(0);
441 while (ss.good() && read<expected) {
442 ss >> data[read];
443 read++;
444 }
445
446 if (expected != read) {
447 log::cout() << " Not expected nr of element in line" << std::endl;
448 log::cout() << " Expected number: "<< expected << std::endl;
449 log::cout() << " Actual number: "<< read << std::endl;
450 }
451 }
452}
453
464template< class data_T, size_t d >
465void lineStream(std::fstream &str, std::array<data_T,d> &data)
466{
467 std::vector<data_T> temp;
468 data_T x_;
469 std::string line;
470 int expected, read(0), i;
471
472 expected = d;
473
474 getline(str, line);
476
477 std::stringstream ss(line);
478
479 while (ss.good()) {
480 ss >> x_;
481 temp.push_back(x_);
482 read++;
483 }
484
485 if (expected == read) {
486 for (i=0; i<read; i++) data[i] = temp[i];
487 } else {
488 log::cout() << " Not expected nr of element in line" << std::endl;
489 log::cout() << " Expected number: "<< expected << std::endl;
490 log::cout() << " Actual number: "<< read << std::endl;
491 }
492}
493
504template<class data_T>
505void lineStream(std::fstream &str, data_T *data, int nr)
506{
507 std::vector<data_T> temp;
508 data_T x_;
509 std::string line;
510 int expected, read(0), i;
511
512 expected = nr;
513
514 getline(str, line);
516
517 std::stringstream ss(line);
518
519 while (ss.good()) {
520 ss >> x_;
521 temp.push_back(x_);
522 read++;
523 }
524
525 if (expected == read) {
526 for (i=0; i<read; i++) data[i] = temp[i];
527 } else {
528 log::cout() << " Not expected nr of element in line" << std::endl;
529 log::cout() << " Expected number: "<< expected << std::endl;
530 log::cout() << " Actual number: "<< read << std::endl;
531 }
532}
533
541template<class data_T>
542void absorbASCII(std::fstream &str, data_T &data)
543{
544 str >> data;
545}
546
556template<class data_T>
557void absorbASCII(std::fstream &str, std::vector<data_T> &data)
558{
559 std::vector<data_T> temp;
560
561 typename std::vector<data_T>::iterator itrData, begData, endData;
562 typename std::vector<data_T>::iterator itrTemp, begTemp, endTemp;
563
564 begData = data.begin();
565 endData = data.end();
566
567 itrData = begData;
568
569 while (str.good() && itrData!=endData) {
570 temp.clear();
571 lineStream(str, temp);
572
573 begTemp = temp.begin();
574 endTemp = temp.end();
575
576 for (itrTemp=begTemp; (itrTemp!=endTemp && itrData!=endData);) {
577 *itrData = *itrTemp;
578
579 ++itrTemp;
580 ++itrData;
581 }
582 }
583
584 if (itrData != endData) {
585 log::cout() << "Not enough elements found to fill vector" << std::endl;
586 }
587}
588
595template<class data_T>
596void absorbASCII(std::fstream &str, std::vector<std::vector<data_T>> &data)
597{
598
599 size_t dim1= data.size();
600 if(dim1 == 0){
601 return;
602 }
603
604 size_t row(0);
605
606 typename std::vector<data_T>::iterator itrRow = data[row].begin();
607 typename std::vector<data_T>::iterator endRow = data[row].end();
608
609 std::vector<data_T> temp;
610 typename std::vector<data_T>::iterator itrTemp;
611
612 while(itrRow==endRow){
613 ++row;
614
615 if(row==dim1){
616 log::cout() << "all internal vectors have 0 size in absorbASCII " << std::endl;
617 return;
618 }
619
620 itrRow = data[row].begin();
621 endRow = data[row].end();
622 }
623
624 while(str.good()) {
625
626 temp.clear();
627 lineStream(str, temp);
628
629 for (itrTemp=temp.begin(); itrTemp!=temp.end(); ++itrTemp) {
630 *itrRow = *itrTemp;
631
632 ++itrRow;
633 while(itrRow==endRow){
634 ++row;
635
636 if(row==dim1){
637 return;
638 }
639
640 itrRow = data[row].begin();
641 endRow = data[row].end();
642 }
643 }
644 }
645
646 log::cout() << "Not enough elements found to fill vector" << std::endl;
647}
648
656template< class data_T, size_t d >
657void absorbASCII(std::fstream &str, std::vector<std::array<data_T,d> > &data)
658{
659 if(d == 0){
660 return;
661 }
662
663 size_t nArrays = data.size();
664 if(nArrays == 0){
665 return;
666 }
667
668 absorbASCII(str, &data[0][0], nArrays * d);
669}
670
681template< class data_T, size_t d >
682void absorbASCII(std::fstream &str, std::array<data_T,d> &data)
683{
684 std::vector<data_T> temp;
685
686 typename std::array<data_T,d>::iterator itrData, begData, endData;
687 typename std::vector<data_T>::iterator itrTemp, begTemp, endTemp;
688
689 begData = data.begin();
690 endData = data.end();
691
692 itrData = begData;
693
694 while (str.good() && itrData!=endData) {
695 temp.clear();
696 lineStream(str, temp);
697
698 begTemp = temp.begin();
699 endTemp = temp.end();
700
701 for (itrTemp = begTemp; (itrTemp!=endTemp && itrData!=endData);) {
702 *itrData = *itrTemp;
703
704 ++itrTemp;
705 ++itrData;
706 }
707 }
708
709 if (itrData != endData) {
710 log::cout() << "Not enough elements found to fill array" << std::endl;
711 }
712}
713
724template<class data_T>
725void absorbASCII(std::fstream &str, data_T *data, int nr)
726{
727 std::vector<data_T> temp;
728
729 data_T *itrData, *begData, *endData;
730 typename std::vector<data_T>::iterator itrTemp, begTemp, endTemp;
731
732 begData = &data[0];
733 endData = &data[nr];
734
735 itrData = begData;
736
737 while (str.good() && itrData!=endData) {
738 temp.clear();
739 lineStream(str, temp);
740
741 begTemp = temp.begin();
742 endTemp = temp.end();
743
744 for (itrTemp=begTemp; (itrTemp!=endTemp && itrData!=endData);) {
745 *itrData = *itrTemp;
746
747 ++itrTemp;
748 ++itrData;
749 }
750 }
751
752 if (itrData != endData) {
753 log::cout() << "Not enough elements found to fill array" << std::endl;
754 }
755}
756
763template<class data_T>
764void absorbASCII(std::fstream &str, bitpit::PiercedVector<data_T> &data)
765{
766 bool read(true);
767 std::string line;
768
769 typename bitpit::PiercedVector<data_T>::iterator dataItr = data.begin(), dataEnd = data.end();
770
771 while (str.good() && read) {
772 getline(str, line);
774
775 std::stringstream ss(line);
776
777 while (ss.good() && read) {
778 ss >> *dataItr;
779 ++dataItr;
780
781 read = dataItr != dataEnd;
782 }
783 }
784}
785
794template<class data_T>
795void absorbASCII(std::fstream &str, bitpit::PiercedVector<data_T> &data, long N)
796{
797 bool read(true);
798 long n(0), index;
799 data_T value;
800 std::string line;
801
802 while (str.good() && read) {
803 getline(str, line);
805
806 std::stringstream ss(line);
807
808 while (ss.good() && read) {
809 ss >> index;
810 ss >> value;
811 data.insert(index,value);
812 ++n;
813 read = n<N;
814 }
815 }
816}
817
824template<class data_T>
825void absorbBINARY(std::fstream &str, data_T &data)
826{
827 int nbytes;
828 nbytes = sizeof(data_T);
829
830 str.read(reinterpret_cast<char*>(&data), nbytes);
831}
832
839template<class data_T>
840void absorbBINARY(std::fstream &str, std::vector<data_T> &data)
841{
842 int nbytes, nr;
843 nr = data.size();
844 nbytes = sizeof(data_T) *nr;
845
846 str.read(reinterpret_cast<char*>(&data[0]), nbytes);
847}
848
855template<class data_T>
856void absorbBINARY(std::fstream &str, std::vector<std::vector<data_T>> &data)
857{
858 for (auto &item: data) {
859 absorbBINARY(str, item);
860 }
861}
862
870template< class data_T, size_t d >
871void absorbBINARY(std::fstream &str, std::vector<std::array<data_T,d> > &data)
872{
873 int nbytes, nr;
874 nr = data.size();
875 nbytes = sizeof(data_T) *nr *d;
876
877 str.read(reinterpret_cast<char*>(&data[0]), nbytes);
878}
879
887template< class data_T, size_t d >
888void absorbBINARY(std::fstream &str, std::array<data_T,d> &data)
889{
890 int nbytes;
891 nbytes = sizeof(data_T) *d;
892
893 str.read(reinterpret_cast<char*>(&data[0]), nbytes);
894}
895
903template<class data_T>
904void absorbBINARY(std::fstream &str, data_T *data, int nr)
905{
906 int nbytes;
907 nbytes = sizeof(data_T) *nr;
908
909 str.read(reinterpret_cast<char*>(data), nbytes);
910}
911
920template<class data_T>
921void absorbBINARY(std::fstream &str, PiercedVector<data_T> &data)
922{
923 typename bitpit::PiercedVector<data_T>::iterator dataItr, dataEnd = data.end();
924
925 for (dataItr = data.begin(); dataItr != dataEnd; ++dataItr) {
926 absorbBINARY(str, *dataItr);
927 }
928}
929
939template<class data_T>
940void absorbBINARY(std::fstream &str, PiercedVector<data_T> &data, long nElements)
941{
942 long n, index;
943 data_T value;
944
945 data.reserve(nElements);
946
947 for (n=0; n<nElements; ++n) {
948 absorbBINARY(str,index);
949 absorbBINARY(str,value);
950
951 data.insert(index,value);
952 }
953}
954
955}
956
957}
id_t getId(const id_t &fallback=-1) const noexcept
const_iterator begin() const noexcept
const_iterator end() const noexcept
std::size_t size() const
Iterator for the class PiercedStorage.
Metafunction for generating a pierced vector.
iterator insert(id_t id, const value_t &value)
void reserve(std::size_t n)
std::string & trim(std::string &s)
void flushASCII(std::fstream &str, const uint8_t &data)
Definition GenericIO.cpp:37
void absorbBINARY(std::fstream &str, data_T &data)
void absorbASCII(std::fstream &str, data_T &data)
void lineStream(std::fstream &str, data_T &data)
void flushBINARY(std::fstream &str, const data_T &data)
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
--- layout: doxygen_footer ---