Diana Software
QGlobalWriterDispatcher.cc
Go to the documentation of this file.
2 #include "QGlobalRWFactory.hh"
3 #include "QGlobalWriter.hh"
4 #include "QMessage.hh"
5 #include "QError.hh"
6 
7 using namespace Diana;
8 
10 {
11  static QGlobalWriterDispatcher dispatcher;
12  return dispatcher;
13 }
14 
15 
17 {
18  fLastWriter = NULL;
20 }
21 
23 {
24  std::string fname = filename;
25 
26  // check for current writer
27  bool isWriter = false;
28  if(fname.find(Q_CURRENT_WRITER,0) != std::string::npos) {
29  if(!fCurrentWriterFilename.empty())
30  fname = fCurrentWriterFilename;
31  isWriter = true;
32  }
33  // check for already open files
34  /* deprecated
35  if(fname == "same") {
36  if(!fLastWriter) QMessageHandler::Error(GetName(), std::string("Cannot write to the \"same\" file, as none has been previously well defined!"));
37  return fLastWriter;
38  } else */
39  if(fWriters.find(fname) != fWriters.end()) {
40  fLastWriter = fWriters[fname];
41  return fLastWriter;
42  }
43 
44  // Create the GlobalWriter for filename checking its extension
45  fLastWriter = NULL;
46  size_t dotpos = fname.find_last_of(".");
47  std::string fileExtension = "";
48  if(dotpos != std::string::npos)
49  fileExtension = fname.substr( dotpos );
50 
51 
52  std::list<const QGlobalWriterFactory*> writerFactories = QGlobalRWFactory::GetInstance().GetListOfGlobalWriters();
53  std::list<const QGlobalWriterFactory*>::iterator iter;
54  for(iter = writerFactories.begin(); iter!= writerFactories.end(); iter++)
55  if((*iter)->GetExt() == fileExtension)
57  // defaults
58  if(fLastWriter == 0) {
59  if(fileExtension == ".root") {
60  QGlobalRWFactory::GetInstance().LoadPlugin("LRootGlobalWriter");
61  QMessageHandler::Info(GetName(), std::string("loaded default g-writer LRootGlobalWriter"));
62  }
63  else if(fileExtension == ".txt") {
64  QGlobalRWFactory::GetInstance().LoadPlugin("LASCIIGlobalWriter");
65  QMessageHandler::Info(GetName(), std::string("loaded default g-writer LASCIIGlobalWriter"));
66  }
67  else if(fileExtension == ".gui") {
68  QGlobalRWFactory::GetInstance().LoadPlugin("LGuiSessionWriter");
69  QMessageHandler::Info(GetName(), std::string("loaded default g-writer LGUISessionWriter"));
70  }
71  std::list<const QGlobalWriterFactory*> writerFactories = QGlobalRWFactory::GetInstance().GetListOfGlobalWriters();
72  for(iter = writerFactories.begin(); iter!= writerFactories.end(); iter++)
73  if((*iter)->GetExt() == fileExtension)
75  }
76 
77 
78  // no writer found
79  if(fLastWriter == NULL){
81  err.SetDescription(GetName(),__LINE__,std::string("No writer found for extension: \"") + fileExtension + "\"" + " (file: "+ fname +")");
82  return NULL;
83  }
84 
85  // open file and register it
86  if(isWriter)
87  err = fLastWriter->Open(fname,"CURRENT_WRITER");
88  else
89  err = fLastWriter->Open(fname);
90 
91  if(err != QERR_SUCCESS) {
92  delete fLastWriter;
93  return NULL;
94  }
95  fWriters[fname] = fLastWriter;
96  return fLastWriter;
97 }
98 
100 {
101  Clear();
102 }
103 
104 
106 {
107  std::map<std::string, QGlobalWriter*>::iterator iter;
108  for(iter=fWriters.begin(); iter!=fWriters.end(); iter++) {
109  QGlobalWriter* writer = iter->second;
110  if(writer) {
111  writer->Close();
112  delete writer;
113  iter->second = 0;
114  }
115  }
116  fWriters.clear();
117  fLastWriter = NULL;
118 }
err
Definition: CheckOF.C:114
#define Q_CURRENT_WRITER
Definition: QDiana.hh:40
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
error class with error type and description
Definition: QError.hh:115
std::list< const QGlobalWriterFactory * > GetListOfGlobalWriters() const
static QGlobalRWFactory & GetInstance()
void LoadPlugin(const std::string &name)
QGlobalWriter * CreateGlobalWriter(const std::string &name)
Class that returns a pointer to the writer matching the given input filename.
QGlobalWriter * GetWriter(const std::string &filename, QError &err)
get writer
std::map< std::string, QGlobalWriter * > fWriters
static QGlobalWriterDispatcher & GetInstance()
Abstract class for global writers.
virtual QError Open(const std::string &filename, const std::string &opt="")=0
Open file, called by QGlobalWriterDispatcher.
virtual QError Close()=0
Close file, called by QGlobalWriterDispatcher.
static void Info(const std::string &sender, const std::string &msg)
Definition: QMessage.hh:40
base class for anything that has a name
Definition: QNamed.hh:14
const std::string & GetName() const
Definition: QNamed.hh:19
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...