Changeset 17260
- Timestamp:
- 10/18/13 16:54:42 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/fits.h
r17259 r17260 48 48 namespace std 49 49 { 50 #else51 using namespace std;52 50 #endif 53 51 … … 65 63 struct Entry 66 64 { 67 char type;68 st ring value;69 st ring comment;70 st ring fitsString;65 char type; 66 std::string value; 67 std::string comment; 68 std::string fitsString; 71 69 72 70 template<typename T> … … 75 73 T t; 76 74 77 istringstream str(value);75 std::istringstream str(value); 78 76 str >> t; 79 77 … … 88 86 bool is_compressed; 89 87 90 st ring name;88 std::string name; 91 89 size_t bytes_per_row; 92 90 size_t num_rows; … … 101 99 size_t bytes; // num*size 102 100 char type; 103 st ring unit;101 std::string unit; 104 102 Compression_t comp; 105 103 }; 106 104 107 typedef map<string, Entry> Keys;108 typedef map<string, Column> Columns;109 typedef vector<Column> SortedColumns;105 typedef std::map<std::string, Entry> Keys; 106 typedef std::map<std::string, Column> Columns; 107 typedef std::vector<Column> SortedColumns; 110 108 111 109 Columns cols; … … 115 113 int64_t datasum; 116 114 117 st ring Trim(conststring &str, char c=' ') const115 std::string Trim(const std::string &str, char c=' ') const 118 116 { 119 117 // Trim Both leading and trailing spaces … … 122 120 123 121 // if all spaces or empty return an empty string 124 if (st ring::npos==pstart ||string::npos==pend)125 return st ring();122 if (std::string::npos==pstart || std::string::npos==pend) 123 return std::string(); 126 124 127 125 return str.substr(pstart, pend-pstart+1); 128 126 } 129 127 130 bool Check(const st ring &key, char type, conststring &value="") const128 bool Check(const std::string &key, char type, const std::string &value="") const 131 129 { 132 130 const Keys::const_iterator it = keys.find(key); 133 131 if (it==keys.end()) 134 132 { 135 ostringstream str;133 std::ostringstream str; 136 134 str << "Key '" << key << "' not found."; 137 135 #ifdef __EXCEPTIONS 138 throw runtime_error(str.str());136 throw std::runtime_error(str.str()); 139 137 #else 140 138 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 145 143 if (it->second.type!=type) 146 144 { 147 ostringstream str;145 std::ostringstream str; 148 146 str << "Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << "."; 149 147 #ifdef __EXCEPTIONS 150 throw runtime_error(str.str());148 throw std::runtime_error(str.str()); 151 149 #else 152 150 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 157 155 if (!value.empty() && it->second.value!=value) 158 156 { 159 ostringstream str;157 std::ostringstream str; 160 158 str << "Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << "."; 161 159 #ifdef __EXCEPTIONS 162 throw runtime_error(str.str());160 throw std::runtime_error(str.str()); 163 161 #else 164 162 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 170 168 } 171 169 172 Keys ParseBlock(const vector<string> &vec) const173 { 174 map<string,Entry> rc;170 Keys ParseBlock(const std::vector<std::string> &vec) const 171 { 172 std::map<std::string,Entry> rc; 175 173 176 174 for (unsigned int i=0; i<vec.size(); i++) 177 175 { 178 const st ring key = Trim(vec[i].substr(0,8));176 const std::string key = Trim(vec[i].substr(0,8)); 179 177 // Keywords without a value, like COMMENT / HISTORY 180 178 if (vec[i].substr(8,2)!="= ") … … 183 181 char type = 0; 184 182 185 st ring com;186 st ring val = Trim(vec[i].substr(10));183 std::string com; 184 std::string val = Trim(vec[i].substr(10)); 187 185 188 186 if (val[0]=='\'') … … 193 191 { 194 192 const size_t pp = val.find_first_of('\'', p); 195 if (pp==st ring::npos)193 if (pp==std::string::npos) 196 194 break; 197 195 … … 204 202 // Set value, comment and type 205 203 // comments could be just spaces. take care of this. 206 if (ppp!=st ring::npos && val.size() != ppp+1)204 if (ppp!=std::string::npos && val.size() != ppp+1) 207 205 com = Trim(val.substr(ppp+1)); 208 206 … … 219 217 val = Trim(val.substr(0, p)); 220 218 221 if (val.empty() || val.find_first_of('T')!=st ring::npos || val.find_first_of('F')!=string::npos)219 if (val.empty() || val.find_first_of('T')!=std::string::npos || val.find_first_of('F')!=std::string::npos) 222 220 type = 'B'; 223 221 else 224 type = val.find_last_of('.')==st ring::npos ? 'I' : 'F';222 type = val.find_last_of('.')==std::string::npos ? 'I' : 'F'; 225 223 } 226 224 … … 234 232 235 233 Table() : offset(0), is_compressed(false) { } 236 Table(const vector<string> &vec, off_t off) : offset(off),234 Table(const std::vector<std::string> &vec, off_t off) : offset(off), 237 235 keys(ParseBlock(vec)) 238 236 { … … 270 268 size_t bytes = 0; 271 269 272 const st ring tFormName = is_compressed ? "ZFORM" : "TFORM";270 const std::string tFormName = is_compressed ? "ZFORM" : "TFORM"; 273 271 for (long long unsigned int i=1; i<=num_cols; i++) 274 272 { 275 const st ring num(to_string(i));273 const std::string num(std::to_string(i)); 276 274 277 275 if (!Check("TTYPE"+num, 'T') || … … 279 277 return; 280 278 281 const st ring id = Get<string>("TTYPE"+num);282 const st ring fmt = Get<string>(tFormName+num);283 const st ring unit = Get<string>("TUNIT"+num, "");284 const st ring comp = Get<string>("ZCTYP"+num, "");279 const std::string id = Get<std::string>("TTYPE"+num); 280 const std::string fmt = Get<std::string>(tFormName+num); 281 const std::string unit = Get<std::string>("TUNIT"+num, ""); 282 const std::string comp = Get<std::string>("ZCTYP"+num, ""); 285 283 286 284 Compression_t compress = kCompUnknown; … … 288 286 compress = kCompFACT; 289 287 290 istringstream sin(fmt);288 std::istringstream sin(fmt); 291 289 int n = 0; 292 290 sin >> n; … … 316 314 default: 317 315 { 318 ostringstream str;316 std::ostringstream str; 319 317 str << "FITS format TFORM='" << fmt << "' not yet supported."; 320 318 #ifdef __EXCEPTIONS 321 throw runtime_error(str.str());319 throw std::runtime_error(str.str()); 322 320 #else 323 321 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 337 335 { 338 336 #ifdef __EXCEPTIONS 339 throw runtime_error("Column size mismatch");337 throw std::runtime_error("Column size mismatch"); 340 338 #else 341 339 gLog << ___err___ << "ERROR - Column size mismatch" << endl; … … 344 342 } 345 343 346 name = Get<st ring>("EXTNAME");344 name = Get<std::string>("EXTNAME"); 347 345 } 348 346 349 347 void PrintKeys(bool display_all=false) const 350 348 { 351 for (Keys::const_iterator it=keys. begin(); it!=keys.end(); it++)349 for (Keys::const_iterator it=keys.cbegin(); it!=keys.cend(); it++) 352 350 { 353 351 if (!display_all && … … 369 367 void PrintColumns() const 370 368 { 371 typedef map<pair<size_t,string>, Column> Sorted;369 typedef std::map<std::pair<size_t, std::string>, Column> Sorted; 372 370 373 371 Sorted sorted; 374 372 375 for (Columns::const_iterator it=cols. begin(); it!=cols.end(); it++)376 sorted[ make_pair(it->second.offset, it->first)] = it->second;377 378 for (Sorted::const_iterator it=sorted. begin(); it!=sorted.end(); it++)373 for (Columns::const_iterator it=cols.cbegin(); it!=cols.cend(); it++) 374 sorted[std::make_pair(it->second.offset, it->first)] = it->second; 375 376 for (Sorted::const_iterator it=sorted.cbegin(); it!=sorted.cend(); it++) 379 377 { 380 378 gLog << ___all___ << setw(6) << it->second.offset << "| "; … … 397 395 operator bool() const { return !name.empty(); } 398 396 399 bool HasKey(const st ring &key) const397 bool HasKey(const std::string &key) const 400 398 { 401 399 return keys.find(key)!=keys.end(); 402 400 } 403 401 404 bool HasColumn(const st ring& col) const402 bool HasColumn(const std::string& col) const 405 403 { 406 404 return cols.find(col)!=cols.end(); … … 419 417 // Values of keys are always signed 420 418 template<typename T> 421 T Get(const st ring &key) const422 { 423 const map<string,Entry>::const_iterator it = keys.find(key);419 T Get(const std::string &key) const 420 { 421 const std::map<std::string,Entry>::const_iterator it = keys.find(key); 424 422 if (it==keys.end()) 425 423 { 426 ostringstream str;427 str << "Key '" << key << "' not found." << endl;428 #ifdef __EXCEPTIONS 429 throw runtime_error(str.str());424 std::ostringstream str; 425 str << "Key '" << key << "' not found."; 426 #ifdef __EXCEPTIONS 427 throw std::runtime_error(str.str()); 430 428 #else 431 429 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 438 436 // Values of keys are always signed 439 437 template<typename T> 440 T Get(const st ring &key, const T &deflt) const441 { 442 const map<string,Entry>::const_iterator it = keys.find(key);438 T Get(const std::string &key, const T &deflt) const 439 { 440 const std::map<std::string,Entry>::const_iterator it = keys.find(key); 443 441 return it==keys.end() ? deflt :it->second.Get<T>(); 444 442 } 445 443 446 size_t GetN(const st ring &key) const444 size_t GetN(const std::string &key) const 447 445 { 448 446 const Columns::const_iterator it = cols.find(key); … … 485 483 486 484 protected: 487 ofstream fCopy;485 std::ofstream fCopy; 488 486 489 487 Table fTable; 490 488 491 typedef pair<void*, Table::Column> Address;492 typedef vector<Address> Addresses;489 typedef std::pair<void*, Table::Column> Address; 490 typedef std::vector<Address> Addresses; 493 491 //map<void*, Table::Column> fAddresses; 494 492 Addresses fAddresses; 495 493 496 494 #if defined(__MARS__) || defined(__CINT__) 497 typedef map<string, void*> Pointers;498 #else 499 typedef unordered_map<string, void*> Pointers;495 typedef std::map<std::string, void*> Pointers; 496 #else 497 typedef std::unordered_map<std::string, void*> Pointers; 500 498 #endif 501 499 Pointers fPointers; 502 500 503 vector<vector<char>> fGarbage;504 505 vector<char> fBufferRow;506 vector<char> fBufferDat;501 std::vector<std::vector<char>> fGarbage; 502 503 std::vector<char> fBufferRow; 504 std::vector<char> fBufferDat; 507 505 508 506 size_t fRow; … … 511 509 Checksum fChkData; 512 510 513 bool ReadBlock( vector<string> &vec)511 bool ReadBlock(std::vector<std::string> &vec) 514 512 { 515 513 int endtag = 0; … … 527 525 // return vector<string>(); 528 526 529 st ring str(c);527 std::string str(c); 530 528 531 529 // if (!str.empty()) … … 554 552 } 555 553 556 string Compile(const string &key, int16_t i=-1) const 557 { 558 if (i<0) 559 return key; 560 561 ostringstream str; 562 str << key << i; 563 return str.str(); 554 std::string Compile(const std::string &key, int16_t i=-1) const 555 { 556 return i<0 ? key : key+std::to_string(i); 564 557 } 565 558 … … 577 570 clear(rdstate()|ios::badbit); 578 571 #ifdef __EXCEPTIONS 579 throw runtime_error("File is not a FITS file.");572 throw std::runtime_error("File is not a FITS file."); 580 573 #else 581 574 gLog << ___err___ << "ERROR - File is not a FITS file." << endl; … … 588 581 while (good()) 589 582 { 590 vector<string> block;583 std::vector<std::string> block; 591 584 while (1) 592 585 { … … 605 598 clear(rdstate()|ios::badbit); 606 599 #ifdef __EXCEPTIONS 607 throw runtime_error("FITS file corrupted.");600 throw std::runtime_error("FITS file corrupted."); 608 601 #else 609 602 gLog << ___err___ << "ERROR - FITS file corrupted." << endl; … … 618 611 clear(rdstate()|ios::badbit); 619 612 #ifdef __EXCEPTIONS 620 throw runtime_error("END keyword missing in FITS header.");613 throw std::runtime_error("END keyword missing in FITS header."); 621 614 #else 622 615 gLog << ___err___ << "ERROR - END keyword missing in FITS file... file might be corrupted." << endl; … … 691 684 clear(rdstate()|ios::badbit); 692 685 #ifdef __EXCEPTIONS 693 throw runtime_error("Could not open output file.");686 throw std::runtime_error("Could not open output file."); 694 687 #else 695 688 gLog << ___err___ << "ERROR - Failed to open output file." << endl; … … 700 693 seekg(0); 701 694 702 vector<char> buf(p);695 std::vector<char> buf(p); 703 696 read(buf.data(), p); 704 697 … … 709 702 710 703 public: 711 fits(const st ring &fname, conststring& tableName="", bool force=false) : izstream(fname.c_str())704 fits(const std::string &fname, const std::string& tableName="", bool force=false) : izstream(fname.c_str()) 712 705 { 713 706 Constructor(fname, "", tableName, force); … … 716 709 { 717 710 #ifdef __EXCEPTIONS 718 throw runtime_error("You are trying to read a compressed fits with the base fits class. Please use factfits instead.");711 throw std::runtime_error("You are trying to read a compressed fits with the base fits class. Please use factfits instead."); 719 712 #else 720 713 gLog << ___err___ << "ERROR - You are trying to read a compressed fits with the base fits class. Please use factfits instead." << endl; … … 724 717 } 725 718 726 fits(const st ring &fname, const string &fout, conststring& tableName, bool force=false) : izstream(fname.c_str())719 fits(const std::string &fname, const std::string &fout, const std::string& tableName, bool force=false) : izstream(fname.c_str()) 727 720 { 728 721 Constructor(fname, fout, tableName, force); … … 731 724 { 732 725 #ifdef __EXCEPTIONS 733 throw runtime_error("You are trying to read a compressed fits with the base fits class. Please use factfits instead.");726 throw std::runtime_error("You are trying to read a compressed fits with the base fits class. Please use factfits instead."); 734 727 #else 735 728 gLog << ___err___ << "ERROR - You are trying to read a compressed fits with the base fits class. Please use factfits instead." << endl; … … 746 739 ~fits() 747 740 { 748 copy(istreambuf_iterator<char>(*this),749 istreambuf_iterator<char>(),750 ostreambuf_iterator<char>(fCopy));741 std::copy(std::istreambuf_iterator<char>(*this), 742 std::istreambuf_iterator<char>(), 743 std::ostreambuf_iterator<char>(fCopy)); 751 744 } 752 745 … … 776 769 } 777 770 778 void ZeroBufferForChecksum( vector<char>& vec, const uint64_t extraZeros=0)771 void ZeroBufferForChecksum(std::vector<char>& vec, const uint64_t extraZeros=0) 779 772 { 780 773 auto ib = vec.begin(); … … 842 835 const char *ptr = fBufferRow.data() + offset; 843 836 844 for (Addresses::const_iterator it=fAddresses. begin(); it!=fAddresses.end(); it++)837 for (Addresses::const_iterator it=fAddresses.cbegin(); it!=fAddresses.cend(); it++) 845 838 { 846 839 const Table::Column &c = it->second; … … 876 869 877 870 template<class T, class S> 878 const T &GetAs(const st ring &name)871 const T &GetAs(const std::string &name) 879 872 { 880 873 return *reinterpret_cast<S*>(fPointers[name]); 881 874 } 882 875 883 void *SetPtrAddress(const st ring &name)876 void *SetPtrAddress(const std::string &name) 884 877 { 885 878 if (fTable.cols.count(name)==0) 886 879 { 887 ostringstream str;888 str << "SetPtrAddress('" << name << "') - Column not found." << endl;889 #ifdef __EXCEPTIONS 890 throw runtime_error(str.str());880 std::ostringstream str; 881 str << "SetPtrAddress('" << name << "') - Column not found."; 882 #ifdef __EXCEPTIONS 883 throw std::runtime_error(str.str()); 891 884 #else 892 885 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 910 903 911 904 template<typename T> 912 bool SetPtrAddress(const st ring &name, T *ptr, size_t cnt)905 bool SetPtrAddress(const std::string &name, T *ptr, size_t cnt) 913 906 { 914 907 if (fTable.cols.count(name)==0) 915 908 { 916 ostringstream str;917 str << "SetPtrAddress('" << name << "') - Column not found." << endl;918 #ifdef __EXCEPTIONS 919 throw runtime_error(str.str());909 std::ostringstream str; 910 str << "SetPtrAddress('" << name << "') - Column not found."; 911 #ifdef __EXCEPTIONS 912 throw std::runtime_error(str.str()); 920 913 #else 921 914 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 926 919 if (sizeof(T)!=fTable.cols[name].size) 927 920 { 928 ostringstream str;921 std::ostringstream str; 929 922 str << "SetPtrAddress('" << name << "') - Element size mismatch: expected " 930 << fTable.cols[name].size << " from header, got " << sizeof(T) << endl;931 #ifdef __EXCEPTIONS 932 throw runtime_error(str.str());923 << fTable.cols[name].size << " from header, got " << sizeof(T); 924 #ifdef __EXCEPTIONS 925 throw std::runtime_error(str.str()); 933 926 #else 934 927 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 939 932 if (cnt!=fTable.cols[name].num) 940 933 { 941 ostringstream str;934 std::ostringstream str; 942 935 str << "SetPtrAddress('" << name << "') - Element count mismatch: expected " 943 << fTable.cols[name].num << " from header, got " << cnt << endl;944 #ifdef __EXCEPTIONS 945 throw runtime_error(str.str());936 << fTable.cols[name].num << " from header, got " << cnt; 937 #ifdef __EXCEPTIONS 938 throw std::runtime_error(str.str()); 946 939 #else 947 940 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 961 954 962 955 template<class T> 963 bool SetRefAddress(const st ring &name, T &ptr)956 bool SetRefAddress(const std::string &name, T &ptr) 964 957 { 965 958 return SetPtrAddress(name, &ptr, sizeof(ptr)/sizeof(T)); … … 967 960 968 961 template<typename T> 969 bool SetVecAddress(const st ring &name,vector<T> &vec)962 bool SetVecAddress(const std::string &name, std::vector<T> &vec) 970 963 { 971 964 return SetPtrAddress(name, vec.data(), vec.size()); … … 973 966 974 967 template<typename T> 975 T Get(const st ring &key) const968 T Get(const std::string &key) const 976 969 { 977 970 return fTable.Get<T>(key); … … 979 972 980 973 template<typename T> 981 T Get(const st ring &key, conststring &deflt) const974 T Get(const std::string &key, const std::string &deflt) const 982 975 { 983 976 return fTable.Get<T>(key, deflt); 984 977 } 985 978 986 bool SetPtrAddress(const st ring &name, void *ptr, size_t cnt=0)979 bool SetPtrAddress(const std::string &name, void *ptr, size_t cnt=0) 987 980 { 988 981 if (fTable.cols.count(name)==0) 989 982 { 990 ostringstream str;991 str <<"SetPtrAddress('" << name << "') - Column not found." << endl;992 #ifdef __EXCEPTIONS 993 throw runtime_error(str.str());983 std::ostringstream str; 984 str <<"SetPtrAddress('" << name << "') - Column not found."; 985 #ifdef __EXCEPTIONS 986 throw std::runtime_error(str.str()); 994 987 #else 995 988 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 1000 993 if (cnt && cnt!=fTable.cols[name].num) 1001 994 { 1002 ostringstream str;995 std::ostringstream str; 1003 996 str << "SetPtrAddress('" << name << "') - Element count mismatch: expected " 1004 << fTable.cols[name].num << " from header, got " << cnt << endl;1005 #ifdef __EXCEPTIONS 1006 throw runtime_error(str.str());997 << fTable.cols[name].num << " from header, got " << cnt; 998 #ifdef __EXCEPTIONS 999 throw std::runtime_error(str.str()); 1007 1000 #else 1008 1001 gLog << ___err___ << "ERROR - " << str.str() << endl; … … 1021 1014 } 1022 1015 1023 bool HasKey(const st ring &key) const { return fTable.HasKey(key); }1024 bool HasColumn(const st ring& col) const { return fTable.HasColumn(col);}1016 bool HasKey(const std::string &key) const { return fTable.HasKey(key); } 1017 bool HasColumn(const std::string& col) const { return fTable.HasColumn(col);} 1025 1018 const Table::Columns &GetColumns() const { return fTable.GetColumns();} 1026 1019 const Table::SortedColumns& GetSortedColumns() const { return fTable.sorted_cols;} 1027 1020 const Table::Keys &GetKeys() const { return fTable.GetKeys();} 1028 1021 1029 int64_t GetInt(conststring &key) const { return fTable.Get<int64_t>(key); }1030 uint64_t GetUInt(conststring &key) const { return fTable.Get<uint64_t>(key); }1031 double GetFloat(conststring &key) const { return fTable.Get<double>(key); }1032 st ring GetStr(const string &key) const { return fTable.Get<string>(key); }1033 1034 size_t GetN(const st ring &key) const1022 int64_t GetInt(const std::string &key) const { return fTable.Get<int64_t>(key); } 1023 uint64_t GetUInt(const std::string &key) const { return fTable.Get<uint64_t>(key); } 1024 double GetFloat(const std::string &key) const { return fTable.Get<double>(key); } 1025 std::string GetStr(const std::string &key) const { return fTable.Get<std::string>(key); } 1026 1027 size_t GetN(const std::string &key) const 1035 1028 { 1036 1029 return fTable.GetN(key);
Note:
See TracChangeset
for help on using the changeset viewer.