1 mike 1.1 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 mike 1.3 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
|
4 mike 1.1 //
|
5 mike 1.3 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
14 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20 mike 1.1 //
21 //==============================================================================
22 //
23 // Author: Mike Brasher (mbrasher@bmc.com)
24 //
25 // Modified By:
26 //
27 //%/////////////////////////////////////////////////////////////////////////////
28
29 #include <fstream>
30 #include <Pegasus/Common/Exception.h>
31 #include <Pegasus/Common/FileSystem.h>
32 #include <Pegasus/Common/Exception.h>
33 #include "AssocTable.h"
34
35 PEGASUS_USING_STD;
36
37 PEGASUS_NAMESPACE_BEGIN
38
39 #define ASSOC_INSTANCE_NAME_INDEX 0
40 #define ASSOC_CLASS_NAME_INDEX 1
41 mike 1.1 #define FROM_OBJECT_NAME_INDEX 2
42 #define FROM_CLASS_NAME_INDEX 3
43 #define FROM_PROPERTY_NAME_INDEX 4
44 #define TO_OBJECT_NAME_INDEX 5
45 #define TO_CLASS_NAME_INDEX 6
46 #define TO_PROPERTY_NAME_INDEX 7
|
47 mike 1.3 #define NUM_FIELDS 8
|
48 mike 1.1
|
49 mike 1.4 static inline Boolean _MatchNoCase(const String& x, const String& pattern)
|
50 mike 1.1 {
|
51 mike 1.4 return pattern.size() == 0 || String::equalNoCase(x, pattern);
|
52 mike 1.1 }
53
|
54 mike 1.3 static String _Escape(const String& str)
55 {
56 String result;
57
58 for (Uint32 i = 0, n = str.size(); i < n; i++)
59 {
60 Char16 c = str[i];
61
62 switch (c)
63 {
64 case '\n':
65 result += "\\n";
66 break;
67
68 case '\r':
69 result += "\\r";
70 break;
71
72 case '\t':
73 result += "\\t";
74 break;
75 mike 1.3
76 case '\f':
77 result += "\\f";
78 break;
79
80 case '\\':
81 result += "\\\\";
82 break;
83
84 default:
85 result += c;
86 }
87 }
88
89 return result;
90 }
91
92 static String _Unescape(const String& str)
|
93 mike 1.1 {
|
94 mike 1.3 String result;
95
96 for (Uint32 i = 0, n = str.size(); i < n; i++)
97 {
98 Char16 c = str[i];
|
99 mike 1.1
|
100 mike 1.3 if (c == '\\')
101 {
102 if (i + 1 == n)
103 break;
104
105 c = str[i + 1];
106
107 switch (c)
108 {
109 case 'n':
110 result += "\n";
111 break;
112
113 case 'r':
114 result += "\r";
115 break;
116
117 case 't':
118 result += "\t";
119 break;
120
121 mike 1.3 case 'f':
122 result += "\f";
123 break;
124
125 default:
126 result += c;
127 }
|
128 mike 1.5
129 i++;
|
130 mike 1.3 }
131 else
132 result += c;
133 }
134
135 return result;
136 }
137
138 static Boolean _GetRecord(ifstream& is, Array<String>& fields)
139 {
140 fields.clear();
|
141 mike 1.1 String line;
142
|
143 mike 1.3 for (Uint32 i = 0; i < NUM_FIELDS; i++)
144 {
145 if (!GetLine(is, line))
146 return false;
147
148 fields.append(_Unescape(line));
149 }
150
151 // Skip the blank line:
152
|
153 mike 1.1 if (!GetLine(is, line))
154 return false;
155
|
156 mike 1.3 return true;
157 }
|
158 mike 1.1
|
159 mike 1.3 static void _PutRecord(ofstream& os, Array<String>& fields)
160 {
161 for (Uint32 i = 0, n = fields.size(); i < n; i++)
162 os << _Escape(fields[i]) << endl;
163 os << endl;
|
164 mike 1.1 }
165
|
166 mike 1.3 void AssocTable::append(
167 PEGASUS_STD(ofstream)& os,
168 const String& assocInstanceName,
169 const String& assocClassName,
170 const String& fromObjectName,
171 const String& fromClassName,
172 const String& fromPropertyName,
173 const String& toObjectName,
174 const String& toClassName,
175 const String& toPropertyName)
|
176 mike 1.1 {
|
177 mike 1.3 Array<String> fields;
178 fields.reserve(8);
179 fields.append(assocInstanceName);
180 fields.append(assocClassName);
181 fields.append(fromObjectName);
182 fields.append(fromClassName);
183 fields.append(fromPropertyName);
184 fields.append(toObjectName);
185 fields.append(toClassName);
186 fields.append(toPropertyName);
|
187 mike 1.1
|
188 mike 1.3 _PutRecord(os, fields);
|
189 mike 1.1 }
190
191 void AssocTable::append(
192 const String& path,
193 const String& assocInstanceName,
194 const String& assocClassName,
195 const String& fromObjectName,
196 const String& fromClassName,
197 const String& fromPropertyName,
198 const String& toObjectName,
199 const String& toClassName,
200 const String& toPropertyName)
201 {
202 // Open input file:
203
204 ofstream os;
205
206 if (!OpenAppend(os, path))
207 throw CannotOpenFile(path);
208
209 // Insert the entry:
210 mike 1.1
211 Array<String> fields;
212 fields.reserve(8);
213 fields.append(assocInstanceName);
214 fields.append(assocClassName);
215 fields.append(fromObjectName);
216 fields.append(fromClassName);
217 fields.append(fromPropertyName);
218 fields.append(toObjectName);
219 fields.append(toClassName);
220 fields.append(toPropertyName);
221
|
222 mike 1.3 _PutRecord(os, fields);
|
223 mike 1.1 }
224
225 Boolean AssocTable::containsObject(
226 const String& path,
|
227 mike 1.4 const CIMReference& objectName)
|
228 mike 1.1 {
229 // Open input file:
230
231 ifstream is;
232
233 if (!Open(is, path))
|
234 mike 1.7 return false;
|
235 mike 1.1
236 // Look at each line:
237
238 Array<String> fields;
239
|
240 mike 1.3 while (_GetRecord(is, fields))
|
241 mike 1.1 {
242 if (fields[TO_OBJECT_NAME_INDEX] == objectName)
243 return true;
244 }
245
246 return false;
247 }
248
249 Boolean AssocTable::deleteAssociation(
250 const String& path,
|
251 mike 1.6 const CIMReference& assocInstanceName)
|
252 mike 1.1 {
253 // Open input file:
254
255 ifstream is;
256
257 if (!Open(is, path))
|
258 mike 1.7 return false;
|
259 mike 1.1
260 // Open output file:
261
262 String tmpPath = path + ".tmp";
263 ofstream os;
264
265 if (!Open(os, tmpPath))
266 throw CannotOpenFile(tmpPath);
267
268 // Copy over all lines except ones with the given association instance name:
269
270 Array<String> fields;
271 Boolean found = false;
272
|
273 mike 1.3 while (_GetRecord(is, fields))
|
274 mike 1.1 {
275 if (assocInstanceName != fields[ASSOC_INSTANCE_NAME_INDEX])
276 {
|
277 mike 1.3 _PutRecord(os, fields);
|
278 mike 1.1 found = true;
279 }
280 }
281
282 // Close both files:
283
284 is.close();
285 os.close();
286
287 // Remove orginal file:
288
289 if (!FileSystem::removeFile(path))
290 throw CannotRemoveFile(path);
291
292 // Rename back to original name:
293
294 if (!FileSystem::renameFile(tmpPath, path))
295 throw CannotRenameFile(path);
296
297 return found;
298 }
299 mike 1.1
300 Boolean AssocTable::getAssociatorNames(
301 const String& path,
|
302 mike 1.4 const CIMReference& objectName,
|
303 mike 1.1 const String& assocClass,
304 const String& resultClass,
305 const String& role,
306 const String& resultRole,
307 Array<String>& associatorNames)
308 {
309 // Open input file:
310
311 ifstream is;
312
313 if (!Open(is, path))
|
314 mike 1.7 return false;
|
315 mike 1.1
316 // For each line:
317
318 Array<String> fields;
319 Boolean found = false;
320
|
321 mike 1.3 while (_GetRecord(is, fields))
|
322 mike 1.1 {
323 if (objectName == fields[FROM_OBJECT_NAME_INDEX] &&
|
324 mike 1.4 _MatchNoCase(fields[ASSOC_CLASS_NAME_INDEX], assocClass) &&
325 _MatchNoCase(fields[TO_CLASS_NAME_INDEX], resultClass) &&
326 _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role) &&
327 _MatchNoCase(fields[TO_PROPERTY_NAME_INDEX], resultRole))
|
328 mike 1.1 {
329 associatorNames.append(fields[TO_OBJECT_NAME_INDEX]);
330 found = true;
331 }
332 }
333
334 return found;
335 }
336
337 Boolean AssocTable::getReferenceNames(
338 const String& path,
|
339 mike 1.4 const CIMReference& objectName,
|
340 mike 1.1 const String& resultClass,
341 const String& role,
342 Array<String>& referenceNames)
343 {
344 // Open input file:
345
346 ifstream is;
347
348 if (!Open(is, path))
|
349 mike 1.7 return false;
|
350 mike 1.1
351 // For each line:
352
353 Array<String> fields;
354 Boolean found = false;
355
|
356 mike 1.3 while (_GetRecord(is, fields))
|
357 mike 1.1 {
358 if (objectName == fields[FROM_OBJECT_NAME_INDEX] &&
|
359 mike 1.4 _MatchNoCase(fields[ASSOC_CLASS_NAME_INDEX], resultClass) &&
360 _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role))
|
361 mike 1.1 {
362 referenceNames.append(fields[ASSOC_INSTANCE_NAME_INDEX]);
363 found = true;
364 }
365 }
366
367 // Get rid of duplicates:
368
369 BubbleSort(referenceNames);
370 Unique(referenceNames);
371
372 return found;
373 }
374
375 PEGASUS_NAMESPACE_END
|