ttcalc/src/parsermanager.cpp

205 lines
5.1 KiB
C++

/*
* This file is a part of TTCalc - a mathematical calculator
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
*/
/*
* Copyright (c) 2006-2007, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "compileconfig.h"
#include "parsermanager.h"
ParserManager::ParserManager() : buffer_len(2048)
{
buffer = 0;
base_input = base_output = 10;
always_scientific = 0;
when_scientific = 15;
rounding = -2;
}
ParserManager::ParserManager(const ParserManager & p) : buffer_len(p.buffer_len)
{
buffer = 0;
base_input = p.base_input;
base_output = p.base_output;
always_scientific = p.always_scientific;
when_scientific = p.when_scientific;
rounding = p.rounding;
}
ParserManager & ParserManager::operator=(const ParserManager & p)
{
buffer = 0;
return *this;
}
ParserManager::~ParserManager()
{
delete [] buffer;
}
ttmath::ErrorCode ParserManager::GetLastCode()
{
return code;
}
ttmath::ErrorCode ParserManager::Parse()
{
try
{
switch( precision )
{
case 0:
parser1.SetBase(base_input);
code = parser1.Parse(buffer);
break;
case 1:
parser2.SetBase(base_input);
code = parser2.Parse(buffer);
break;
default:
parser3.SetBase(base_input);
code = parser3.Parse(buffer);
break;
}
}
catch(...)
{
// we are in a thread, we shouldn't go up
code = ttmath::err_internal_error;
}
return code;
}
void ParserManager::MakeCopyOfVariables()
{
unsigned int i;
char * pchar = (char*)GetPrgRes()->GetBuffer();
for(i = 0 ; i<buffer_len-1 && pchar[i]!=0 ; ++i)
buffer[i] = pchar[i];
buffer[i] = 0;
//
variables = *GetPrgRes()->GetVariables();
functions = *GetPrgRes()->GetFunctions();
//
base_input = GetPrgRes()->GetBaseInput();
base_output = GetPrgRes()->GetBaseOutput();
always_scientific = GetPrgRes()->GetDisplayAlwaysScientific();
when_scientific = GetPrgRes()->GetDisplayWhenScientific();
rounding = GetPrgRes()->GetDisplayRounding();
precision = GetPrgRes()->GetPrecision();
country = GetPrgRes()->GetLanguages()->GetCurrentLanguage();
}
void ParserManager::Init()
{
buffer = new char[buffer_len];
buffer[0] = 0;
parser1.SetStopObject( GetPrgRes()->GetThreadController()->GetStopObject() );
parser1.SetVariables( &variables );
parser1.SetFunctions( &functions );
parser2.SetStopObject( GetPrgRes()->GetThreadController()->GetStopObject() );
parser2.SetVariables( &variables );
parser2.SetFunctions( &functions );
parser3.SetStopObject( GetPrgRes()->GetThreadController()->GetStopObject() );
parser3.SetVariables( &variables );
parser3.SetFunctions( &functions );
}
void ParserManager::PrintResult()
{
if( code == ttmath::err_ok )
{
switch( precision )
{
case 0:
return PrintResult(parser1);
case 1:
return PrintResult(parser2);
default:
return PrintResult(parser3);
}
}
else
{
// we can call GetPrgRes() here because we're reading
// static value like GetMainWindow() which are set only once
// at the beginning of the program and will never be changed later
// by the first thread
// note that we're using a long form of a method ErrorMessage i.g.
// ErrorMessage(Country country, ttmath::ErrorCode code)
// and not ErrorMessage(ttmath::ErrorCode code)
// because someone can change the country when we're calling this method
// (in the main thread) and the latter function would be using its own
// 'current_country' value which could be inconsistent
// now, displying an error message is making in the main windows
// (after pressing a special key)
/*
SetDlgItemText(
GetPrgRes()->GetMainWindow(),
IDC_OUTPUT_EDIT,
GetPrgRes()->GetLanguages()->ErrorMessage(country, code) );
*/
}
}