From 0ce3868e74f73e8d46f21f8ad07db2041895224e Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Mon, 11 Sep 2017 22:50:07 +0200 Subject: [PATCH] Fix handling of negative numbers in FString::toLong() --- ChangeLog | 1 + src/ffiledialog.cpp | 31 +++++++++++++------------------ src/ffiledialog.h | 2 +- src/fstring.cpp | 19 ++++++++++++++++++- src/fstring.h | 1 + src/fterm.cpp | 2 +- test/ui.cpp | 2 +- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4a8117b1..46bc95f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 2017-09-11 Markus Gans * Some code improvements + * Fix handling of negative numbers in FString::toLong() 2017-09-09 Markus Gans * Wrong UTF-8 string length fixed when attaching to FString diff --git a/src/ffiledialog.cpp b/src/ffiledialog.cpp index b7108a38..c54287c5 100644 --- a/src/ffiledialog.cpp +++ b/src/ffiledialog.cpp @@ -588,24 +588,6 @@ void FFileDialog::init() readDir(); } -//---------------------------------------------------------------------- -char* FFileDialog::getHomeDir() -{ - struct passwd pwd; - struct passwd* pwd_ptr; - char buf[1024]; - - if ( getpwuid_r (geteuid(), &pwd, buf, sizeof(buf), &pwd_ptr) ) - return const_cast(""); - else - { - if ( getpwnam_r (pwd.pw_name, &pwd, buf, sizeof(buf), &pwd_ptr) ) - return const_cast(""); - else - return pwd.pw_dir; - } -} - //---------------------------------------------------------------------- inline bool FFileDialog::pattern_match ( const char* const pattern , char*& fname ) @@ -754,6 +736,19 @@ void FFileDialog::printPath (const FString& txt) filebrowser->setText(path); } +//---------------------------------------------------------------------- +const FString FFileDialog::getHomeDir() +{ + struct passwd pwd; + struct passwd* pwd_ptr; + char buf[1024]; + + if ( getpwuid_r (geteuid(), &pwd, buf, sizeof(buf), &pwd_ptr) ) + return FString(""); + else + return FString(pwd.pw_dir); +} + //---------------------------------------------------------------------- void FFileDialog::cb_processActivate (FWidget*, data_ptr) { diff --git a/src/ffiledialog.h b/src/ffiledialog.h index 16e6c78a..7abbecda 100644 --- a/src/ffiledialog.h +++ b/src/ffiledialog.h @@ -130,12 +130,12 @@ class FFileDialog : public FDialog // Method void init(); - static char* getHomeDir(); inline bool pattern_match (const char* const, char*&); void clear(); int numOfDirs(); int changeDir (const FString&); void printPath (const FString&); + static const FString getHomeDir(); // Callback methods void cb_processActivate (FWidget*, data_ptr); diff --git a/src/fstring.cpp b/src/fstring.cpp index d44fe0ec..8cd2deda 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -682,12 +682,14 @@ uInt FString::toUInt() const //---------------------------------------------------------------------- long FString::toLong() const { + register bool neg; register long num; register long tenth_limit; register long tenth_limit_digit; register wchar_t* p; FString s; + neg = false; num = 0; tenth_limit = LONG_MAX / 10; tenth_limit_digit = LONG_MAX % 10; @@ -703,6 +705,7 @@ long FString::toLong() const if ( *p == L'-' ) { p++; + neg = true; tenth_limit = -(LONG_MIN / 10); tenth_limit_digit += 1; } @@ -721,13 +724,16 @@ long FString::toLong() const throw std::overflow_error ("overflow"); } - num = (num<<3)+(num<<1) + d; // (10 * num) + d + num = (num << 3) + (num << 1) + d; // (10 * num) + d p++; } if ( *p != L'\0' && ! std::iswdigit(wint_t(*p)) ) throw std::invalid_argument ("no valid number"); + if ( neg ) + num = (~num) + 1; + return num; } @@ -2654,3 +2660,14 @@ const FString operator + (const wchar_t c, const std::wstring& s) tmp._insert (1, uInt(s.length()), s.c_str()); return (tmp); } + +//---------------------------------------------------------------------- +const FString operator + (const FString& s, const char c) +{ + FString tmp1(s); + wchar_t tmp2[2]; + tmp2[0] = wchar_t(c & 0xff); + tmp2[1] = L'\0'; + tmp1._insert (s.length, 1, tmp2); + return (tmp1); +} diff --git a/src/fstring.h b/src/fstring.h index d2d0e5d6..45a44aff 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -152,6 +152,7 @@ class FString friend const FString operator + (const wchar_t, const FString&); friend const FString operator + (const char, const FString&); friend const FString operator + (const wchar_t, const std::wstring&); + friend const FString operator + (const FString&, const char); // inquiries bool isNull() const; diff --git a/src/fterm.cpp b/src/fterm.cpp index b3aa548c..b580c171 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -3699,7 +3699,7 @@ void FTerm::init() std::abort(); // Get pathname of the terminal device - if ( ! ttyname_r (stdout_no, term_name, sizeof(term_name)) ) + if ( ttyname_r(stdout_no, term_name, sizeof(term_name)) ) term_name[0] = '\0'; #if defined(__linux__) diff --git a/test/ui.cpp b/test/ui.cpp index e91b08fc..65ad0627 100644 --- a/test/ui.cpp +++ b/test/ui.cpp @@ -533,7 +533,7 @@ MyDialog::MyDialog (FWidget* parent) FLabel* sum_count = new FLabel (this); sum_count->setGeometry(29, 5, 5, 3); - sum_count->setNumber (myList->getCount()); + sum_count->setNumber (long(myList->getCount())); // Statusbar at the bottom FStatusBar* statusbar = new FStatusBar (this);