property.cpp 2.25 KB
/***************************************************************************
 *   Universidade Federal da Paraíba                                       *
 *   Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital      *
 *                                                                         *
 *   Centro de Informática - UFPB - Campus I                               *
 *   João Pessoa - PB - Brasil                                             *
 *                                                                         *
 *   Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br)  *
 *   Date: Qui Nov 28 14:06:10 BRT 2013                                    *
 *                                                                         *
 **************************************************************************/

 #include "property.h"


 namespace util {

 	
 	PropertyHandler::PropertyHandler(std::string _filename) 
 	{
 		if (checkFileExtension(_filename) != 1)
 			throw "Format file not is recognized";
 		
 		filename = _filename;
 	}

 	
 	PropertyHandler::~PropertyHandler() 
 	{
 		//todo
 	}


 	std::string PropertyHandler::getAttributeValue(std::string attr) 
 	{
 		std::ifstream file_property(filename.c_str());
 		std::string buffer_str;
 		try {
 			int target_pos = -1, begin = 0;
 			std::string attr_t, value_t;

	 		while (std::getline(file_property, buffer_str))
	 		{	 			
	 			target_pos = buffer_str.find("=");
	 			if (target_pos < 2)
	 				throw "The assignment symbol was not found.";
	 			
	 			attr_t = buffer_str.substr(begin, target_pos);
	 			begin = target_pos+1;
	 			value_t = buffer_str.substr(begin, buffer_str.size());
	 			if (attr.compare(attr_t) == 0) 
	 			{
	 				file_property.seekg (0, file_property.beg);
	 				return value_t;
	 			}
	 			target_pos = -1;
	 			begin = 0;
	 		}
 		} 
 		catch (std::exception& ex) 
 		{
 			printf("[Exception] File is completed\n[Error] %s\n", ex.what());
 		}
 		file_property.seekg (0, file_property.beg);
 		
 		return buffer_str;

 	}


 	#define EXTENSION ".conf"

 	int PropertyHandler::checkFileExtension(std::string &filename) {
 		
 		return (filename.find(EXTENSION) > 0 && 
 			(filename.size() == (filename.find(EXTENSION) + strlen(EXTENSION)))) ? 1 : -1; 		

 	}


 }