source: Sophya/trunk/SophyaExt/FitsIOServer/fitsmanager.cc@ 2895

Last change on this file since 2895 was 2895, checked in by ansari, 20 years ago

Ajout priorite a l'enregistrement pour Fitshandlers - Reza 12/01/2006

  • Property svn:executable set to *
File size: 3.5 KB
RevLine 
[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
12struct hand_list_el {
13 FitsHandlerInterface * fhi;
[2895]14 int glev;
15 string desc;
[2820]16};
17
18typedef list<hand_list_el> HandlerList;
19
20static HandlerList * hlistp = NULL;
21
22static inline void ChkHLP()
23{
[2843]24 if (hlistp == NULL) hlistp = new HandlerList;
[2820]25}
[2895]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*/
33int FitsManager::RegisterHandler(FitsHandlerInterface * fhi, int glev, string desc)
[2820]34{
[2864]35 ChkHLP();
[2820]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;
[2895]45 hle.glev = glev;
46 hle.desc = desc;
[2820]47 hlistp->push_back(hle);
48 return hlistp->size();
49}
50
51int FitsManager::ListHandlers()
52{
[2864]53 ChkHLP();
[2820]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++;
[2895]60 cout << kk << "- " << (*it).desc << " : "
61 << typeid(*((*it).fhi)).name() << " glev= " <<(*it).glev << endl;
[2820]62 }
63 return hlistp->size();
64}
65
66FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
67{
[2864]68 ChkHLP();
[2820]69 FitsHandlerInterface * fhi = NULL;
70 HandlerList::iterator it;
[2864]71 int hfg = 0;
72 int bhfg = 0;
[2895]73 int clev = 0;
74 int blev = 0;
[2864]75 for(it = hlistp->begin(); it != hlistp->end(); it++) {
76 hfg = (*it).fhi->CheckHandling(o);
[2895]77 if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
78 fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
[2820]79 }
[2864]80 }
[2820]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
89void 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
[2864]97void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
[2820]98{
99 FitsHandlerInterface * fhi2 = FindHandler(o)->Clone();
100 fhi2->SetDataObj(o);
[2864]101 fhi2->Read(is);
102 delete fhi2;
[2820]103 return;
104}
[2864]105
106FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
107{
108 ChkHLP();
109 FitsHandlerInterface * fhi = NULL;
110 HandlerList::iterator it;
111 int hfg = 0;
112 int bhfg = 0;
[2895]113 int blev = 0;
[2864]114 for(it = hlistp->begin(); it != hlistp->end(); it++) {
115 hfg = (*it).fhi->CheckReadability(is);
[2895]116 if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
117 fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
[2864]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
132FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
133{
134 FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
135 fhi2->Read(is);
136 return fhi2;
137}
Note: See TracBrowser for help on using the repository browser.