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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2