(file) Return to AssocTable.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Repository

  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           static inline Boolean _Match(const String& x, const String& pattern)
 50           {
 51               return pattern.size() == 0 || x == pattern;
 52           }
 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           	}
129           	else
130           	    result += c;
131               }
132           
133               return result;
134           }
135           
136           static Boolean _GetRecord(ifstream& is, Array<String>& fields)
137           {
138               fields.clear();
139 mike  1.1     String line;
140           
141 mike  1.3     for (Uint32 i = 0; i < NUM_FIELDS; i++)
142               {
143           	if (!GetLine(is, line))
144           	    return false;
145           
146           	fields.append(_Unescape(line));
147               }
148           
149               // Skip the blank line:
150           
151 mike  1.1     if (!GetLine(is, line))
152           	return false;
153           
154 mike  1.3     return true;
155           }
156 mike  1.1 
157 mike  1.3 static void _PutRecord(ofstream& os, Array<String>& fields)
158           {
159               for (Uint32 i = 0, n = fields.size(); i < n; i++)
160           	os << _Escape(fields[i]) << endl;
161               os << endl;
162 mike  1.1 }
163           
164 mike  1.3 void AssocTable::append(
165               PEGASUS_STD(ofstream)& os,
166               const String& assocInstanceName,
167               const String& assocClassName,
168               const String& fromObjectName,
169               const String& fromClassName,
170               const String& fromPropertyName,
171               const String& toObjectName,
172               const String& toClassName,
173               const String& toPropertyName)
174 mike  1.1 {
175 mike  1.3     Array<String> fields;
176               fields.reserve(8);
177               fields.append(assocInstanceName);
178               fields.append(assocClassName);
179               fields.append(fromObjectName);
180               fields.append(fromClassName);
181               fields.append(fromPropertyName);
182               fields.append(toObjectName);
183               fields.append(toClassName);
184               fields.append(toPropertyName);
185 mike  1.1 
186 mike  1.3     _PutRecord(os, fields);
187 mike  1.1 }
188           
189           void AssocTable::append(
190               const String& path,
191               const String& assocInstanceName,
192               const String& assocClassName,
193               const String& fromObjectName,
194               const String& fromClassName,
195               const String& fromPropertyName,
196               const String& toObjectName,
197               const String& toClassName,
198               const String& toPropertyName)
199           {
200               // Open input file:
201               
202               ofstream os;
203           
204               if (!OpenAppend(os, path))
205           	throw CannotOpenFile(path);
206           
207               // Insert the entry:
208 mike  1.1 
209               Array<String> fields;
210               fields.reserve(8);
211               fields.append(assocInstanceName);
212               fields.append(assocClassName);
213               fields.append(fromObjectName);
214               fields.append(fromClassName);
215               fields.append(fromPropertyName);
216               fields.append(toObjectName);
217               fields.append(toClassName);
218               fields.append(toPropertyName);
219           
220 mike  1.3     _PutRecord(os, fields);
221 mike  1.1 }
222           
223           Boolean AssocTable::containsObject(
224               const String& path,
225               const String& objectName)
226           {
227               // Open input file:
228           
229               ifstream is;
230           
231               if (!Open(is, path))
232           	throw CannotOpenFile(path);
233           
234               // Look at each line:
235           
236               Array<String> fields;
237           
238 mike  1.3     while (_GetRecord(is, fields))
239 mike  1.1     {
240           	if (fields[TO_OBJECT_NAME_INDEX] == objectName)
241           	    return true;
242               }
243           
244               return false;
245           }
246           
247           Boolean AssocTable::deleteAssociation(
248               const String& path,
249               const String& assocInstanceName)
250           {
251               // Open input file:
252           
253               ifstream is;
254           
255               if (!Open(is, path))
256           	throw CannotOpenFile(path);
257           
258               // Open output file:
259           
260 mike  1.1     String tmpPath = path + ".tmp";
261               ofstream os;
262           
263               if (!Open(os, tmpPath))
264           	throw CannotOpenFile(tmpPath);
265           
266               // Copy over all lines except ones with the given association instance name:
267           
268               Array<String> fields;
269               Boolean found = false;
270           
271 mike  1.3     while (_GetRecord(is, fields))
272 mike  1.1     {
273           	if (assocInstanceName != fields[ASSOC_INSTANCE_NAME_INDEX])
274           	{
275 mike  1.3 	    _PutRecord(os, fields);
276 mike  1.1 	    found = true;
277           	}
278               }
279           
280               // Close both files:
281           
282               is.close();
283               os.close();
284           
285               // Remove orginal file:
286           
287               if (!FileSystem::removeFile(path))
288           	throw CannotRemoveFile(path);
289           
290               // Rename back to original name:
291           
292               if (!FileSystem::renameFile(tmpPath, path))
293           	throw CannotRenameFile(path);
294           
295               return found;
296           }
297 mike  1.1 
298           Boolean AssocTable::getAssociatorNames(
299               const String& path,
300               const String& objectName,
301               const String& assocClass,
302               const String& resultClass,
303               const String& role,
304               const String& resultRole,
305               Array<String>& associatorNames)
306           {
307               // Open input file:
308               
309               ifstream is;
310           
311               if (!Open(is, path))
312           	throw CannotOpenFile(path);
313           
314               // For each line:
315           
316               Array<String> fields;
317               Boolean found = false;
318 mike  1.1 
319 mike  1.3     while (_GetRecord(is, fields))
320 mike  1.1     {
321           	if (objectName == fields[FROM_OBJECT_NAME_INDEX] &&
322           	    _Match(fields[ASSOC_CLASS_NAME_INDEX], assocClass) &&
323           	    _Match(fields[TO_CLASS_NAME_INDEX], resultRole) &&
324           	    _Match(fields[FROM_PROPERTY_NAME_INDEX], role) &&
325           	    _Match(fields[TO_PROPERTY_NAME_INDEX], resultRole))
326           	{
327           	    associatorNames.append(fields[TO_OBJECT_NAME_INDEX]);
328           	    found = true;
329           	}
330               }
331           
332               return found;
333           }
334           
335           Boolean AssocTable::getReferenceNames(
336               const String& path,
337               const String& objectName,
338               const String& resultClass,
339               const String& role,
340               Array<String>& referenceNames)
341 mike  1.1 {
342               // Open input file:
343               
344               ifstream is;
345           
346               if (!Open(is, path))
347           	throw CannotOpenFile(path);
348           
349               // For each line:
350           
351               Array<String> fields;
352               Boolean found = false;
353           
354 mike  1.3     while (_GetRecord(is, fields))
355 mike  1.1     {
356           	if (objectName == fields[FROM_OBJECT_NAME_INDEX] &&
357           	    _Match(fields[ASSOC_CLASS_NAME_INDEX], resultClass) &&
358           	    _Match(fields[FROM_PROPERTY_NAME_INDEX], role))
359           	{
360           	    referenceNames.append(fields[ASSOC_INSTANCE_NAME_INDEX]);
361           	    found = true;
362           	}
363               }
364           
365               // Get rid of duplicates:
366           
367               BubbleSort(referenceNames);
368               Unique(referenceNames);
369           
370               return found;
371           }
372           
373           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2