Commit 913533997ca065118c4de5a2d37e105f165d154e

Authored by Eric Menezes Noronha
1 parent 77bad974
Exists in master

Funções e teste de getValueFromTags e getValueFromFile.

cacic-teste/testcacic.cpp
@@ -42,6 +42,13 @@ void CTestCacic::testComm() @@ -42,6 +42,13 @@ void CTestCacic::testComm()
42 42
43 void CTestCacic::testGetValueFromFile() 43 void CTestCacic::testGetValueFromFile()
44 { 44 {
45 - QVERIFY(OCacic.getValueFromFile("teste", "teste","E:/Lightbase/teste.txt") != ""); 45 + QVERIFY( OCacic.getValueFromFile("Teste", "teste","E:/teste.txt") == "Valor de teste");
46 } 46 }
47 47
  48 +void CTestCacic::testGetValueFromTags()
  49 +{
  50 + QString value = "blablab[teste]Valor de teste[/teste]feihgj";
  51 + QString retorno = OCacic.getValueFromTags(value, "teste");
  52 + qDebug(retorno.toLocal8Bit());
  53 + QVERIFY(retorno == "Valor de teste");
  54 +}
cacic-teste/testcacic.h
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 #include "../src/CACIC_comm.h" 3 #include "../src/CACIC_comm.h"
4 #include "../src/ccacic.h" 4 #include "../src/ccacic.h"
5 #include <QtTest/QtTest> 5 #include <QtTest/QtTest>
  6 +#include <QDebug>
6 7
7 class CTestCacic : public QObject 8 class CTestCacic : public QObject
8 { 9 {
@@ -22,6 +23,7 @@ private slots: @@ -22,6 +23,7 @@ private slots:
22 void testCommStatus(); 23 void testCommStatus();
23 void testComm(); 24 void testComm();
24 void testGetValueFromFile(); 25 void testGetValueFromFile();
  26 + void testGetValueFromTags();
25 }; 27 };
26 28
27 #endif // TESTINSTALLCACIC_H 29 #endif // TESTINSTALLCACIC_H
src/ccacic.cpp
@@ -4,6 +4,44 @@ CCacic::CCacic(QObject *parent) : @@ -4,6 +4,44 @@ CCacic::CCacic(QObject *parent) :
4 QObject(parent) 4 QObject(parent)
5 { 5 {
6 } 6 }
  7 +
  8 +QString CCacic::getValueFromFile(QString sectionName, QString keyName, QString filePath)
  9 +{
  10 + QFile file(filePath);
  11 + sectionName = "<" + sectionName + ">";
  12 + keyName = keyName + "=";
  13 + int sizeKeyName = keyName.size();
  14 +
  15 + if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  16 + return "0";
  17 +
  18 + QString line = file.readLine();
  19 + while (!file.atEnd()) {
  20 + if (line.contains(sectionName, Qt::CaseInsensitive)) {
  21 + line = file.readLine();
  22 + while (!file.atEnd()){
  23 + if (line.contains(keyName, Qt::CaseInsensitive)) {
  24 + file.close();
  25 + return line.mid(sizeKeyName).trimmed();
  26 + }
  27 + }
  28 + } else {
  29 + line = file.readLine();
  30 + }
  31 + }
  32 + file.close();
  33 + return "";
  34 +}
  35 +
  36 +QString CCacic::getValueFromTags(QString fullString, QString tag, QString tagType) {
  37 + QString tagFim = tagType.mid(0,1) + "/" + tag + tagType.mid(1);
  38 + int tagSize;
  39 + tag = tagType.mid(0,1) + tag + tagType.mid(1);
  40 + tagSize = tag.size();
  41 + return fullString.mid(fullString.indexOf(tag) + tagSize,
  42 + fullString.indexOf(tagFim) - (fullString.indexOf(tag) + tagSize)).trimmed();
  43 +}
  44 +
7 /*Getters/Setters 45 /*Getters/Setters
8 * Begin: 46 * Begin:
9 */ 47 */
1 #ifndef CCACIC_H 1 #ifndef CCACIC_H
2 #define CCACIC_H 2 #define CCACIC_H
3 3
  4 +#include <QCoreApplication>
4 #include <QObject> 5 #include <QObject>
  6 +#include <QDebug>
  7 +#include <QFile>
  8 +#include <iostream>
5 9
6 class CCacic : public QObject 10 class CCacic : public QObject
7 { 11 {
8 Q_OBJECT 12 Q_OBJECT
9 public: 13 public:
10 explicit CCacic(QObject *parent = 0); 14 explicit CCacic(QObject *parent = 0);
11 - QString getValueFromFile();  
12 - QString getValueFromTags(); 15 + QString getValueFromFile(QString sectionName, QString keyName, QString filePath);
  16 + QString getValueFromTags(QString fullString, QString tag, QString tagType = "[]");
13 17
14 //Geters/seters: 18 //Geters/seters:
15 19