#include "TGridJDL.h"
#include "TObjString.h"
#include "Riostream.h"
ClassImp(TGridJDL)
TGridJDL::~TGridJDL()
{
Clear();
}
void TGridJDL::Clear(const Option_t*)
{
fMap.DeleteAll();
}
void TGridJDL::SetValue(const char *key, const char *value)
{
TObject *object = fMap.FindObject(key);
TPair *pair = dynamic_cast<TPair*>(object);
if (pair) {
TObject *oldObject = pair->Key();
if (oldObject) {
TObject *oldValue = pair->Value();
fMap.Remove(oldObject);
delete oldObject;
oldObject = 0;
if (oldValue) {
delete oldValue;
oldValue = 0;
}
}
}
fMap.Add(new TObjString(key), new TObjString(value));
}
const char *TGridJDL::GetValue(const char *key)
{
if (!key)
return 0;
TObject *object = fMap.FindObject(key);
if (!object)
return 0;
TPair *pair = dynamic_cast<TPair*>(object);
if (!pair)
return 0;
TObject *value = pair->Value();
if (!value)
return 0;
TObjString *string = dynamic_cast<TObjString*>(value);
if (!string)
return 0;
return string->GetName();
}
void TGridJDL::SetDescription(const char *key, const char* description)
{
TObject *object = fDescriptionMap.FindObject(key);
TPair *pair = dynamic_cast<TPair*>(object);
if (pair) {
TObject *oldObject = pair->Key();
if (oldObject) {
TObject *oldValue = pair->Value();
fDescriptionMap.Remove(oldObject);
delete oldObject;
oldObject = 0;
if (oldValue) {
delete oldValue;
oldValue = 0;
}
}
}
fDescriptionMap.Add(new TObjString(key), new TObjString(description));
}
const char *TGridJDL::GetDescription(const char *key)
{
if (!key)
return 0;
TObject *object = fDescriptionMap.FindObject(key);
if (!object)
return 0;
TPair *pair = dynamic_cast<TPair*>(object);
if (!pair)
return 0;
TObject *value = pair->Value();
if (!value)
return 0;
TObjString *string = dynamic_cast<TObjString*>(value);
if (!string)
return 0;
return string->GetName();
}
TString TGridJDL::AddQuotes(const char *value)
{
TString temp = TString("\"");
temp += value;
temp += "\"";
return temp;
}
void TGridJDL::AddToSet(const char *key, const char *value)
{
const char *oldValue = GetValue(key);
TString newString;
if (oldValue)
newString = oldValue;
if (newString.IsNull()) {
newString = "{";
} else {
newString.Remove(newString.Length()-1);
newString += ",";
}
newString += AddQuotes(value);
newString += "}";
SetValue(key, newString);
}
void TGridJDL::AddToSetDescription(const char *key, const char *description)
{
const char *oldValue = GetDescription(key);
TString newString;
if (oldValue)
newString = oldValue;
newString += description;
SetDescription(key, newString);
}
TString TGridJDL::Generate()
{
TString output("");
TIter next(&fMap);
TIter nextDescription(&fDescriptionMap);
TObject *object = 0;
TObject *objectD = 0;
while ((object = next())) {
TObjString *key = dynamic_cast<TObjString*>(object);
if (key) {
TObject *value = fMap.GetValue(object);
TObjString *valueobj = dynamic_cast<TObjString*>(value);
if (valueobj) {
nextDescription.Reset();
while ((objectD = nextDescription())) {
TObjString *keyD = dynamic_cast<TObjString*>(objectD);
if (keyD) {
TObject *valueD = fDescriptionMap.GetValue(objectD);
TObjString *valueobjD = dynamic_cast<TObjString*>(valueD);
if (valueobjD && !strcmp(key->GetName(), keyD->GetName())){
output += "# ";
output += valueobjD->GetName();
output += "\n";
}
}
}
output += key->GetName();
output += " = ";
output += valueobj->GetName();
output += ";\n\n";
}
}
}
return output;
}