author | llornkcor <llornkcor> | 2004-12-27 23:41:09 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2004-12-27 23:41:09 (UTC) |
commit | 02395040e422485ceb2fa435a3fb30879d5f3df2 (patch) (unidiff) | |
tree | 0052da61e1a209bddf2da6f130210d569610de9f /noncore/apps/opie-bartender/drinkdata.cpp | |
parent | b2b6055b9180214150cdbbff72b79b35db379fb2 (diff) | |
download | opie-02395040e422485ceb2fa435a3fb30879d5f3df2.zip opie-02395040e422485ceb2fa435a3fb30879d5f3df2.tar.gz opie-02395040e422485ceb2fa435a3fb30879d5f3df2.tar.bz2 |
new code from Paul Eggleton
Diffstat (limited to 'noncore/apps/opie-bartender/drinkdata.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-bartender/drinkdata.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/noncore/apps/opie-bartender/drinkdata.cpp b/noncore/apps/opie-bartender/drinkdata.cpp new file mode 100644 index 0000000..8856e46 --- a/dev/null +++ b/noncore/apps/opie-bartender/drinkdata.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | /**************************************************************************** | ||
2 | ** Created: Sun Dec 26 22:00:00 2004 | ||
3 | ** by: Paul Eggleton <bluelightning@bluelightning.org> | ||
4 | ** copyright : (C) 2004 by Paul Eggleton | ||
5 | email : bluelightning@bluelightning.org | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /* QT */ | ||
13 | #include <qfile.h> | ||
14 | #include <qtextstream.h> | ||
15 | #include <qvaluelist.h> | ||
16 | |||
17 | /* STD */ | ||
18 | #include <fcntl.h> | ||
19 | #include <unistd.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <iostream> | ||
22 | #include <errno.h> | ||
23 | |||
24 | #include "drinkdata.h" | ||
25 | |||
26 | // ----- DrinkData ----- | ||
27 | |||
28 | DrinkData::DrinkData() { | ||
29 | } | ||
30 | |||
31 | void DrinkData::setFile(const QString &filename) { | ||
32 | this->filename = filename; | ||
33 | } | ||
34 | |||
35 | bool DrinkData::read(void) { | ||
36 | QFile dbFile; | ||
37 | |||
38 | dbFile.setName(filename); | ||
39 | if ( !dbFile.open( IO_ReadOnly)) { | ||
40 | return false; | ||
41 | } | ||
42 | |||
43 | items.clear(); | ||
44 | |||
45 | QTextStream t( &dbFile); | ||
46 | QString s, name = "", ingredients = ""; | ||
47 | |||
48 | while ( !t.eof()) { | ||
49 | s = t.readLine(); | ||
50 | if(s.find( "#", 0, TRUE) != -1 || dbFile.atEnd()) { | ||
51 | if(name != "") | ||
52 | addDrink(name, ingredients); | ||
53 | // Start new entry | ||
54 | name = s.right(s.length()-2); | ||
55 | ingredients = ""; | ||
56 | } | ||
57 | else { | ||
58 | if(ingredients != "") | ||
59 | ingredients += '\n'; | ||
60 | ingredients += s; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | dbFile.close(); | ||
65 | |||
66 | return true; | ||
67 | } | ||
68 | |||
69 | void DrinkData::print(void) { | ||
70 | DrinkList::Iterator it = items.begin(); | ||
71 | while ( it != items.end() ) { | ||
72 | std::cout << (*it).getName() << '\n'; | ||
73 | ++it; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | void DrinkData::addDrink(const QString &name, const QString &ingredients) { | ||
78 | DrinkItem item(name, ingredients); | ||
79 | items.append(item); | ||
80 | } | ||
81 | |||
82 | DrinkList::Iterator DrinkData::findDrink(const QString &name) { | ||
83 | DrinkList::Iterator it = items.begin(); | ||
84 | while ( it != items.end() ) { | ||
85 | if((*it).getName() == name) | ||
86 | return it; | ||
87 | ++it; | ||
88 | } | ||
89 | return items.end(); | ||
90 | } | ||
91 | |||
92 | DrinkList::Iterator DrinkData::getBegin(void) { | ||
93 | return items.begin(); | ||
94 | } | ||
95 | |||
96 | DrinkList::Iterator DrinkData::getEnd(void) { | ||
97 | return items.end(); | ||
98 | } | ||
99 | |||
100 | bool DrinkData::writeChanges(void) { | ||
101 | QFile dbFile; | ||
102 | |||
103 | dbFile.setName(filename); | ||
104 | if ( !dbFile.open( IO_WriteOnly | IO_Truncate )) { | ||
105 | return false; | ||
106 | } | ||
107 | |||
108 | QTextStream t( &dbFile); | ||
109 | |||
110 | DrinkList::Iterator it = items.begin(); | ||
111 | while ( it != items.end() ) { | ||
112 | t << "# " << (*it).getName() << '\n'; | ||
113 | t << (*it).getIngredients() << '\n'; | ||
114 | ++it; | ||
115 | } | ||
116 | |||
117 | dbFile.close(); | ||
118 | |||
119 | return true; | ||
120 | } | ||
121 | |||
122 | |||
123 | // ----- DrinkItem ----- | ||
124 | |||
125 | DrinkItem::DrinkItem(void) { | ||
126 | } | ||
127 | |||
128 | DrinkItem::DrinkItem(const QString &name, const QString &ingredients) { | ||
129 | this->name = name; | ||
130 | this->ingredients = ingredients; | ||
131 | } | ||
132 | |||
133 | const QString &DrinkItem::getName(void) { | ||
134 | return name; | ||
135 | } | ||
136 | |||
137 | const QString &DrinkItem::getIngredients(void) { | ||
138 | return ingredients; | ||
139 | } | ||
140 | |||
141 | void DrinkItem::setName(const QString &name) { | ||
142 | this->name = name; | ||
143 | } | ||
144 | |||
145 | void DrinkItem::setIngredients(const QString &ingredients) { | ||
146 | this->ingredients = ingredients; | ||
147 | } | ||