From 04fbbacecaf600177730fade2021090251719b71 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 1 Apr 2009 02:31:38 +0000 Subject: [PATCH] added: when the program exits new variables and functions are read from the configuration file and then the file is saved (if you have inserted some variables or functions in a different instance of TTCalc those variables or functions were previously skipped) git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@115 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/mainwindow.cpp | 6 --- src/programresources.cpp | 80 ++++++++++++++++++++++++++++++++-------- src/programresources.h | 7 +++- src/winmain.cpp | 6 ++- 4 files changed, 75 insertions(+), 24 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index deb86b9..b5c8672 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -882,12 +882,6 @@ BOOL WmCommand_NewWindow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) STARTUPINFO si; PROCESS_INFORMATION pi; - GetPrgRes()->SetXPos( GetPrgRes()->GetXPos()+10 ); - GetPrgRes()->SetYPos( GetPrgRes()->GetYPos()+10 ); - GetPrgRes()->SaveToFile(); - GetPrgRes()->SetXPos( GetPrgRes()->GetXPos()-10 ); - GetPrgRes()->SetYPos( GetPrgRes()->GetYPos()-10 ); - char * buffer = new char[MAX_PATH+1]; GetModuleFileName(GetPrgRes()->GetInstance(),buffer, MAX_PATH); diff --git a/src/programresources.cpp b/src/programresources.cpp index b7edf56..1544dfc 100644 --- a/src/programresources.cpp +++ b/src/programresources.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2006-2008, Tomasz Sowa + * Copyright (c) 2006-2009, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -638,6 +638,68 @@ void ProgramResources::CheckCoordinates() +void ProgramResources::AddVariablesFunctions( IniParser::Section & temp_variables, + IniParser::Section & temp_functions, + bool if_not_exist) +{ +IniParser::Section::iterator ic; + + // we're adding variables + + if( !if_not_exist ) + variables.Clear(); + + for( ic = temp_variables.begin() ; ic!=temp_variables.end() ; ++ic ) + if( !if_not_exist || !variables.IsDefined(ic->first) ) + variables.Add(ic->first, ic->second); + + + // we're adding functions + + if( !if_not_exist ) + functions.Clear(); + + for( ic = temp_functions.begin() ; ic!=temp_functions.end() ; ++ic ) + { + const char * name; + int param; + + if( !if_not_exist || !functions.IsDefined(ic->first) ) + if( SplitFunction(ic->second, &name, ¶m) ) + functions.Add(ic->first, name, param); + } +} + + +IniParser::Error ProgramResources::ReadVariablesFunctionsFromFile() +{ +IniParser iparser; +IniParser::Section temp_variables, temp_functions; +std::string ini_value[20]; + + iparser.ConvertValueToSmallLetters(false); + iparser.SectionCaseSensitive(false); + + // we have variables and functions case-sensitive + iparser.PatternCaseSensitive(true); + + iparser.Associate( "variables", &temp_variables ); + iparser.Associate( "functions", &temp_functions ); + + bad_line = -1; + IniParser::Error err = iparser.ReadFromFile( configuration_file.c_str() ); + + if( err == IniParser::err_cant_open_file ) + return err; + + AddVariablesFunctions(temp_variables, temp_functions, true); + + if( err != IniParser::err_ok ) + bad_line = iparser.GetBadLine(); + +return err; +} + IniParser::Error ProgramResources::ReadFromFile() @@ -680,21 +742,7 @@ std::string ini_value[20]; if( err == IniParser::err_cant_open_file ) return err; - // we're adding variables - variables.Clear(); - for( ic = temp_variables.begin() ; ic!=temp_variables.end() ; ++ic ) - variables.Add(ic->first, ic->second); - - // we're adding functions - functions.Clear(); - for( ic = temp_functions.begin() ; ic!=temp_functions.end() ; ++ic ) - { - const char * name; - int param; - - if( SplitFunction(ic->second, &name, ¶m) ) - functions.Add(ic->first, name, param); - } + AddVariablesFunctions(temp_variables, temp_functions, false); // we're setting various values always_on_top = bool( atoi(ini_value[0].c_str()) ); diff --git a/src/programresources.h b/src/programresources.h index 0c3a376..6a16f57 100644 --- a/src/programresources.h +++ b/src/programresources.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2006-2008, Tomasz Sowa + * Copyright (c) 2006-2009, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -265,7 +265,10 @@ public: /*! reading and writing to a file + + ReadVariablesFunctionsFromFile() reads variables/functions only when they are not exist (in this instance of TTCalc) */ + IniParser::Error ReadVariablesFunctionsFromFile(); IniParser::Error ReadFromFile(); void SaveToFile(); @@ -309,6 +312,8 @@ private: bool IsDecDigit(int c); bool SplitFunction(const std::string & input, const char * * name, int * param); void CheckCoordinates(); + void AddVariablesFunctions(IniParser::Section & temp_variables, IniParser::Section & temp_functions, bool if_not_exist); + ttmath::Objects variables; ttmath::Objects functions; diff --git a/src/winmain.cpp b/src/winmain.cpp index 5961da1..f71bb9f 100644 --- a/src/winmain.cpp +++ b/src/winmain.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2006-2008, Tomasz Sowa + * Copyright (c) 2006-2009, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -84,6 +84,10 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) // there's the main loop of messages here MainMessagesLoop(); + // there can be another variables/functions saved by other instantion of TTCalc + // we're reading them now + GetPrgRes()->ReadVariablesFunctionsFromFile(); + GetPrgRes()->SaveToFile(); CloseHandle( (HANDLE)thread_handle );