aknlayoutcompiler/src/AdaptationLayerTemplate.cpp
changeset 0 f58d6ec98e88
child 1 b700e12870ca
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 
       
    18 
       
    19 
       
    20 // disable "identifier was truncated to '255' characters in the browser information" warning
       
    21 #pragma warning (disable:4786)
       
    22 
       
    23 #include "AdaptationLayerTemplate.h"
       
    24 #include "LayoutCompilerErr.h"
       
    25 #include "LayoutParse.h"
       
    26 #include "LayoutWriter.h"
       
    27 #include "CppWriter.h"
       
    28 #include "Lay2Cdl.h"
       
    29 #include <fstream>
       
    30 #include <algorithm>
       
    31 #include <iostream>
       
    32 #include <sstream>
       
    33 #include "CodeGenConsts.h"
       
    34 using namespace std;
       
    35 using namespace CdlCompilerToolkit;
       
    36 
       
    37 typedef LayoutProcessArgsErr<AdaptationLayerTemplate> AdaptationLayerTemplateArgsErr;
       
    38 
       
    39 
       
    40 
       
    41 
       
    42 int AdaptationLayerTemplate::Process(const vector<string>& args)
       
    43 	{
       
    44 	if (args.size() < 6)
       
    45 		throw AdaptationLayerTemplateArgsErr();
       
    46 
       
    47 	string layName = args[2];
       
    48 	auto_ptr<TLayParseLayout> layoutParse = TLayParseLayout::Parse(layName);
       
    49 
       
    50 	string layCdlName = args[3];
       
    51 	CCdlTkCdlFileParser layCdlParser(layCdlName);
       
    52 	auto_ptr<CCdlTkInterface> layIface(layCdlParser.LoadAndParse(true));
       
    53 
       
    54 	string scaleCdlName = args[4];
       
    55 	CCdlTkCdlFileParser scaleCdlParser(scaleCdlName);
       
    56 	auto_ptr<CCdlTkInterface> scaleIface(scaleCdlParser.LoadAndParse(true));
       
    57 
       
    58 	string instName = args[5];
       
    59 	auto_ptr<CCdlTkInstance> inst(new CCdlTkInstance(*layIface));
       
    60 	inst->SetName(instName);
       
    61 	inst->TemplateAllImplementations();
       
    62 
       
    63 	string existing;
       
    64 	if (args.size() == 8)
       
    65 		{
       
    66 		string inputName = args[6];
       
    67 		CdlTkUtil::ReadFile(existing, inputName);
       
    68 		}
       
    69 
       
    70 	AdaptationLayerTemplate process(*layoutParse, *layIface, *scaleIface, *inst, existing);
       
    71 	process.Process();
       
    72 
       
    73 	CCdlTkWriteInstance writer(*inst);
       
    74 	writer.Process();
       
    75 
       
    76 	if (args.size() == 8)
       
    77 		{
       
    78 		string outputName = args[7];
       
    79 		CdlTkUtil::WriteFile(existing, outputName);
       
    80 		}
       
    81 
       
    82 	return 0;
       
    83 	}
       
    84 
       
    85 void AdaptationLayerTemplate::ShowHelp(ostream& stream)
       
    86 	{
       
    87 	stream << "AdaptationLayerTemplate <layout.lay> <layInterface.cdl> <scaleInterface.cdl> <instanceName> [<existingImplToModify> <fileToWriteModsTo>]" << endl;
       
    88 	stream << "  Creates a template layout adaptation layer implementation." << endl;
       
    89 	}
       
    90 
       
    91 
       
    92 AdaptationLayerTemplate::AdaptationLayerTemplate(TLayout& aLayout, CCdlTkInterface& aLayIface, CCdlTkInterface& aScaleIface, CCdlTkInstance& aInstance, string& aExisting)
       
    93 : iLayout(aLayout), iLayIface(aLayIface), iScaleIface(aScaleIface), iInstance(aInstance), iExisting(aExisting)
       
    94 	{
       
    95 	}
       
    96 
       
    97 AdaptationLayerTemplate::~AdaptationLayerTemplate()
       
    98 	{
       
    99 	}
       
   100 
       
   101 void AdaptationLayerTemplate::Process()
       
   102 	{
       
   103 	for (TLayout::iterator pTab = iLayout.begin(); pTab != iLayout.end(); ++pTab)
       
   104 		{
       
   105 		AddTableToInstance(**pTab);
       
   106 		}
       
   107 	}
       
   108 
       
   109 void AdaptationLayerTemplate::AddTableToInstance(TLayoutTable& aTable)
       
   110 	{
       
   111 	for (TLayoutTable::iterator pLine = aTable.begin(); pLine != aTable.end(); ++pLine)
       
   112 		{
       
   113 		TLayoutLine& line = **pLine;
       
   114 		AddLineToInstance(line);
       
   115 		}
       
   116 	}
       
   117 
       
   118 void AdaptationLayerTemplate::AddLineToInstance(TLayoutLine& aLine)
       
   119 	{
       
   120 	string apiName = LayoutToCdl::LineApiName(aLine);
       
   121 	if (!HasApi(apiName))
       
   122 		return;
       
   123 	CCdlTkImplementation& imp = FindImp(apiName);
       
   124 	SetFuncLine(imp, aLine);
       
   125 	}
       
   126 
       
   127 bool AdaptationLayerTemplate::HasApi(const string& aName)
       
   128 	{
       
   129 	return iLayIface.ApiList().Find(aName) != 0;
       
   130 	}
       
   131 
       
   132 CCdlTkImplementation& AdaptationLayerTemplate::FindImp(const string& aName)
       
   133 	{
       
   134 	CCdlTkImplementation* impl = iInstance.Impl().Find(aName);
       
   135 	if (!impl)
       
   136 		throw NotFoundErr(aName + " in interface " + iLayIface.FileName());
       
   137 	return *impl;
       
   138 	}
       
   139 
       
   140 int CountMatch(const string& aLeft, const string& aRight)
       
   141 	{
       
   142 	string left(CdlTkUtil::ToLower(aLeft));
       
   143 	left = CdlTkUtil::Replace("_line_", "", left);
       
   144 	string right(CdlTkUtil::ToLower(aRight));
       
   145 
       
   146 	int count = 0;
       
   147 	int subLen = 3;
       
   148 	int leftSize = left.size();
       
   149 	const int maxPossibleHits = (leftSize + 1 - subLen);
       
   150 	for (int ii=0; ii<maxPossibleHits; ii++)
       
   151 		{
       
   152 		if (right.find(left.substr(ii, subLen)) != string::npos)
       
   153 			count++;
       
   154 		}
       
   155 
       
   156 	const int KMatchPercentLimit = 25;
       
   157 	int percent = count * 100 / maxPossibleHits;
       
   158 	if (percent < KMatchPercentLimit)
       
   159 		return 0;
       
   160 	return percent;
       
   161 	}
       
   162 
       
   163 pair<string,int> AdaptationLayerTemplate::GetApiMatch(const string& aName)
       
   164 	{
       
   165 	int size = aName.size();
       
   166 	CCdlTkApiList& apiList = iScaleIface.ApiList();
       
   167 	string bestMatch;
       
   168 	int bestScore = 0;
       
   169 	for (CCdlTkApiList::iterator pApi = apiList.begin(); pApi != apiList.end(); ++pApi)
       
   170 		{
       
   171 		CCdlTkApi& api = **pApi;
       
   172 		string name = api.Name();
       
   173 		if (aName == name)
       
   174 			return make_pair(name,100);
       
   175 
       
   176 		int score = CountMatch(aName, name);
       
   177 		if (score > bestScore)
       
   178 			{
       
   179 			bestScore = score;
       
   180 			bestMatch = name;
       
   181 			}
       
   182 		}
       
   183 	return make_pair(bestMatch, bestScore);
       
   184 	}
       
   185 
       
   186 bool IsValueCell(const string& aCellName)
       
   187 	{
       
   188 	return
       
   189 		(find(KWindowOutputOrder, KWindowOutputOrder+KWindowOutputOrderSize, aCellName) != KWindowOutputOrder+KWindowOutputOrderSize)
       
   190 		||
       
   191 		(find(KTextOutputOrder, KTextOutputOrder+KTextOutputOrderSize, aCellName) != KTextOutputOrder+KTextOutputOrderSize);
       
   192 	}
       
   193 
       
   194 const string KUsefulInfo = "\
       
   195 // $LAYOUT\n\
       
   196 // Remark                    : $REMARK\n\
       
   197 // Parent                    : $PARENTNAME\n\
       
   198 // Parent API                : $PARENTAPI\n\
       
   199 // Scale API match    ($AM%) : $SCALEAPI\n\
       
   200 // Scale parent match ($PM%) : $SCALEPARENT\n\
       
   201 ";
       
   202 
       
   203 void AdaptationLayerTemplate::SetFuncLine(CCdlTkImplementation& aImp, TLayoutLine& aLine)
       
   204 	{
       
   205 	string layout;
       
   206 	for (TLayoutLine::iterator pCell = aLine.begin(); pCell != aLine.end(); ++pCell)
       
   207 		{
       
   208 		const string& cellName = pCell->first;
       
   209 		if (IsValueCell(cellName))
       
   210 			{
       
   211 			TValues& values = pCell->second;
       
   212 			stringstream s;
       
   213 			TLayoutTableWriter::WriteCell(s, values);
       
   214 			CdlTkUtil::AppendString(layout, cellName);
       
   215 			CdlTkUtil::AppendString(layout, "=");
       
   216 			CdlTkUtil::AppendString(layout, s.str());
       
   217 			CdlTkUtil::AppendString(layout, " ");
       
   218 			}
       
   219 		}
       
   220 
       
   221 	string parent;
       
   222 	string parentApi;
       
   223 	if (aLine.iTable && aLine.iTable->iParent)
       
   224 		{
       
   225 		parent = aLine.iTable->iParent->Name();
       
   226 		parentApi = LayoutToCdl::LineApiName(*aLine.iTable->iParent);
       
   227 		}
       
   228 
       
   229 	pair<string,int> apiMatch(GetApiMatch(LayoutToCdl::LineApiName(aLine)));
       
   230 	pair<string,int> parentMatch(GetApiMatch(parentApi));
       
   231 
       
   232 	char apiPercent[12];
       
   233 	sprintf(apiPercent, "%3d", apiMatch.second);
       
   234 	char parentPercent[12];
       
   235 	sprintf(parentPercent, "%3d", parentMatch.second);
       
   236 
       
   237 	CdlTkUtil::CReplaceSet set;
       
   238 	set.Add("$LAYOUT", layout);
       
   239 	set.Add("$REMARK", aLine["Remarks"][0]);
       
   240 	set.Add("$PARENTNAME", parent);
       
   241 	set.Add("$PARENTAPI", parentApi);
       
   242 	set.Add("$SCALEAPI", apiMatch.first);
       
   243 	set.Add("$SCALEPARENT", parentMatch.first);
       
   244 	set.Add("$AM", apiPercent);
       
   245 	set.Add("$PM", parentPercent);
       
   246 	string useful = CdlTkUtil::MultiReplace(set, KUsefulInfo);
       
   247 
       
   248 	string defn = aImp.Definition();
       
   249 	string existingTarget = defn.substr(0,defn.find_first_of('(')+1);
       
   250 	string existingSub = useful;
       
   251 	CdlTkUtil::AppendString(existingSub, existingTarget);
       
   252 	iExisting = CdlTkUtil::Replace(existingTarget, existingSub, iExisting);
       
   253 
       
   254 	CdlTkUtil::AppendString(useful, defn);
       
   255 	aImp.SetDefinition(useful);
       
   256 	}