94 lines
2.5 KiB
C++

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2007 - 2008.
//
// File: CFParser.cpp
//
// Contents: Parses a file to a canonical string hash using the ACE
// Configuration type parsing
//
// Notes:
//----------------------------------------------------------------------------
#include "CFParser.h"
#include <ace/Configuration_Import_Export.h>
#include <ace/Configuration.h>
int CF_Parser::parseToCanonicalStringHash(const ACE_TCHAR * fileName, ACE_TStringHash &strHash)
{
int status = PARSER_STATUS_FAILURE;
if (fileName == NULL)
{
return status;
}
ACE_TString sectionName, elementName, elementValue;
int elementIndex;
int sectionIndex = 0;
int sectionIterStatus, elementsIterStatus;
ACE_Configuration_Section_Key section_key;
ACE_Configuration::VALUETYPE valueType;
// Test import of a legit INI format from a previously-existing file.
ACE_Configuration_Heap cf;
int ace_status;
if ((ace_status = (cf.open ())) != 0)
{
ACE_ERROR_RETURN ((MY_ERROR ACE_TEXT ("ACE_Configuration_Heap::open returned %d\n"),
ace_status), PARSER_STATUS_FAILURE);
}
ACE_Ini_ImpExp import (cf);
// This one should work...
ace_status = import.import_config (ACE_TEXT (fileName));
if (ace_status != 0)
{
ACE_ERROR_RETURN ((MY_ERROR ACE_TEXT ("%s import failed\n"), fileName),
PARSER_STATUS_FAILURE);
}
const ACE_Configuration_Section_Key &root = cf.root_section ();
while ((sectionIterStatus = cf.enumerate_sections(root, sectionIndex, sectionName)) == 0)
{
// Open next section
if (cf.open_section(root, sectionName.c_str(), STATUS_SUCCESS, section_key) != PARSER_STATUS_SUCCESS)
{
return status;
}
sectionIndex++;
elementIndex = 0;
while ((elementsIterStatus = cf.enumerate_values(section_key, elementIndex, elementName, valueType)) == 0)
{
elementIndex++;
if (valueType != ACE_Configuration::STRING)
{
return status;
}
// Can get the value now and add it to the hash
if (cf.get_string_value(section_key, elementName.c_str(), elementValue) != PARSER_STATUS_SUCCESS)
{
return status;
}
strHash.addInner(sectionName, elementName, elementValue);
}
// iteration status -1 means an error, and 1 means the iteration ended
if (elementsIterStatus == -1)
{
return status;
}
}
// iteration status -1 means an error, and 1 means the iteration ended
if (sectionIterStatus == -1)
{
return status;
}
status = PARSER_STATUS_SUCCESS;
return status;
}