property.cpp
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/***************************************************************************
* 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;
}
}