www.pudn.com > TurboPadSource.tar.gz > template.cpp
/**
* \file Code Template Implementation
*
* Implementation of CodeTemplate functions.
*/
#include "template.h"
void CodeTemplate::AlignTemplate(wxString &templ, unsigned int spaces)
{
wxString alignString = "\n";
while (alignString.Length() <= spaces)
alignString += " ";
templ.Replace("\n", alignString.c_str(), true);
}
int CodeTemplate::FindInsertionPoint(wxString &templ)
{
wxString code = templ;
int insPoint = -1;
int rep = 0;
int depth = 0; // Holds the number of "||" strings prior to the insertion point
for (;;)
{
insPoint = code.Find('|');
if (insPoint == -1) return -1;
else if (code[insPoint+1] == '|')
{
code = code(insPoint+2, code.Length()-(insPoint+2));
depth++;
}
else break;
}
// If the cursor position was found, remove the | character
templ.Remove((templ.Length()-code.Length())+insPoint, 1);
if (templ.Contains("||"))
rep = templ.Replace("||", "|", true);
return ((templ.Length()-code.Length())+insPoint)+(rep-depth)+1; // Add 1 because | was removed
}
wxString CodeTemplate::GetTemplate(const wxString &fileName, const wxString &tName)
{
int tLine = GetTemplateLine(fileName, tName);
wxString code;
if (tLine == -1) return "";
tFile.Open(fileName);
for (int i = tLine; (unsigned int) i < tFile.GetLineCount(); i++)
{
code += tFile.GetLine(i) + '\n';
if ((unsigned int) i+1 < tFile.GetLineCount() && IsHeaderLine(tFile.GetLine(i+1))) break;
}
RemoveTrailingNewlines(code);
tFile.Close();
if (code.Contains("[["))
code.Replace("[[", "[", true);
return code;
}
int CodeTemplate::GetTemplateLine(const wxString &fileName, const wxString &tName)
{
wxString line;
tFile.Open(fileName);
for (int i = 0; (unsigned int) i < tFile.GetLineCount(); i++)
{
line = tFile.GetLine(i);
if (line.Contains('[') && IsHeaderLine(line))
{
if (tName == TemplateNameFromHeader(line))
{
tFile.Close();
return i+1; // Return the first line of the actual template code
}
}
}
tFile.Close();
return -1;
}
bool CodeTemplate::HasTemplate(const wxString &fileName, const wxString &tName)
{
wxString line;
tFile.Open(fileName);
for (int i = 0; (unsigned int) i < tFile.GetLineCount(); i++)
{
line = tFile.GetLine(i);
if (line.Contains('[') && IsHeaderLine(line))
{
if (tName == TemplateNameFromHeader(line))
{
tFile.Close();
return true;
}
}
}
tFile.Close();
return false;
}
bool CodeTemplate::IsHeaderLine(wxString line)
{
RemoveWhitespace(line);
return (line[0] == '[') && (line[1] != '[');
}
inline void CodeTemplate::RemoveTrailingNewlines(wxString &string)
{
while (string.Last() == '\n')
string.RemoveLast();
}
inline void CodeTemplate::RemoveWhitespace(wxString &string)
{
string.Trim(true);
string.Trim(false);
}
wxString CodeTemplate::TemplateNameFromHeader(const wxString &header)
{
wxString name = header.BeforeLast('|');
name = name.AfterFirst('[');
RemoveWhitespace(name);
return name;
}