cdlcompilertoolkit/src/CdlTkWriteCommonDefs.cpp
changeset 0 f58d6ec98e88
child 1 b700e12870ca
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #pragma warning (disable:4786)	// disable "identifier was truncated to '255' characters in the browser information" warning
       
    18 #include "CdlCompilerToolkit/CdlTkProcess.h"
       
    19 #include "CdlTkPriv.h"
       
    20 #include <fstream>
       
    21 #include <iomanip>
       
    22 #include <iostream>
       
    23 using namespace std;
       
    24 
       
    25 namespace CdlCompilerToolkit {
       
    26 
       
    27 void CCdlTkWriteCommonDefs::ExportCommonDefs(const CCdlTkInterface& aCdl, const std::string& aFileName)
       
    28 	{
       
    29 	CCdlTkFileCleanup tempFile;
       
    30 	ofstream out;
       
    31 
       
    32 	CdlTkUtil::OpenTempOutput(out, tempFile);
       
    33 	CCdlTkWriteCommonDefs commonDefs(aCdl, out, aFileName);
       
    34 	commonDefs.ProcessHRH(out);
       
    35 	out.close();
       
    36 	CdlTkUtil::ExportFileIfWritable(tempFile, CdlTkUtil::ToLower(aFileName+"rh"));
       
    37 
       
    38 	CdlTkUtil::OpenTempOutput(out, tempFile);
       
    39 	commonDefs.Process();
       
    40 	out.close();
       
    41 	CdlTkUtil::ExportFileIfWritable(tempFile, CdlTkUtil::ToLower(aFileName));
       
    42 	}
       
    43 
       
    44 CCdlTkWriteCommonDefs::CCdlTkWriteCommonDefs(const CCdlTkInterface& aCdl, ofstream& aStream, const string& aFileName)
       
    45 : iCdl(aCdl), iStream(&aStream), iFileName(aFileName)
       
    46 	{
       
    47 	}
       
    48 
       
    49 const string KCommonDefsHeader = "\
       
    50 /*\n\
       
    51 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).\n\
       
    52 * All rights reserved.\n\
       
    53 * This component and the accompanying materials are made available\n\
       
    54 * under the terms of  \"Eclipse Public License v1.0\"\n\
       
    55 * which accompanies this distribution, and is available\n\
       
    56 * at the URL \"http://www.eclipse.org/legal/epl-v10.html\".\n\
       
    57 *\n\
       
    58 * Initial Contributors:\n\
       
    59 * Nokia Corporation - initial contribution.\n\
       
    60 *\n\
       
    61 * Contributors:\n\
       
    62 *\n\
       
    63 * Description:\n\
       
    64 *\n\
       
    65 */\n\n\
       
    66 // It contains common definitions for the client and customisation API.\n\
       
    67 // It should not be modified manually.\n\
       
    68 \n";
       
    69 
       
    70 const string KHrhContents ="\
       
    71 // These constants are defined by the CDL interface: $NAME\n\
       
    72 #define _CDL_$NSPACE_KCdlInterfaceUidValue $UID\n\
       
    73 \n";
       
    74 
       
    75 void CCdlTkWriteCommonDefs::ProcessHRH(ofstream& aStream)
       
    76 	{
       
    77 	iStream = &aStream;
       
    78 	string fileName = iFileName + "rh";
       
    79 
       
    80 	string cdlFileName(CdlTkUtil::ToLower(iCdl.FileName()));
       
    81 //	Stream() << CdlTkUtil::Replace("$FILE", cdlFileName, KCommonDefsHeader);
       
    82 	Stream() << KCommonDefsHeader;
       
    83 	WriteHeaderGuardStart(fileName, Stream());
       
    84 
       
    85 	const CCdlTkInterfaceHeader& header = iCdl.Header();
       
    86 	CdlTkUtil::CReplaceSet headerSet;
       
    87 	headerSet.Add("$NAME", header.Name());
       
    88 	headerSet.Add("$UID", CdlTkUtil::IntToHexString(header.Uid()));
       
    89 	headerSet.Add("$NSPACE", iCdl.NamespaceName());
       
    90 	Stream() << CdlTkUtil::MultiReplace(headerSet, KHrhContents);
       
    91 
       
    92 	WriteHeaderGuardEnd(fileName, Stream());
       
    93 	}
       
    94 
       
    95 const string KInterfaceConstants ="\
       
    96 #include \"$THISFILErh\"\n\
       
    97 \n\
       
    98 // These constants are defined by the CDL interface: $NAME\n\
       
    99 _LIT(KCdlName, \"$NAME\");\n\
       
   100 const TInt KCdlInterfaceUidValue = _CDL_$NSPACE_KCdlInterfaceUidValue;\n\
       
   101 const TUid KCdlInterfaceUid = { KCdlInterfaceUidValue };\n\
       
   102 const TInt KCdlInterfaceMajorVersion = $MAJOR;\n\
       
   103 const TInt KCdlInterfaceMinorVersion = $MINOR;\n\
       
   104 const TUint KCdlInterfaceFlags = $FLAGS;\n\
       
   105 \n\
       
   106 // These are the API ids used in this interface.\n\
       
   107 enum TApiId\n\
       
   108 \t{\n";
       
   109 
       
   110 const string KCdlInterface = "\
       
   111 \tE_TApiId_TableSize\n\
       
   112 \t};\n\
       
   113 \n\
       
   114 // This is the CDL interface definition for this interface\n\
       
   115 const SCdlInterface KCdlInterface =\n\
       
   116 \t{\n\
       
   117 \tKCdlCompilerMajorVersion,\n\
       
   118 \tKCdlCompilerMinorVersion,\n\
       
   119 \tLIT_AS_DESC_PTR(KCdlName),\n\
       
   120 \t{ KCdlInterfaceUidValue },\n\
       
   121 \tKCdlInterfaceMajorVersion,\n\
       
   122 \tKCdlInterfaceMinorVersion,\n\
       
   123 \tKCdlInterfaceFlags,\n\
       
   124 \tE_TApiId_TableSize\n\
       
   125 \t};\n\
       
   126 \n\
       
   127 // These typedefs define the types for function APIs\n";
       
   128 
       
   129 void CCdlTkWriteCommonDefs::Process()
       
   130 	{
       
   131 	string cdlFileName(CdlTkUtil::ToLower(iCdl.FileName()));
       
   132 //	Stream() << CdlTkUtil::Replace("$FILE", cdlFileName, KCommonDefsHeader);
       
   133 	Stream() << KCommonDefsHeader;
       
   134 	WriteHeaderGuardStart(iFileName, Stream());
       
   135 	const CCdlTkCpp& cpp = iCdl.Cpp();
       
   136 	for (CCdlTkCpp::const_iterator pCpp = cpp.begin(); pCpp != cpp.end(); ++pCpp)
       
   137 		Stream() << *pCpp << endl;
       
   138 	Stream() << endl;
       
   139 	WriteNamespaceStart(iCdl, Stream());
       
   140 	const CCdlTkInterfaceHeader& header = iCdl.Header();
       
   141 	CdlTkUtil::CReplaceSet headerSet;
       
   142 	headerSet.Add("$NAME", header.Name());
       
   143 	headerSet.Add("$NSPACE", iCdl.NamespaceName());
       
   144 	headerSet.Add("$THISFILE", CdlTkUtil::ToLower(CdlTkUtil::StripPath(iFileName)));
       
   145 	headerSet.Add("$MAJOR", CdlTkUtil::IntToString(header.Version().Major()));
       
   146 	headerSet.Add("$MINOR", CdlTkUtil::IntToString(header.Version().Minor()));
       
   147 	headerSet.Add("$FLAGS", header.Flags().FlagsAsString());
       
   148 	Stream() << CdlTkUtil::MultiReplace(headerSet, KInterfaceConstants);
       
   149 	WriteApiEnum();
       
   150 	Stream() << KCdlInterface;
       
   151 	WriteApiTypedefs();
       
   152 	Stream() << endl;
       
   153 	WriteNamespaceEnd(iCdl, Stream());
       
   154 	WriteHeaderGuardEnd(iFileName, Stream());
       
   155 	}
       
   156 
       
   157 void CCdlTkWriteCommonDefs::WriteApiEnum()
       
   158 	{
       
   159 	const CCdlTkApiList& apiList = iCdl.ApiList();
       
   160 	for (CCdlTkApiList::const_iterator pApi = apiList.begin(); pApi != apiList.end(); ++pApi)
       
   161 		{
       
   162 		Stream() << "\tEApiId_" << (*pApi)->Name() << "," << endl;
       
   163 		}
       
   164 	}
       
   165 
       
   166 string CCdlTkWriteCommonDefs::TypedefForApi(const CCdlTkApi& aApi) const
       
   167 	{
       
   168 	if (aApi.IsFunc())
       
   169 		{
       
   170 		const CCdlTkFunctionApi& func = aApi.AsFunc();
       
   171 		return string("typedef ") + func.ReturnType() + " (" + func.ApiNameAsTypeName() + ")(" + func.ParamTypeList() + ");";
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		return string("// ") + aApi.ReturnType() + " " + aApi.Name() + " \tdoes not need a typedef";
       
   176 		}
       
   177 	}
       
   178 
       
   179 void CCdlTkWriteCommonDefs::WriteApiTypedefs()
       
   180 	{
       
   181 	for (CCdlTkApiList::const_iterator pApi = iCdl.ApiList().begin(); pApi != iCdl.ApiList().end(); ++pApi)
       
   182 		Stream() << TypedefForApi(**pApi) << endl;
       
   183 	}
       
   184 
       
   185 std::ofstream& CCdlTkWriteCommonDefs::Stream()
       
   186 	{
       
   187 	return *iStream;
       
   188 	}
       
   189 
       
   190 }	// end of namespace CdlCompilerToolkit