libraries/iosrv/client/pod_formatter.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // pod_formatter.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "pod_formatter.h"
       
    14 
       
    15 const TInt KGap = 2;
       
    16 
       
    17 _LIT(KPartialCommandStart, "\r\n=");
       
    18 _LIT(KCommandStart, "\r\n\r\n=");
       
    19 _LIT(KBlankLine, "\r\n\r\n");
       
    20 _LIT(KNewLine, "\r\n");
       
    21 
       
    22 using namespace IoUtils;
       
    23 
       
    24 
       
    25 TPodFormatter::TPodFormatter(CTextFormatter& aFormatter)
       
    26 	: iFormatter(aFormatter)
       
    27 	{
       
    28 	}
       
    29 
       
    30 void TPodFormatter::FormatL(const TDesC& aPod)
       
    31 	{
       
    32 	iPodLex = aPod;
       
    33 	while (!iPodLex.Eos())
       
    34 		{
       
    35 		TPtrC command(NextCommand());
       
    36 		if (command == _L("pod"))
       
    37 			{
       
    38 			SkipToNextBlankLine();
       
    39 			}
       
    40 		else if ((command == _L("head1")) || (command == _L("head2")))
       
    41 			{
       
    42 			iPodLex.SkipSpaceAndMark();
       
    43 			SkipToLineEnd();
       
    44 			iFormatter.SetAttributesL(ConsoleAttributes::EBold | ConsoleAttributes::EUnderscore);
       
    45 			iFormatter.WrapL(0, iPodLex.MarkedToken());
       
    46 			iFormatter.SetAttributesL(ConsoleAttributes::ENone);
       
    47 			iFormatter.AppendL(KBlankLine);
       
    48 			}
       
    49 		else if (command == _L("cut"))
       
    50 			{
       
    51 			SkipToNextBlankLine();
       
    52 			break;
       
    53 			}
       
    54 		else if (command == _L("begin"))
       
    55 			{
       
    56 			TPtrC nextCommand;
       
    57 			do
       
    58 				{
       
    59 				nextCommand.Set(NextCommand());
       
    60 				}
       
    61 				while ((nextCommand.Length() > 0) && (nextCommand != _L("end")));
       
    62 
       
    63 			SkipToNextBlankLine();
       
    64 			}
       
    65 		else if (command == _L("for"))
       
    66 			{
       
    67 			SkipToNextBlankLine();
       
    68 			}
       
    69 		else if (command == _L("over"))
       
    70 			{
       
    71 			iPodLex.SkipSpace();
       
    72 			TUint indent;
       
    73 			User::LeaveIfError(iPodLex.Val(indent));
       
    74 			OverL(indent);
       
    75 			}
       
    76 
       
    77 		FormatToNextCommandL();
       
    78 		}
       
    79 	}
       
    80 
       
    81 void TPodFormatter::OverL(TInt aIndent)
       
    82 	{
       
    83 	CTextBuffer* buffer = CTextBuffer::NewLC(0x100);
       
    84 
       
    85 	while (!iPodLex.Eos())
       
    86 		{
       
    87 		FormatToNextCommandL(aIndent);
       
    88 
       
    89 		TPtrC command(NextCommand());
       
    90 		if (command == _L("item"))
       
    91 			{
       
    92 			iPodLex.SkipSpaceAndMark();
       
    93 			SkipToLineEnd();
       
    94 			buffer->AppendL(iPodLex.MarkedToken());
       
    95 			iPodLex.SkipSpace();
       
    96 			//buffer->AppendL('\t');
       
    97 			//buffer->AppendL(TextToNextCommand());
       
    98 			// Need to split the text up putting a tab in front of every paragraph of text, in case the item description is multi-paragraph.
       
    99 			// If we don't, the next paragraph will end up in column one which will break the column formatter.
       
   100 			// If there's a neater way of doing this, someone feel free! -TomS
       
   101 			TPtrC text(TextToNextCommand());
       
   102 			while(text.Length())
       
   103 				{
       
   104 				TInt nextNewLine = text.Find(KNewLine);
       
   105 				if (nextNewLine == KErrNotFound) nextNewLine = text.Length();
       
   106 				TPtrC line(text.Left(nextNewLine+KNewLine().Length()));
       
   107 				buffer->AppendL('\t');
       
   108 				buffer->AppendL(line);
       
   109 				text.Set(text.Mid(line.Length()));
       
   110 				}
       
   111 			buffer->AppendL(KNewLine);
       
   112 			}
       
   113 		else if (command == _L("back"))
       
   114 			{
       
   115 			break;
       
   116 			}
       
   117 		else if (command == _L("over"))
       
   118 			{
       
   119 			if (buffer->Descriptor().Length() > 0)
       
   120 				{
       
   121 				iFormatter.TabulateL(aIndent, KGap, buffer->Descriptor(), EWrapLastColumn);
       
   122 				iFormatter.AppendL(KNewLine);
       
   123 				buffer->Zero();
       
   124 				}
       
   125 			iPodLex.SkipSpace();
       
   126 			TUint indent;
       
   127 			User::LeaveIfError(iPodLex.Val(indent));
       
   128 			OverL(indent + aIndent);
       
   129 			}
       
   130 		}
       
   131 
       
   132 	if (buffer->Descriptor().Length() > 0)
       
   133 		{
       
   134 		iFormatter.TabulateL(aIndent, KGap, buffer->Descriptor(), EWrapLastColumn);
       
   135 		iFormatter.AppendL(KNewLine);
       
   136 		}
       
   137 
       
   138 	CleanupStack::PopAndDestroy(buffer);
       
   139 	}
       
   140 
       
   141 void TPodFormatter::FormatToNextCommandL(TInt aIndent)
       
   142 	{
       
   143 	FOREVER
       
   144 		{
       
   145 		TPtrC paragraph(NextParagraph());
       
   146 		if (paragraph.Length() > 0)
       
   147 			{
       
   148 			if (TChar(paragraph[0]).IsSpace())
       
   149 				{
       
   150 				iFormatter.AppendL(paragraph);
       
   151 				}
       
   152 			else
       
   153 				{
       
   154 				iFormatter.WrapL(aIndent, paragraph);
       
   155 				}
       
   156 			iFormatter.AppendL(KNewLine);
       
   157 			}
       
   158 		else
       
   159 			{
       
   160 			break;
       
   161 			}
       
   162 		}
       
   163 	}
       
   164 
       
   165 TPtrC TPodFormatter::NextCommand()
       
   166 	{
       
   167 	TPtrC command;
       
   168 	if (SkipToNextCommand() == KErrNone)
       
   169 		{
       
   170 		iPodLex.Mark();
       
   171 		iPodLex.SkipCharacters();
       
   172 		command.Set(iPodLex.MarkedToken());
       
   173 		}
       
   174 	return command;
       
   175 	}
       
   176 
       
   177 TPtrC TPodFormatter::NextParagraph()
       
   178 	{
       
   179 	// Consume leading new line characters.
       
   180 	while (iPodLex.Remainder().Find(KNewLine) == 0)
       
   181 		{
       
   182 		if (AtCommandStart())
       
   183 			{
       
   184 			return TPtrC();
       
   185 			}
       
   186 		iPodLex.Inc(KNewLine().Length());
       
   187 		}
       
   188 	
       
   189 	iPodLex.Mark();
       
   190 
       
   191 	if (iPodLex.Peek().IsSpace())
       
   192 		{
       
   193 		// This line has leading white space - treat everything up to the next blank line as a single paragraph.
       
   194 		if (SkipToNextBlankLine() != KErrNone)
       
   195 			{
       
   196 			SkipToLineEnd();
       
   197 			}
       
   198 		}
       
   199 	else if (SkipToNextLine() != KErrNone)
       
   200 		{
       
   201 		SkipToLineEnd();
       
   202 		}
       
   203 
       
   204 	return iPodLex.MarkedToken();
       
   205 	}
       
   206 
       
   207 TPtrC TPodFormatter::TextToNextCommand()
       
   208 	{
       
   209 	TInt initialOffset = iPodLex.Offset();
       
   210 	iPodLex.Mark();
       
   211 
       
   212 	if (SkipToNextCommand() == KErrNone)
       
   213 		{
       
   214 		if (iPodLex.Offset() >= KCommandStart().Length())
       
   215 			{
       
   216 			iPodLex.Inc(-(KCommandStart().Length()));
       
   217 			}
       
   218 		else
       
   219 			{
       
   220 			iPodLex.Inc(-(iPodLex.Remainder().Length()));
       
   221 			}
       
   222 		}
       
   223 	else
       
   224 		{
       
   225 		iPodLex.Inc(iPodLex.Remainder().Length());
       
   226 		}
       
   227 
       
   228 	if (initialOffset < iPodLex.Offset())
       
   229 		{
       
   230 		return iPodLex.MarkedToken();
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 		return TPtrC(NULL, 0);
       
   235 		}
       
   236 	}
       
   237 
       
   238 void TPodFormatter::SkipToLineEnd()
       
   239 	{
       
   240 	if (SkipTo(KNewLine) != KErrNone)
       
   241 		{
       
   242 		iPodLex.Inc(iPodLex.Remainder().Length());
       
   243 		}
       
   244 	}
       
   245 
       
   246 TInt TPodFormatter::SkipToNextLine()
       
   247 	{
       
   248 	TInt ret = SkipTo(KNewLine);
       
   249 	if (ret == KErrNone)
       
   250 		{
       
   251 		iPodLex.Inc(KNewLine().Length());
       
   252 		}
       
   253 	return ret;
       
   254 	}
       
   255 
       
   256 TInt TPodFormatter::SkipToNextCommand()
       
   257 	{
       
   258 	if (iPodLex.Remainder().Find(KPartialCommandStart) == 0)
       
   259 		{
       
   260 		if (iPodLex.Offset() == 0)
       
   261 			{
       
   262 			iPodLex.Inc(KPartialCommandStart().Length());
       
   263 			return KErrNone;
       
   264 			}
       
   265 		else
       
   266 			{
       
   267 			iPodLex.Inc(-2);
       
   268 			if (iPodLex.Remainder().Find(KCommandStart) == 0)
       
   269 				{
       
   270 				iPodLex.Inc(KCommandStart().Length());
       
   271 				return KErrNone;
       
   272 				}
       
   273 			else
       
   274 				{
       
   275 				iPodLex.Inc(2);
       
   276 				}
       
   277 			}
       
   278 		}
       
   279 
       
   280 	TInt ret = SkipTo(KCommandStart);
       
   281 	if (ret == KErrNone)
       
   282 		{
       
   283 		iPodLex.Inc(KCommandStart().Length());
       
   284 		}
       
   285 	return ret;
       
   286 	}
       
   287 
       
   288 TInt TPodFormatter::SkipToNextBlankLine()
       
   289 	{
       
   290 	TInt ret = SkipTo(KBlankLine);
       
   291 	if (ret == KErrNone)
       
   292 		{
       
   293 		iPodLex.Inc(2);
       
   294 		}
       
   295 	return ret;
       
   296 	}
       
   297 
       
   298 TInt TPodFormatter::SkipTo(const TDesC& aDes)
       
   299 	{
       
   300 	TInt ret = iPodLex.Remainder().Find(aDes);
       
   301 	if (ret >= 0)
       
   302 		{
       
   303 		iPodLex.Inc(ret);
       
   304 		return KErrNone;
       
   305 		}
       
   306 	return KErrNotFound;
       
   307 	}
       
   308 
       
   309 TBool TPodFormatter::AtCommandStart()
       
   310 	{
       
   311 	return ((iPodLex.Remainder().Find(KCommandStart) == 0) || (iPodLex.Remainder().Find(KPartialCommandStart) == 0));
       
   312 	}
       
   313