Line data Source code
1 : /** \file generate_fits_status_info.cpp
2 : * \brief Generates fits_status_info.hpp from the configured CFITSIO status list.
3 : * \author Jared Males
4 : */
5 :
6 : #include <cerrno>
7 : #include <cstring>
8 : #include <fstream>
9 : #include <iostream>
10 : #include <vector>
11 : #include <string>
12 : #include <format>
13 : #include <algorithm>
14 :
15 0 : int main()
16 : {
17 :
18 0 : std::ifstream fin;
19 :
20 0 : std::vector<std::string> ecodes, evals, estrings;
21 :
22 0 : fin.open("fits_error_codes.txt");
23 0 : while(fin)
24 : {
25 0 : std::string line;
26 0 : std::getline( fin, line );
27 :
28 0 : size_t start = line.find_first_not_of( " \t" );
29 :
30 0 : if( start == std::string::npos )
31 : {
32 0 : continue;
33 : }
34 :
35 0 : size_t space1 = line.find( ' ', start );
36 :
37 0 : if( space1 == std::string::npos )
38 : {
39 0 : continue;
40 : }
41 :
42 0 : size_t space2 = space1;
43 0 : while(space2 < line.size() && std::isspace(line[space2]))
44 : {
45 0 : ++space2;
46 : }
47 :
48 0 : if( space2 == std::string::npos )
49 : {
50 0 : continue;
51 : }
52 :
53 0 : size_t space3 = line.find( ' ', space2 );
54 :
55 0 : if( space3 == std::string::npos )
56 : {
57 0 : continue;
58 : }
59 :
60 :
61 :
62 0 : size_t quotest = line.find( '"', space3 );
63 :
64 0 : if( quotest == std::string::npos )
65 : {
66 0 : continue;
67 : }
68 :
69 0 : ++quotest;
70 :
71 0 : size_t quoteed = line.find( '"', quotest );
72 :
73 0 : if( quoteed == std::string::npos )
74 : {
75 0 : continue;
76 : }
77 :
78 0 : ecodes.push_back( line.substr( start, space1 - start ) );
79 0 : evals.push_back( line.substr( space2, space3 - space2 ));
80 0 : estrings.push_back( line.substr( quotest, quoteed - quotest ) );
81 0 : }
82 0 : fin.close();
83 :
84 0 : for(size_t n = 0; n < ecodes.size(); ++n)
85 : {
86 0 : evals[n] = ecodes[n];//we'll use the text codes
87 0 : std::transform(ecodes[n].begin(), ecodes[n].end(), ecodes[n].begin(),[](unsigned char c){ return std::tolower(c); });
88 :
89 0 : if(estrings[n].size() == 0)
90 : {
91 0 : estrings[n] = "FITS: " + ecodes[n];
92 0 : std::transform(estrings[n].begin(), estrings[n].end(), estrings[n].begin(),[](char c){ if(c=='_') return ' '; else return c; });
93 : }
94 : else
95 : {
96 0 : estrings[n] = "FITS: " + estrings[n];
97 : }
98 :
99 0 : ecodes[n] = "fits_" + ecodes[n];
100 :
101 : }
102 :
103 0 : std::ofstream fout;
104 0 : fout.open("fits_status_info.hpp");
105 :
106 0 : fout << "//This is a generated file\n";
107 0 : fout << "//Only used during code generation\n";
108 0 : fout << "//It should not be installed\n";
109 0 : fout << '\n';
110 0 : fout << "#include <vector>\n";
111 0 : fout << "#include <string>\n\n";
112 0 : fout << "void fits_status_info( std::vector<std::string> & ecodes, std::vector<std::string> & evals, std::vector<std::string> & estrings)\n";
113 0 : fout << "{\n";
114 0 : for(size_t n = 0; n < ecodes.size(); ++n)
115 : {
116 0 : fout << " ecodes.push_back(\"" + ecodes[n] + "\");" << '\n';
117 0 : fout << " evals.push_back(\"" + evals[n] + "\");" << '\n';
118 0 : fout << " estrings.push_back(\"" + estrings[n] + "\");" << '\n';
119 0 : fout << '\n';
120 : }
121 :
122 0 : fout << "}\n";
123 :
124 0 : fout.close();
125 :
126 0 : return 0;
127 0 : }
|