ATLAS Offline Software
Loading...
Searching...
No Matches
ALFA_ConfigParams.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5// DefaultParams.cpp: implementation of the ALFA_ConfigParams class.
6//
8
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
15// Construction/Destruction
17
19 : m_bIsValid (false),
20 m_strSection ("invalid")
21{
22}
23
27
29{
30 m_bIsValid=false;
31
32 m_strSection="invalid";
33 m_mapParams.clear();
34}
35
36int ALFA_ConfigParams::Init(const char *szFile, const char *szSection)
37{
38 bool bRes=false;
39 FILE* pfile=nullptr;
40 char szbuff[256],szsec[256];
41 char* ppv;
42
44
45 if((pfile=fopen(szFile,"r"))==nullptr) return false;
46
47 //read section
48 memset(szsec,0,sizeof(szsec));
49 if(szSection) strncpy(szsec,szSection,sizeof(szsec)-1);
50 //strlwr(szsec);
51 m_strSection=szsec;
52
53 while(!feof(pfile)){
54 fgets(szbuff,sizeof(szbuff),pfile);
55 if(*(szbuff+strlen(szbuff)-1)=='\n') *(szbuff+strlen(szbuff)-1)=0;
56 if(strcmp(szbuff,szsec) != 0) continue; //if(strcmp(strlwr(szbuff),szsec)) continue;
57 else{ bRes=true; break; }
58 }
59
60 if(!bRes){
61 fclose(pfile);
62 return 0;
63 }
64
65 while(!feof(pfile)){
66 fgets(szbuff,sizeof(szbuff),pfile);
67 if(*szbuff==0 || *szbuff==' ' || *szbuff=='\n' || *szbuff==';') continue;
68 if(*szbuff=='[') break;
69 if((ppv=strchr(szbuff,'='))==nullptr) continue;
70
71 if(*(szbuff+strlen(szbuff)-1)=='\n') *(szbuff+strlen(szbuff)-1)=0;
72 *ppv=0; ppv++;
73 m_mapParams.insert(MAPSTR2STR::value_type(szbuff,ppv)); //m_mapParams.insert(MAPSTR2STR::value_type(strlwr(szbuff),ppv));
74 }
75
76 fclose(pfile);
77 if(!m_mapParams.empty()) m_bIsValid=true;
78
79 return m_mapParams.size();
80}
81
82const char* ALFA_ConfigParams::GetParameter(const char *szKey) const
83{
84 if(!m_bIsValid) return nullptr;
85
86 char szbuff[256];
87 memset(szbuff,0,sizeof(szbuff));
88 if(szKey) strncpy(szbuff,szKey,sizeof(szbuff)-1);
89 //strlwr(szbuff);
90
91 MAPSTR2STR::const_iterator iter;
92 if((iter=m_mapParams.find(szbuff))!=m_mapParams.end()){
93 return (*iter).second.c_str();
94 }
95 else return nullptr;
96}
97
98bool ALFA_ConfigParams::IsKey(const char *szKey) const
99{
100 if(!m_bIsValid) return false;
101
102 char szbuff[256];
103 memset(szbuff,0,sizeof(szbuff));
104 if(szKey) strncpy(szbuff,szKey,sizeof(szbuff)-1);
105 //strlwr(szbuff);
106
107 MAPSTR2STR::const_iterator iter;
108 return (iter=m_mapParams.find(szbuff))!=m_mapParams.end();
109}
110
const char * GetParameter(const char *szKey) const
int Init(const char *szFile, const char *szSection)
bool IsKey(const char *szKey) const