cdlcompilertoolkit/src/CdlTkWriteCdlFile.cpp
changeset 0 f58d6ec98e88
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 #include "CdlCompilerToolkit/CdlTkProcess.h"
       
    18 #include <iomanip>
       
    19 using namespace std;
       
    20 
       
    21 namespace CdlCompilerToolkit {
       
    22 
       
    23 
       
    24 //
       
    25 // CleanupResetPtr
       
    26 //
       
    27 
       
    28 class CleanupResetPtr
       
    29 	{
       
    30 public:
       
    31 	CleanupResetPtr(const CCdlTkInterface*& aPtr) : iPtr(aPtr), iValue(aPtr) {}
       
    32 	~CleanupResetPtr() { iPtr = iValue; }
       
    33 private:
       
    34 	const CCdlTkInterface*& iPtr;
       
    35 	const CCdlTkInterface* iValue;
       
    36 	};
       
    37 
       
    38 
       
    39 //
       
    40 // CCdlTkWriteCdlFile
       
    41 //
       
    42 
       
    43 CCdlTkWriteCdlFile::CCdlTkWriteCdlFile(const CCdlTkInterface& aCdl)
       
    44 : iCdl(&aCdl)
       
    45 	{
       
    46 	}
       
    47 
       
    48 CCdlTkWriteCdlFile::~CCdlTkWriteCdlFile()
       
    49 	{
       
    50 	}
       
    51 
       
    52 void CCdlTkWriteCdlFile::Process()
       
    53 	{
       
    54 	// this function modifies iCdl, but CleanupResetPtr will restore it at the end (it's also exception safe)
       
    55 	CleanupResetPtr resetPtr(iCdl);
       
    56 
       
    57 	CCdlTkFileCleanup tempFile;
       
    58 	CdlTkUtil::OpenTempOutput(iOut, tempFile);
       
    59 	string targetName = iCdl->FileName();
       
    60 
       
    61 	WriteHeaderComment();
       
    62 	int count = 0;
       
    63 	while (iCdl)
       
    64 		{
       
    65 		if (count++)
       
    66 			WriteSeparator("Header");
       
    67 		WriteHeader();
       
    68 		if (!iCdl->Cpp().empty())
       
    69 			{
       
    70 			WriteSeparator("C++");
       
    71 			WriteCpp();
       
    72 			}
       
    73 		if (!iCdl->DataTypeTranslations().empty())
       
    74 			{
       
    75 			WriteSeparator("Translation");
       
    76 			WriteTranslation();
       
    77 			}
       
    78 		if (!iCdl->ApiList().empty())
       
    79 			{
       
    80 			WriteSeparator("API");
       
    81 			WriteApi();
       
    82 			}
       
    83 		iCdl = iCdl->Extension();
       
    84 		}
       
    85 
       
    86 	iOut.close();
       
    87 	CdlTkUtil::ExportFile(tempFile, targetName);
       
    88 
       
    89 	// resetPtr dtor will restore iCdl to original value
       
    90 	}
       
    91 
       
    92 void CCdlTkWriteCdlFile::WriteHeaderComment()
       
    93 	{
       
    94 	iOut << "// " << iCdl->FileName() << endl;
       
    95 	iOut << "// This file was generated by:" << endl;
       
    96 	iOut << "// " << CdlTkUtil::CommandLine() << endl;
       
    97 	iOut << iCdl->AdditionalComment() << endl;
       
    98 	}
       
    99 
       
   100 void CCdlTkWriteCdlFile::WriteHeader()
       
   101 	{
       
   102 	const CCdlTkInterfaceHeader& header = iCdl->Header();
       
   103 	if (!header.Name().empty())
       
   104 		iOut << "Name: " << header.Name() << endl;
       
   105 	iOut << "Version: " << header.Version().Major() << "." << header.Version().Minor() << endl;
       
   106 	if (header.Uid())
       
   107 		iOut << "UID: 0x" << setbase(16) << setw(8) << setfill('0') << header.Uid() << endl;
       
   108 	const CCdlTkInterfaceHeader::CFlags& flags = header.Flags();
       
   109 	for (int ii=0; ii<flags.Count(); ii++)
       
   110 		{
       
   111 		if (flags.IsSet(ii))
       
   112 			iOut << "Flag: " << flags.FlagName(ii) << endl;
       
   113 		}
       
   114 	}
       
   115 
       
   116 void CCdlTkWriteCdlFile::WriteSeparator(const string& aSection)
       
   117 	{
       
   118 	iOut << endl;
       
   119 	iOut << "%% " << aSection << endl;
       
   120 	iOut << endl;
       
   121 	}
       
   122 
       
   123 void CCdlTkWriteCdlFile::WriteCpp()
       
   124 	{
       
   125 	const CCdlTkCpp& cpp = iCdl->Cpp();
       
   126 	if(cpp.size())
       
   127 		{
       
   128 		CCdlTkCpp::const_iterator pStart = cpp.begin();
       
   129 		CCdlTkCpp::const_iterator pFinish = cpp.end() ;
       
   130 
       
   131 		// ignore the first line if it's empty
       
   132 		if(pStart->empty())
       
   133 			++pStart;
       
   134 		// if the last line isn't empty, move the iterator back to the end
       
   135 		if(!(--pFinish)->empty())
       
   136 			++pFinish;
       
   137 
       
   138 		for (CCdlTkCpp::const_iterator pLine = pStart; pLine != pFinish; ++pLine)
       
   139 			{
       
   140 			iOut << *pLine << endl;
       
   141 			}
       
   142 		}
       
   143 	}
       
   144 
       
   145 void CCdlTkWriteCdlFile::WriteTranslation()
       
   146 	{
       
   147 	const CCdlTkDataTypeTranslations& translations = iCdl->DataTypeTranslations();
       
   148 	for (CCdlTkDataTypeTranslations::const_iterator pTrans = translations.begin(); pTrans != translations.end(); ++pTrans)
       
   149 		{
       
   150 		if (pTrans->Source() == CCdlTkDataTypeTranslation::EFromCdl)
       
   151 			{
       
   152 			iOut << pTrans->Type() << " # " << pTrans->Definition() << " # " << pTrans->PointerReference() << endl;
       
   153 			}
       
   154 		}
       
   155 	}
       
   156 
       
   157 void CCdlTkWriteCdlFile::WriteApi()
       
   158 	{
       
   159 	const CCdlTkApiList& apiList = iCdl->ApiList();
       
   160 	for (CCdlTkApiList::const_iterator pApi = apiList.begin(); pApi != apiList.end(); ++pApi)
       
   161 		{
       
   162 		CCdlTkApi& api = **pApi;
       
   163 		iOut << api.Comment();
       
   164 		iOut << api.ReturnType() << " " << api.Name() << api.ParamsTypeAndNameList() << ";" << endl;
       
   165 		iOut << endl;
       
   166 		}
       
   167 	}
       
   168 
       
   169 }	// end of namespace CdlCompilerToolkit