//%///////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //============================================================================== // // Author: Mike Brasher (mbrasher@bmc.com) // // Modified By: // //%///////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "AssocTable.h" PEGASUS_USING_STD; PEGASUS_NAMESPACE_BEGIN #define ASSOC_INSTANCE_NAME_INDEX 0 #define ASSOC_CLASS_NAME_INDEX 1 #define FROM_OBJECT_NAME_INDEX 2 #define FROM_CLASS_NAME_INDEX 3 #define FROM_PROPERTY_NAME_INDEX 4 #define TO_OBJECT_NAME_INDEX 5 #define TO_CLASS_NAME_INDEX 6 #define TO_PROPERTY_NAME_INDEX 7 #define NUM_FIELDS 8 static inline Boolean _MatchNoCase(const String& x, const String& pattern) { return pattern.size() == 0 || String::equalNoCase(x, pattern); } static String _Escape(const String& str) { String result; for (Uint32 i = 0, n = str.size(); i < n; i++) { Char16 c = str[i]; switch (c) { case '\n': result += "\\n"; break; case '\r': result += "\\r"; break; case '\t': result += "\\t"; break; case '\f': result += "\\f"; break; case '\\': result += "\\\\"; break; default: result += c; } } return result; } static String _Unescape(const String& str) { String result; for (Uint32 i = 0, n = str.size(); i < n; i++) { Char16 c = str[i]; if (c == '\\') { if (i + 1 == n) break; c = str[i + 1]; switch (c) { case 'n': result += "\n"; break; case 'r': result += "\r"; break; case 't': result += "\t"; break; case 'f': result += "\f"; break; default: result += c; } i++; } else result += c; } return result; } static Boolean _GetRecord(ifstream& is, Array& fields) { fields.clear(); String line; for (Uint32 i = 0; i < NUM_FIELDS; i++) { if (!GetLine(is, line)) return false; fields.append(_Unescape(line)); } // Skip the blank line: if (!GetLine(is, line)) return false; return true; } static void _PutRecord(ofstream& os, Array& fields) { for (Uint32 i = 0, n = fields.size(); i < n; i++) os << _Escape(fields[i]) << endl; os << endl; } void AssocTable::append( PEGASUS_STD(ofstream)& os, const String& assocInstanceName, const String& assocClassName, const String& fromObjectName, const String& fromClassName, const String& fromPropertyName, const String& toObjectName, const String& toClassName, const String& toPropertyName) { Array fields; fields.reserve(8); fields.append(assocInstanceName); fields.append(assocClassName); fields.append(fromObjectName); fields.append(fromClassName); fields.append(fromPropertyName); fields.append(toObjectName); fields.append(toClassName); fields.append(toPropertyName); _PutRecord(os, fields); } void AssocTable::append( const String& path, const String& assocInstanceName, const String& assocClassName, const String& fromObjectName, const String& fromClassName, const String& fromPropertyName, const String& toObjectName, const String& toClassName, const String& toPropertyName) { // Open input file: ofstream os; if (!OpenAppend(os, path)) throw CannotOpenFile(path); // Insert the entry: Array fields; fields.reserve(8); fields.append(assocInstanceName); fields.append(assocClassName); fields.append(fromObjectName); fields.append(fromClassName); fields.append(fromPropertyName); fields.append(toObjectName); fields.append(toClassName); fields.append(toPropertyName); _PutRecord(os, fields); } Boolean AssocTable::containsObject( const String& path, const CIMReference& objectName) { // Open input file: ifstream is; if (!Open(is, path)) throw CannotOpenFile(path); // Look at each line: Array fields; while (_GetRecord(is, fields)) { if (fields[TO_OBJECT_NAME_INDEX] == objectName) return true; } return false; } Boolean AssocTable::deleteAssociation( const String& path, const CIMReference& assocInstanceName) { // Open input file: ifstream is; if (!Open(is, path)) throw CannotOpenFile(path); // Open output file: String tmpPath = path + ".tmp"; ofstream os; if (!Open(os, tmpPath)) throw CannotOpenFile(tmpPath); // Copy over all lines except ones with the given association instance name: Array fields; Boolean found = false; while (_GetRecord(is, fields)) { if (assocInstanceName != fields[ASSOC_INSTANCE_NAME_INDEX]) { _PutRecord(os, fields); found = true; } } // Close both files: is.close(); os.close(); // Remove orginal file: if (!FileSystem::removeFile(path)) throw CannotRemoveFile(path); // Rename back to original name: if (!FileSystem::renameFile(tmpPath, path)) throw CannotRenameFile(path); return found; } Boolean AssocTable::getAssociatorNames( const String& path, const CIMReference& objectName, const String& assocClass, const String& resultClass, const String& role, const String& resultRole, Array& associatorNames) { // Open input file: ifstream is; if (!Open(is, path)) throw CannotOpenFile(path); // For each line: Array fields; Boolean found = false; while (_GetRecord(is, fields)) { if (objectName == fields[FROM_OBJECT_NAME_INDEX] && _MatchNoCase(fields[ASSOC_CLASS_NAME_INDEX], assocClass) && _MatchNoCase(fields[TO_CLASS_NAME_INDEX], resultClass) && _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role) && _MatchNoCase(fields[TO_PROPERTY_NAME_INDEX], resultRole)) { associatorNames.append(fields[TO_OBJECT_NAME_INDEX]); found = true; } } return found; } Boolean AssocTable::getReferenceNames( const String& path, const CIMReference& objectName, const String& resultClass, const String& role, Array& referenceNames) { // Open input file: ifstream is; if (!Open(is, path)) throw CannotOpenFile(path); // For each line: Array fields; Boolean found = false; while (_GetRecord(is, fields)) { if (objectName == fields[FROM_OBJECT_NAME_INDEX] && _MatchNoCase(fields[ASSOC_CLASS_NAME_INDEX], resultClass) && _MatchNoCase(fields[FROM_PROPERTY_NAME_INDEX], role)) { referenceNames.append(fields[ASSOC_INSTANCE_NAME_INDEX]); found = true; } } // Get rid of duplicates: BubbleSort(referenceNames); Unique(referenceNames); return found; } PEGASUS_NAMESPACE_END