コード例 #1
0
std::string ConfigReader::parseLine(const char* theLine,
    const unsigned long theCount, const std::string theSection)
{
    std::string anResult = theSection;
    size_t anLength = strlen(theLine);
    if (anLength > 1)
    {
        // Ignoramos los espacios
        size_t anOffset = 0;
        while (anOffset < anLength && theLine[anOffset] == ' ')
        {
            anOffset++;
        }

        // Comprobamos si hay comentarios
        if (theLine[anOffset] != '#' && theLine[anOffset] != ';')
        {
            // Comprobamos el comienzo de una nueva sección
            if (theLine[anOffset] == '[')
            {
                // Saltamos el inicio de sección '['
                anOffset++;

                // Ignoramos los espacios
                while (anOffset < anLength && theLine[anOffset] == ' ')
                {
                    anOffset++;
                }

                // Obtenemos el nombre de la sección mientras buscamos el final de la sección ']'
                size_t anIndex = 0;
                char anSection[MAX_CHARS] = { 0 };
                while ((anOffset + anIndex) < anLength && theLine[anOffset + anIndex] != ']')
                {
                    // Obtenemos el nombre de la sección de theLine
                    anSection[anIndex] = theLine[anOffset + anIndex];

                    // Incrementamos anIndex
                    anIndex++;
                }
                // Añadimos el delimitador de final de cadena
                anSection[anIndex] = '\0';
                // Eliminamos espacios finales
                while (anIndex > 0 && anSection[anIndex - 1] == ' ')
                {
                    // Ponemos el delimitador de final de cadena a la sección
                    anSection[--anIndex] = '\0';
                }

                //Solo actualizamos el nombre de sección si encontramos el deliminador de sección ']'
                //antes del final de la línea
                if ((anOffset + anIndex) < anLength && anIndex > 0)
                {
                    // Actualizamos el nombre de la sección
                    anResult = anSection;
                }
                else
                {
                    //TODO Usar GDE::Log::error(tag, text);
                    //ELOG() << "ConfigReader::ParseLine(" << theCount << ") missing section end marker ']'" << std::endl;
                }
            }
            // Leemos el par <clave,valor> de la sección actual.
            else
            {
                size_t anNameIndex = 0;
                char anName[MAX_CHARS];

                // Obtenemos la clave mientras buscamos "=" o ":"
                while ((anOffset + anNameIndex) < anLength &&
                    theLine[(anOffset + anNameIndex)] != '=' &&
                    theLine[(anOffset + anNameIndex)] != ':')
                {
                    // Obtenemos anName de theLine
                    anName[anNameIndex] = theLine[anOffset + anNameIndex];

                    // Incrementamos anNameIndex
                    anNameIndex++;
                }
                // Asignamos nuestro valor offset
                anOffset += anNameIndex;
                // Ponemos el delimitador de cadena
                anName[anNameIndex] = '\0';
                // Eliminamos los espacios del final
                while (anNameIndex > 0 && anName[anNameIndex - 1] == ' ')
                {
                    // Ponemos el delimitador de cadena
                    anName[--anNameIndex] = '\0';
                }

                // Solo buscamos el valor si encontramos el delimitador ":" o "="
                if (anOffset < anLength && anNameIndex > 0)
                {
                    size_t anValueIndex = 0;
                    char anValue[MAX_CHARS];

                    // Nos saltamos el delimitador ":" o "="
                    anOffset++;

                    // Eliminamos espacios
                    while (anOffset < anLength && theLine[anOffset] == ' ')
                    {
                        anOffset++;
                    }
                    // Obtenemos el valor mientras buscamos el final de línea o el comienzo de un comentario
                    while ((anOffset + anValueIndex) < anLength &&
                        theLine[(anOffset + anValueIndex)] != '\r' &&
                        theLine[(anOffset + anValueIndex)] != '\n' &&
                        theLine[(anOffset + anValueIndex)] != ';' &&
                        theLine[(anOffset + anValueIndex)] != '#')
                    {
                        // Obtenemos anValue de theLine
                        anValue[anValueIndex] = theLine[anOffset + anValueIndex];

                        // Incrementamos anValueIndex
                        anValueIndex++;
                    }
                    // Ponemos el delimitador de final de cadena
                    anValue[anValueIndex] = '\0';
                    // Eliminamos los espacios
                    while (anValueIndex > 0 && anValue[anValueIndex - 1] == ' ')
                    {
                        // Ponemos el delimitador de final de cadena
                        anValue[--anValueIndex] = '\0';
                    }

                    // Almacenamos el par <clave,valor>
                    storeNameValue(theSection, anName, anValue);
                }
                else
                {
                    //TODO Usar GDE::Log::error(tag,text);
                    //ELOG() << "ConfigReader::ParseLine(" << theCount << ") missing name or value delimiter of '=' or ':'" << std::endl;
                }
            }
        } // if(theLine[anOffset] != '#' && theLine[anOffset] != ';')
    } // if(anLength > 1)

    // Devolvemos la el nombre de la nueva sección en caso de encontrarla, en caso contrario
    // se devuelve el nombre de la sección anterior
    return anResult;
}
コード例 #2
0
std::string ConfigReader::parseLine(const char* theLine,
	const unsigned long theCount, const std::string theSection)
{
	std::string anResult = theSection;
	size_t anLength = strlen(theLine);
	if (anLength > 1)
	{
		// Skip preceeding spaces at the begining of the line
		size_t anOffset = 0;
		while (anOffset < anLength && theLine[anOffset] == ' ')
		{
			anOffset++;
		}

		// Now check for comments
		if (theLine[anOffset] != '#' && theLine[anOffset] != ';')
		{
			// Next check for the start of a new section
			if (theLine[anOffset] == '[')
			{
				// Skip over the begin section marker '['
				anOffset++;

				// Skip preceeding spaces of section name
				while (anOffset < anLength && theLine[anOffset] == ' ')
				{
					anOffset++;
				}

				// Retrieve the section name while looking for the section end marker ']'
				size_t anIndex = 0;
				char anSection[MAX_CHARS] = { 0 };
				while ((anOffset + anIndex) < anLength && theLine[anOffset + anIndex] != ']')
				{
					// Retrieve the section name from theLine
					anSection[anIndex] = theLine[anOffset + anIndex];

					// Increment anIndex
					anIndex++;
				}
				// Add null terminator
				anSection[anIndex] = '\0';
				// Remove trailing spaces
				while (anIndex > 0 && anSection[anIndex - 1] == ' ')
				{
					// Put a null terminator at the end of the section name
					anSection[--anIndex] = '\0';
				}

				// Only update the current section name if we found the section end
				// marker before the end of the line
				if ((anOffset + anIndex) < anLength && anIndex > 0)
				{
					// Change the return string to the newly parsed section name
					anResult = anSection;
				}
				else
				{
					//ELOG() << "ConfigReader::ParseLine(" << theCount << ") missing section end marker ']'" << std::endl;
				}
			}
			// Just read the name=value pair into the current section
			else
			{
				// Retrieve the name and value while looking for the comment flags ';' or '#'
				size_t anNameIndex = 0;
				char anName[MAX_CHARS];

				// First retrieve the name while looking for either the '=' or ':' delimiter
				while ((anOffset + anNameIndex) < anLength &&
					theLine[(anOffset + anNameIndex)] != '=' &&
					theLine[(anOffset + anNameIndex)] != ':')
				{
					// Retrieve anName from theLine
					anName[anNameIndex] = theLine[anOffset + anNameIndex];

					// Increment anNameIndex
					anNameIndex++;
				}
				// Assign our starting offset value
				anOffset += anNameIndex;
				// Put a null terminator at the end of the name
				anName[anNameIndex] = '\0';
				// Remove trailing spaces from the name
				while (anNameIndex > 0 && anName[anNameIndex - 1] == ' ')
				{
					// Put a null terminator at the end of the name
					anName[--anNameIndex] = '\0';
				}

				// Only search for the value if we found the '=' or ':' delimiter
				if (anOffset < anLength && anNameIndex > 0)
				{
					size_t anValueIndex = 0;
					char anValue[MAX_CHARS];

					// Skip over the delimiter between name and value '=' or ':'
					anOffset++;

					// Skip preceeding spaces
					while (anOffset < anLength && theLine[anOffset] == ' ')
					{
						anOffset++;
					}
					// Next retrieve the value while looking for comments flags ';' or '#'
					while ((anOffset + anValueIndex) < anLength &&
						theLine[(anOffset + anValueIndex)] != '\r' &&
						theLine[(anOffset + anValueIndex)] != '\n' &&
						theLine[(anOffset + anValueIndex)] != ';' &&
						theLine[(anOffset + anValueIndex)] != '#')
					{
						// Retrieve anValue from theLine
						anValue[anValueIndex] = theLine[anOffset + anValueIndex];

						// Increment anValueIndex
						anValueIndex++;
					}
					// Put a null terminator at the end of the section name
					anValue[anValueIndex] = '\0';
					// Remove trailing spaces from the name
					while (anValueIndex > 0 && anValue[anValueIndex - 1] == ' ')
					{
						// Put a null terminator at the end of the name
						anValue[--anValueIndex] = '\0';
					}

					// Store the name,value pair obtained into the current section
					storeNameValue(theSection, anName, anValue);
				}
				else
				{
					//ELOG() << "ConfigReader::ParseLine(" << theCount << ") missing name or value delimiter of '=' or ':'" << std::endl;
				}
			}
		} // if(theLine[anOffset] != '#' && theLine[anOffset] != ';') // Not a comment
	} // if(anLength > 1)

	// Return either the previous section name or the new section name found
	return anResult;
}