redesigned battery data structures and added icon merge
[qbat.git] / common.cpp
blob626b2b7062ec125682f2e9e20b8feeb0da07285c
1 //
2 // C++ Implementation: common
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include <cstdlib>
9 // #include <QString>
10 #include "common.h"
12 namespace qbat {
13 using namespace std;
15 bool readStringFromFile(char * buffer, const char * fileName) {
16 FILE *f = NULL;
17 f = fopen(fileName, "r");
18 if (f) {
19 fgets(buffer, BUF_SIZE, f);
20 fclose(f);
22 bool fill = false;
23 for (int i = 0; i < BUF_SIZE; i++) {
24 if (buffer[i] == '\n')
25 fill = true;
26 if (fill)
27 buffer[i] = '\0';
30 return true;
32 else
33 return false;
36 QString readStringSysFile(const char * fileName) {
37 FILE *f = NULL;
38 char buf[BUF_SIZE];
39 f = fopen(fileName, "r");
40 if (f) {
41 fgets(buf, BUF_SIZE, f);
42 fclose(f);
43 return QString(buf);
45 else
46 return QString();
48 // QString buffer;
49 /* ifstream inFile;
50 string buffer;
52 inFile.open(fileName);
53 inFile >> buffer;
54 inFile.close();*/
58 int readIntSysFile(const char * fileName) {
59 char buffer[BUF_SIZE];
60 readStringFromFile(buffer, fileName);
61 return atoi(buffer);
63 /* ifstream inFile;
64 int buffer;
66 inFile.open(fileName);
67 inFile >> buffer;
68 inFile.close();
70 return buffer;*/
73 int toStatusInt(/*std::string*/ QString status) {
74 if (status == "Discharging")
75 return UI_BATTERY_DISCHARGING;
76 else if (status == "Charging")
77 return UI_BATTERY_CHARGING;
78 else if (status == "Full")
79 return UI_BATTERY_FULL;
80 else
81 return 0;
84 int toStatusInt(const char * status) {
85 if (strcmp(status, "Discharging") == 0)
86 return UI_BATTERY_DISCHARGING;
87 else if (strcmp(status, "Charging") == 0)
88 return UI_BATTERY_CHARGING;
89 else if (strcmp(status, "Full") == 0)
90 return UI_BATTERY_FULL;
91 else
92 return 0;