aknlayoutcompiler/src/LayoutConfig.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 // disable "identifier was truncated to '255' characters in the browser information" warning
       
    18 #pragma warning (disable:4786)
       
    19 
       
    20 #include "LayoutConfig.h"
       
    21 #include "LayoutCompilerErr.h"
       
    22 #include <fstream>
       
    23 #include <iostream>
       
    24 
       
    25 using namespace std;
       
    26 
       
    27 const string KBuildConfigFile("\\epoc32\\include\\oem\\bldvariant.hrh");
       
    28 const string KLayoutMacroStart("__LAYOUT");
       
    29 
       
    30 typedef LayoutProcessArgsErr<LayoutConfig> LayoutConfigArgsErr;
       
    31 
       
    32 
       
    33 LayoutConfig::Size::Size()
       
    34 : iWidth(0), iHeight(0)
       
    35 	{
       
    36 	}
       
    37 
       
    38 LayoutConfig::Size::TOrientation LayoutConfig::Size::Orientation() const
       
    39 	{
       
    40 	if (iWidth < iHeight)
       
    41 		return EPortrait;
       
    42 	else if (iWidth == iHeight)
       
    43 		return ESquare;
       
    44 	else
       
    45 		return ELandscape;
       
    46 	}
       
    47 
       
    48 bool LayoutConfig::Size::operator>(const Size& aRhs) const
       
    49 	{
       
    50 	if (iWidth == aRhs.iWidth)
       
    51 		return iHeight > aRhs.iHeight;
       
    52 	else
       
    53 		return iWidth > aRhs.iWidth;
       
    54 	}
       
    55 
       
    56 
       
    57 int LayoutConfig::Process(const vector<string>& args)
       
    58 	{
       
    59 	if (args.size() < 3)
       
    60 		throw LayoutConfigArgsErr();
       
    61 
       
    62 	string mode = CdlTkUtil::ToLower(args[2]);
       
    63 
       
    64 	auto_ptr<LayoutConfig> config(0);
       
    65 
       
    66 	if (mode == "wsini")
       
    67 		config = auto_ptr<LayoutConfig>(new WsiniConfig);
       
    68 	else
       
    69 		throw LayoutConfigArgsErr();
       
    70 
       
    71 	config->ParseArgs(args);
       
    72 	return config->Process();
       
    73 	}
       
    74 
       
    75 void LayoutConfig::ParseArgs(const vector<string>& args)
       
    76 	{
       
    77 	int argsSize = args.size();
       
    78 	int nextArg = 3;
       
    79 	while (nextArg < argsSize && args[nextArg][0] == '-')
       
    80 		{
       
    81 		ParseOpt(CdlTkUtil::ToLower(args[nextArg]));
       
    82 		nextArg++;
       
    83 		}
       
    84 
       
    85 	if (nextArg == argsSize)
       
    86 		throw LayoutConfigArgsErr();
       
    87 
       
    88 	iTargetFileName = args[nextArg];
       
    89 	nextArg++;
       
    90 
       
    91 	if (nextArg < argsSize)
       
    92 		iBaseFileName = args[nextArg];
       
    93 	}
       
    94 
       
    95 void LayoutConfig::ParseOpt(const std::string& opt)
       
    96 	{
       
    97 	if (opt.size() < 2)
       
    98 		throw LayoutConfigArgsErr();
       
    99 
       
   100 	switch (opt[1])
       
   101 		{
       
   102 		case 't':
       
   103 			if (opt == "-tplatform")
       
   104 				iTarget = EPlatform;
       
   105 			else if (opt == "-tproduct")
       
   106 				iTarget = EProduct;
       
   107 			else
       
   108 				throw LayoutConfigArgsErr();
       
   109 			break;
       
   110 		case 'l':
       
   111 			if (opt == "-llegacy")
       
   112 				iLegacyMode = ELegacyMode;
       
   113 			else if (opt == "-lnolegacy")
       
   114 				iLegacyMode = ENoLegacyMode;
       
   115 			else
       
   116 				throw LayoutConfigArgsErr();
       
   117 			break;
       
   118 		default:
       
   119 			throw LayoutConfigArgsErr();
       
   120 			break;
       
   121 		}
       
   122 	}
       
   123 
       
   124 void LayoutConfig::ShowHelp(ostream& stream)
       
   125 	{
       
   126 	stream << "Config <configMode> [-t<target>] [-l<legacyMode>] <targetFile> [<baseFile>]" << endl;
       
   127 	stream << "  <configMode> is one of wsini, epocini, aknlayout2" << endl;
       
   128 	stream << "  <target> is one of platform, product (default is platform)" << endl;
       
   129 	stream << "  <legacyMode> is one of legacy, nolegacy (default is legacy)" << endl;
       
   130 	stream << "  <targetFile> is the file to be written to" << endl;
       
   131 	stream << "  <baseFile> is a file containing the template for the target file" << endl;
       
   132 	}
       
   133 
       
   134 LayoutConfig::LayoutConfig()
       
   135 : iTarget(EPlatform), iLegacyMode(ELegacyMode)
       
   136 	{
       
   137 	LoadAllSizes();
       
   138 	LoadConfiguredSizes();
       
   139 	}
       
   140 
       
   141 LayoutConfig::~LayoutConfig()
       
   142 	{
       
   143 	}
       
   144 
       
   145 const LayoutConfig::Sizes& LayoutConfig::AllSizes() const
       
   146 	{
       
   147 	return iAll;
       
   148 	}
       
   149 
       
   150 const LayoutConfig::Sizes& LayoutConfig::ConfiguredSizes() const
       
   151 	{
       
   152 	return iConf;
       
   153 	}
       
   154 
       
   155 const LayoutConfig::Sizes& LayoutConfig::SizesForTarget() const
       
   156 	{
       
   157 	if (iTarget == EPlatform)
       
   158 		return iAll;
       
   159 	else
       
   160 		return iConf;
       
   161 	}
       
   162 
       
   163 void LayoutConfig::LoadAllSizes()
       
   164 	{
       
   165 	ifstream bldCfg;
       
   166 	OpenBldCfg(bldCfg);
       
   167 
       
   168 	string line;
       
   169 	while (!bldCfg.eof())
       
   170 		{
       
   171 		getline(bldCfg, line);
       
   172 		Size s;
       
   173 		if (ParseSize(line, s))
       
   174 			{
       
   175 			iAll.insert(s);
       
   176 			if (line.find("#define") == 0)
       
   177 				iConf.insert(s);
       
   178 			}
       
   179 		}
       
   180 	}
       
   181 
       
   182 void LayoutConfig::LoadConfiguredSizes()
       
   183 	{
       
   184 	// done crudely as part of the LoadAllSizes() function currently
       
   185 	}
       
   186 
       
   187 void LayoutConfig::OpenBldCfg(ifstream& aBldCfg)
       
   188 	{
       
   189 	aBldCfg.open(KBuildConfigFile.c_str());
       
   190 	if (!aBldCfg.is_open())
       
   191 		throw NotFoundErr(KBuildConfigFile);
       
   192 	}
       
   193 
       
   194 bool LayoutConfig::ParseSize(const std::string& aLine, Size& aSize) const
       
   195 	{
       
   196 	int pos = aLine.find(KLayoutMacroStart);
       
   197 	if (pos == string::npos)
       
   198 		return false;
       
   199 
       
   200 	pos += KLayoutMacroStart.size();
       
   201 	pos = aLine.find_first_not_of('_', pos);
       
   202 	if (pos == string::npos)
       
   203 		return false;
       
   204 
       
   205 	aSize.iWidth = CdlTkUtil::ParseInt(aLine.substr(pos));
       
   206 
       
   207 	pos = aLine.find('_', pos);
       
   208 	if (pos == string::npos)
       
   209 		return false;
       
   210 	pos = aLine.find_first_not_of('_', pos);
       
   211 	if (pos == string::npos)
       
   212 		return false;
       
   213 
       
   214 	aSize.iHeight = CdlTkUtil::ParseInt(aLine.substr(pos));
       
   215 	
       
   216 	return (aSize.iWidth != 0 && aSize.iHeight != 0);
       
   217 	}
       
   218 
       
   219 /*
       
   220 int LayoutConfig::TestLayoutConfig()
       
   221 	{
       
   222 	LayoutConfig l;
       
   223 
       
   224 	cout << "All:" << endl;
       
   225 	for (Sizes::iterator pAll = l.iAll.begin(); pAll != l.iAll.end(); ++pAll)
       
   226 		{
       
   227 		Size& s = *pAll;
       
   228 		cout << s.iWidth << " x " << s.iHeight << endl;
       
   229 		}
       
   230 	cout << "Conf:" << endl;
       
   231 	for (Sizes::iterator pConf = l.iConf.begin(); pConf != l.iConf.end(); ++pConf)
       
   232 		{
       
   233 		Size& s = *pConf;
       
   234 		cout << s.iWidth << " x " << s.iHeight << endl;
       
   235 		}
       
   236 	return 0;
       
   237 	}
       
   238 */
       
   239 
       
   240 
       
   241 int WsiniConfig::Process()
       
   242 	{
       
   243 	const Sizes& s = SizesForTarget();
       
   244 // need a lot more info to make a sensible descision here
       
   245 // eg what is the prefered size and orientation? How should legacy mode be
       
   246 // handled, etc.
       
   247 // also, what should we do with multiple screens?????
       
   248 // neeeds a lot more thought.
       
   249 	return 0;
       
   250 	}
       
   251