[2820] | 1 | #include "machdefs.h"
|
---|
| 2 | #include "sopnamsp.h"
|
---|
| 3 |
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <string.h>
|
---|
| 6 | #include <iostream>
|
---|
| 7 | #include <typeinfo>
|
---|
| 8 |
|
---|
| 9 | #include "fitsmanager.h"
|
---|
| 10 | #include "fitshandler.h"
|
---|
| 11 |
|
---|
| 12 | struct hand_list_el {
|
---|
| 13 | FitsHandlerInterface * fhi;
|
---|
| 14 | string clname;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | typedef list<hand_list_el> HandlerList;
|
---|
| 18 |
|
---|
| 19 | static HandlerList * hlistp = NULL;
|
---|
| 20 |
|
---|
| 21 | static inline void ChkHLP()
|
---|
| 22 | {
|
---|
[2843] | 23 | if (hlistp == NULL) hlistp = new HandlerList;
|
---|
[2820] | 24 | }
|
---|
| 25 |
|
---|
[2864] | 26 | int FitsManager::RegisterHandler(FitsHandlerInterface * fhi, string clname)
|
---|
[2820] | 27 | {
|
---|
[2864] | 28 | ChkHLP();
|
---|
[2820] | 29 | if (fhi == NULL)
|
---|
| 30 | throw NullPtrError("FitsManager::RegisterHandler() fhi=NULL ");
|
---|
| 31 | HandlerList::iterator it;
|
---|
| 32 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 33 | if (typeid(*((*it).fhi)) == typeid(*fhi))
|
---|
| 34 | throw DuplicateIdExc("FitsManager::RegisterHandler() Already registered handler");
|
---|
| 35 | }
|
---|
| 36 | hand_list_el hle;
|
---|
| 37 | hle.fhi = fhi;
|
---|
| 38 | hle.clname = clname;
|
---|
| 39 | hlistp->push_back(hle);
|
---|
| 40 | return hlistp->size();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | int FitsManager::ListHandlers()
|
---|
| 44 | {
|
---|
[2864] | 45 | ChkHLP();
|
---|
[2820] | 46 | int kk=0;
|
---|
| 47 | cout << "---- FitsManager::ListHandlers() NbHandlers= " << hlistp->size()
|
---|
| 48 | << endl;
|
---|
| 49 | HandlerList::iterator it;
|
---|
| 50 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 51 | kk++;
|
---|
| 52 | cout << kk << "- " << (*it).clname << " : "
|
---|
| 53 | << typeid(*((*it).fhi)).name() << endl;
|
---|
| 54 | }
|
---|
| 55 | return hlistp->size();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
|
---|
| 59 | {
|
---|
[2864] | 60 | ChkHLP();
|
---|
[2820] | 61 | FitsHandlerInterface * fhi = NULL;
|
---|
| 62 | HandlerList::iterator it;
|
---|
[2864] | 63 | int hfg = 0;
|
---|
| 64 | int bhfg = 0;
|
---|
| 65 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 66 | hfg = (*it).fhi->CheckHandling(o);
|
---|
| 67 | if ( hfg > bhfg ) {
|
---|
| 68 | fhi = (*it).fhi; bhfg = hfg;
|
---|
[2820] | 69 | }
|
---|
[2864] | 70 | }
|
---|
[2820] | 71 | if (fhi == NULL) {
|
---|
| 72 | string msg = "FitsManager::FindHandler() Handler not found for ";
|
---|
| 73 | msg += typeid(o).name();
|
---|
| 74 | throw NotFoundExc(msg);
|
---|
| 75 | }
|
---|
| 76 | else return fhi;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void FitsManager::Write(FitsInOutFile& os, AnyDataObj & o)
|
---|
| 80 | {
|
---|
| 81 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
| 82 | fhi2->SetDataObj(o);
|
---|
| 83 | fhi2->Write(os);
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[2864] | 87 | void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
|
---|
[2820] | 88 | {
|
---|
| 89 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
| 90 | fhi2->SetDataObj(o);
|
---|
[2864] | 91 | fhi2->Read(is);
|
---|
| 92 | delete fhi2;
|
---|
[2820] | 93 | return;
|
---|
| 94 | }
|
---|
[2864] | 95 |
|
---|
| 96 | FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
|
---|
| 97 | {
|
---|
| 98 | ChkHLP();
|
---|
| 99 | FitsHandlerInterface * fhi = NULL;
|
---|
| 100 | HandlerList::iterator it;
|
---|
| 101 | int hfg = 0;
|
---|
| 102 | int bhfg = 0;
|
---|
| 103 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 104 | hfg = (*it).fhi->CheckReadability(is);
|
---|
| 105 | if ( hfg > bhfg ) {
|
---|
| 106 | fhi = (*it).fhi; bhfg = hfg;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | if (fhi == NULL) {
|
---|
| 110 | string msg = "FitsManager::FindReader() Reader/Handler not found ";
|
---|
| 111 | msg += is.FileName();
|
---|
| 112 | char buff[64];
|
---|
| 113 | sprintf(buff, " HDU= %d Type= %d", (int)(is.CurrentHDU()),
|
---|
| 114 | (int)(is.CurrentHDUType()) );
|
---|
| 115 | msg += buff;
|
---|
| 116 | throw NotFoundExc(msg);
|
---|
| 117 | }
|
---|
| 118 | else return fhi;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
|
---|
| 122 | {
|
---|
| 123 | FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
|
---|
| 124 | fhi2->Read(is);
|
---|
| 125 | return fhi2;
|
---|
| 126 | }
|
---|