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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2