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 | int glev;
|
---|
15 | string desc;
|
---|
16 | };
|
---|
17 |
|
---|
18 | typedef list<hand_list_el> HandlerList;
|
---|
19 |
|
---|
20 | static HandlerList * hlistp = NULL;
|
---|
21 |
|
---|
22 | static inline void ChkHLP()
|
---|
23 | {
|
---|
24 | if (hlistp == NULL) hlistp = new HandlerList;
|
---|
25 | }
|
---|
26 | /*!
|
---|
27 | \param fhi : handler object pointer (created by new) which will be kept in the
|
---|
28 | handler list
|
---|
29 | \param glev : global priority level associated with the handler. Used when different
|
---|
30 | handlers return the same value for CheckHandling() or CheckReadability().
|
---|
31 | \param desc : classe name description associated with the handler
|
---|
32 | */
|
---|
33 | int FitsManager::RegisterHandler(FitsHandlerInterface * fhi, int glev, string desc)
|
---|
34 | {
|
---|
35 | ChkHLP();
|
---|
36 | if (fhi == NULL)
|
---|
37 | throw NullPtrError("FitsManager::RegisterHandler() fhi=NULL ");
|
---|
38 | HandlerList::iterator it;
|
---|
39 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
40 | if (typeid(*((*it).fhi)) == typeid(*fhi))
|
---|
41 | throw DuplicateIdExc("FitsManager::RegisterHandler() Already registered handler");
|
---|
42 | }
|
---|
43 | hand_list_el hle;
|
---|
44 | hle.fhi = fhi;
|
---|
45 | hle.glev = glev;
|
---|
46 | hle.desc = desc;
|
---|
47 | hlistp->push_back(hle);
|
---|
48 | return hlistp->size();
|
---|
49 | }
|
---|
50 |
|
---|
51 | int FitsManager::ListHandlers()
|
---|
52 | {
|
---|
53 | ChkHLP();
|
---|
54 | int kk=0;
|
---|
55 | cout << "---- FitsManager::ListHandlers() NbHandlers= " << hlistp->size()
|
---|
56 | << endl;
|
---|
57 | HandlerList::iterator it;
|
---|
58 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
59 | kk++;
|
---|
60 | cout << kk << "- " << (*it).desc << " : "
|
---|
61 | << typeid(*((*it).fhi)).name() << " glev= " <<(*it).glev << endl;
|
---|
62 | }
|
---|
63 | return hlistp->size();
|
---|
64 | }
|
---|
65 |
|
---|
66 | FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
|
---|
67 | {
|
---|
68 | ChkHLP();
|
---|
69 | FitsHandlerInterface * fhi = NULL;
|
---|
70 | HandlerList::iterator it;
|
---|
71 | int hfg = 0;
|
---|
72 | int bhfg = 0;
|
---|
73 | int clev = 0;
|
---|
74 | int blev = 0;
|
---|
75 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
76 | hfg = (*it).fhi->CheckHandling(o);
|
---|
77 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
78 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if (fhi == NULL) {
|
---|
82 | string msg = "FitsManager::FindHandler() Handler not found for ";
|
---|
83 | msg += typeid(o).name();
|
---|
84 | throw NotFoundExc(msg);
|
---|
85 | }
|
---|
86 | else return fhi;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void FitsManager::Write(FitsInOutFile& os, AnyDataObj & o)
|
---|
90 | {
|
---|
91 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
92 | fhi2->SetDataObj(o);
|
---|
93 | fhi2->Write(os);
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
|
---|
98 | {
|
---|
99 | FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
|
---|
100 | fhi2->SetDataObj(o);
|
---|
101 | fhi2->Read(is);
|
---|
102 | delete fhi2;
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 | FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
|
---|
107 | {
|
---|
108 | ChkHLP();
|
---|
109 | FitsHandlerInterface * fhi = NULL;
|
---|
110 | HandlerList::iterator it;
|
---|
111 | int hfg = 0;
|
---|
112 | int bhfg = 0;
|
---|
113 | int blev = 0;
|
---|
114 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
115 | hfg = (*it).fhi->CheckReadability(is);
|
---|
116 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
117 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | if (fhi == NULL) {
|
---|
121 | string msg = "FitsManager::FindReader() Reader/Handler not found ";
|
---|
122 | msg += is.FileName();
|
---|
123 | char buff[64];
|
---|
124 | sprintf(buff, " HDU= %d Type= %d", (int)(is.CurrentHDU()),
|
---|
125 | (int)(is.CurrentHDUType()) );
|
---|
126 | msg += buff;
|
---|
127 | throw NotFoundExc(msg);
|
---|
128 | }
|
---|
129 | else return fhi;
|
---|
130 | }
|
---|
131 |
|
---|
132 | FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
|
---|
133 | {
|
---|
134 | FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
|
---|
135 | fhi2->Read(is);
|
---|
136 | return fhi2;
|
---|
137 | }
|
---|