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