22// properties.cpp
33//
44// Circle - A C++ bare metal environment for Raspberry Pi
5- // Copyright (C) 2016-2021 R. Stange <rsta2@o2online.de>
5+ // Copyright (C) 2016-2023 R. Stange <rsta2@o2online.de>
66//
77// This program is free software: you can redistribute it and/or modify
88// it under the terms of the GNU General Public License as published by
1818// along with this program. If not, see <http://www.gnu.org/licenses/>.
1919//
2020#include < Properties/properties.h>
21+ #include < circle/ptrarray.h>
2122#include < circle/util.h>
2223#include < circle/logger.h>
2324#include < assert.h>
2425
26+ struct TSection
27+ {
28+ CString Name;
29+ CPtrArray PropArray;
30+ };
31+
2532struct TPropertyPair
2633{
2734 char *pName;
2835 char *pValue;
2936};
3037
38+ const char CProperties::DefaultSection[] = " " ;
39+
3140CProperties::CProperties (void )
32- : m_nGetIndex (0 )
41+ : m_CurrentSectionName (DefaultSection),
42+ m_pCurrentSection (nullptr ),
43+ m_pGetSection (nullptr ),
44+ m_nGetIndex (0 )
3345{
3446}
3547
@@ -38,6 +50,13 @@ CProperties::~CProperties (void)
3850 RemoveAll ();
3951}
4052
53+ void CProperties::SelectSection (const char *pSectionName)
54+ {
55+ m_CurrentSectionName = pSectionName;
56+
57+ m_pCurrentSection = LookupSection (pSectionName);
58+ }
59+
4160boolean CProperties::IsSet (const char *pPropertyName) const
4261{
4362 return Lookup (pPropertyName) != 0 ;
@@ -214,20 +233,35 @@ void CProperties::SetIPAddress (const char *pPropertyName, const u8 *pAddress)
214233
215234void CProperties::RemoveAll (void )
216235{
217- for (unsigned i = 0 ; i < m_PropArray.GetCount (); i++)
236+ TPtrListElement *pElement;
237+ while ((pElement = m_SectionList.GetFirst ()) != 0 )
218238 {
219- TPropertyPair *pProperty = (TPropertyPair *) m_PropArray[i];
220- assert (pProperty != 0 );
239+ TSection *pSection = (TSection *) m_SectionList.GetPtr (pElement);
240+ assert (pSection != 0 );
241+ CPtrArray *pPropArray = &pSection->PropArray ;
221242
222- delete [] ( char *) pProperty-> pName ;
223- delete [] ( char *) pProperty-> pValue ;
224- delete pProperty;
225- }
243+ for ( unsigned i = 0 ; i < pPropArray-> GetCount (); i++)
244+ {
245+ TPropertyPair * pProperty = (TPropertyPair *) (*pPropArray)[i] ;
246+ assert (pProperty != 0 );
226247
227- while (m_PropArray.GetCount () > 0 )
228- {
229- m_PropArray.RemoveLast ();
248+ delete [] (char *) pProperty->pName ;
249+ delete [] (char *) pProperty->pValue ;
250+ delete pProperty;
251+ }
252+
253+ while (pPropArray->GetCount () > 0 )
254+ {
255+ pPropArray->RemoveLast ();
256+ }
257+
258+ delete pSection;
259+
260+ m_SectionList.Remove (pElement);
230261 }
262+
263+ m_CurrentSectionName = DefaultSection;
264+ m_pCurrentSection = nullptr ;
231265}
232266
233267void CProperties::AddProperty (const char *pPropertyName, const char *pValue)
@@ -252,27 +286,97 @@ void CProperties::AddProperty (const char*pPropertyName, const char *pValue)
252286 assert (pProperty->pValue != 0 );
253287 strcpy (pProperty->pValue , pValue);
254288
255- m_PropArray.Append (pProperty);
289+ if (m_pCurrentSection == 0 )
290+ {
291+ m_pCurrentSection = new TSection;
292+ assert (m_pCurrentSection != 0 );
293+
294+ m_pCurrentSection->Name = m_CurrentSectionName;
295+
296+ TPtrListElement *pElement = m_SectionList.GetFirst ();
297+ while (pElement != 0 )
298+ {
299+ TPtrListElement *pNext = m_SectionList.GetNext (pElement);
300+ if (pNext == 0 )
301+ {
302+ break ;
303+ }
304+
305+ pElement = pNext;
306+ }
307+
308+ m_SectionList.InsertAfter (pElement, m_pCurrentSection);
309+ }
310+
311+ m_pCurrentSection->PropArray .Append (pProperty);
256312}
257313
258314boolean CProperties::GetFirst (void )
259315{
316+ m_pGetSection = m_SectionList.GetFirst ();
260317 m_nGetIndex = 0 ;
261318
262- return m_PropArray.GetCount () > m_nGetIndex;
319+ if (m_pGetSection == 0 )
320+ {
321+ m_CurrentSectionName = DefaultSection;
322+
323+ return FALSE ;
324+ }
325+
326+ TSection *pSection = (TSection *) m_SectionList.GetPtr (m_pGetSection);
327+ assert (pSection != 0 );
328+
329+ m_CurrentSectionName = pSection->Name ;
330+
331+ return pSection->PropArray .GetCount () > m_nGetIndex;
263332}
264333
265334boolean CProperties::GetNext (void )
266335{
267336 m_nGetIndex++;
268337
269- return m_PropArray.GetCount () > m_nGetIndex;
338+ assert (m_pGetSection != 0 );
339+ TSection *pSection = (TSection *) m_SectionList.GetPtr (m_pGetSection);
340+ assert (pSection != 0 );
341+
342+ if (pSection->PropArray .GetCount () > m_nGetIndex)
343+ {
344+ return TRUE ;
345+ }
346+
347+ m_pGetSection = m_SectionList.GetNext (m_pGetSection);
348+ if (m_pGetSection == 0 )
349+ {
350+ m_CurrentSectionName = DefaultSection;
351+ m_pCurrentSection = LookupSection (m_CurrentSectionName);
352+
353+ return FALSE ;
354+ }
355+
356+ pSection = (TSection *) m_SectionList.GetPtr (m_pGetSection);
357+ assert (pSection != 0 );
358+
359+ m_CurrentSectionName = pSection->Name ;
360+ m_pCurrentSection = pSection;
361+
362+ m_nGetIndex = 0 ;
363+
364+ return TRUE ;
365+ }
366+
367+ const char *CProperties::GetSectionName (void ) const
368+ {
369+ return m_CurrentSectionName;
270370}
271371
272372const char *CProperties::GetName (void ) const
273373{
274- assert (m_nGetIndex < m_PropArray.GetCount ());
275- TPropertyPair *pProperty = (TPropertyPair *) m_PropArray[m_nGetIndex];
374+ assert (m_pGetSection != 0 );
375+ TSection *pSection = (TSection *) m_SectionList.GetPtr (m_pGetSection);
376+ assert (pSection != 0 );
377+
378+ assert (m_nGetIndex < pSection->PropArray .GetCount ());
379+ TPropertyPair *pProperty = (TPropertyPair *) pSection->PropArray [m_nGetIndex];
276380 assert (pProperty != 0 );
277381 assert (pProperty->pName != 0 );
278382
@@ -281,19 +385,50 @@ const char *CProperties::GetName (void) const
281385
282386const char *CProperties::GetValue (void ) const
283387{
284- assert (m_nGetIndex < m_PropArray.GetCount ());
285- TPropertyPair *pProperty = (TPropertyPair *) m_PropArray[m_nGetIndex];
388+ assert (m_pGetSection != 0 );
389+ TSection *pSection = (TSection *) m_SectionList.GetPtr (m_pGetSection);
390+ assert (pSection != 0 );
391+
392+ assert (m_nGetIndex < pSection->PropArray .GetCount ());
393+ TPropertyPair *pProperty = (TPropertyPair *) pSection->PropArray [m_nGetIndex];
286394 assert (pProperty != 0 );
287395 assert (pProperty->pValue != 0 );
288396
289397 return pProperty->pValue ;
290398}
291399
400+ TSection *CProperties::LookupSection (const char *pSectionName) const
401+ {
402+ assert (pSectionName != 0 );
403+
404+ TPtrListElement *pElement = m_SectionList.GetFirst ();
405+ while (pElement != 0 )
406+ {
407+ TSection *pSection = (TSection *) m_SectionList.GetPtr (pElement);
408+ assert (pSection != 0 );
409+
410+ if (pSection->Name .Compare (pSectionName) == 0 )
411+ {
412+ return pSection;
413+ }
414+
415+ pElement = m_SectionList.GetNext (pElement);
416+ }
417+
418+ return 0 ;
419+ }
420+
421+
292422TPropertyPair *CProperties::Lookup (const char *pPropertyName) const
293423{
294- for ( unsigned i = 0 ; i < m_PropArray. GetCount (); i++ )
424+ if (m_pCurrentSection == 0 )
295425 {
296- TPropertyPair *pProperty = (TPropertyPair *) m_PropArray[i];
426+ return 0 ;
427+ }
428+
429+ for (unsigned i = 0 ; i < m_pCurrentSection->PropArray .GetCount (); i++)
430+ {
431+ TPropertyPair *pProperty = (TPropertyPair *) m_pCurrentSection->PropArray [i];
297432 assert (pProperty != 0 );
298433 if (strcmp (pProperty->pName , pPropertyName) == 0 )
299434 {
@@ -308,14 +443,30 @@ TPropertyPair *CProperties::Lookup (const char*pPropertyName) const
308443
309444void CProperties::Dump (const char *pSource) const
310445{
311- CLogger::Get ()->Write (pSource, LogDebug, " Dumping %u properties:" , m_PropArray. GetCount () );
446+ CLogger::Get ()->Write (pSource, LogDebug, " Dumping properties:" );
312447
313- for (unsigned i = 0 ; i < m_PropArray.GetCount (); i++)
448+ TPtrListElement *pElement = m_SectionList.GetFirst ();
449+ while (pElement != 0 )
314450 {
315- TPropertyPair *pProperty = (TPropertyPair *) m_PropArray[i];
316- assert (pProperty != 0 );
451+ TSection *pSection = (TSection *) m_SectionList.GetPtr (pElement);
452+ assert (pSection != 0 );
453+
454+ if (pSection->Name .Compare (DefaultSection) != 0 )
455+ {
456+ CLogger::Get ()->Write (pSource, LogDebug, " [%s]" ,
457+ static_cast <const char *> (pSection->Name ));
458+ }
459+
460+ for (unsigned i = 0 ; i < pSection->PropArray .GetCount (); i++)
461+ {
462+ TPropertyPair *pProperty = (TPropertyPair *) pSection->PropArray [i];
463+ assert (pProperty != 0 );
464+
465+ CLogger::Get ()->Write (pSource, LogDebug, " %s=%s" ,
466+ pProperty->pName , pProperty->pValue );
467+ }
317468
318- CLogger::Get ()-> Write (pSource, LogDebug, " %s=%s " , pProperty-> pName , pProperty-> pValue );
469+ pElement = m_SectionList. GetNext (pElement );
319470 }
320471}
321472
0 commit comments