Fix handling of negative numbers in FString::toLong()

This commit is contained in:
Markus Gans 2017-09-11 22:50:07 +02:00
parent 01a4fb9db7
commit 0ce3868e74
7 changed files with 36 additions and 22 deletions

View File

@ -1,5 +1,6 @@
2017-09-11 Markus Gans <guru.mail@muenster.de> 2017-09-11 Markus Gans <guru.mail@muenster.de>
* Some code improvements * Some code improvements
* Fix handling of negative numbers in FString::toLong()
2017-09-09 Markus Gans <guru.mail@muenster.de> 2017-09-09 Markus Gans <guru.mail@muenster.de>
* Wrong UTF-8 string length fixed when attaching to FString * Wrong UTF-8 string length fixed when attaching to FString

View File

@ -588,24 +588,6 @@ void FFileDialog::init()
readDir(); 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<char*>("");
else
{
if ( getpwnam_r (pwd.pw_name, &pwd, buf, sizeof(buf), &pwd_ptr) )
return const_cast<char*>("");
else
return pwd.pw_dir;
}
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline bool FFileDialog::pattern_match ( const char* const pattern inline bool FFileDialog::pattern_match ( const char* const pattern
, char*& fname ) , char*& fname )
@ -754,6 +736,19 @@ void FFileDialog::printPath (const FString& txt)
filebrowser->setText(path); 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) void FFileDialog::cb_processActivate (FWidget*, data_ptr)
{ {

View File

@ -130,12 +130,12 @@ class FFileDialog : public FDialog
// Method // Method
void init(); void init();
static char* getHomeDir();
inline bool pattern_match (const char* const, char*&); inline bool pattern_match (const char* const, char*&);
void clear(); void clear();
int numOfDirs(); int numOfDirs();
int changeDir (const FString&); int changeDir (const FString&);
void printPath (const FString&); void printPath (const FString&);
static const FString getHomeDir();
// Callback methods // Callback methods
void cb_processActivate (FWidget*, data_ptr); void cb_processActivate (FWidget*, data_ptr);

View File

@ -682,12 +682,14 @@ uInt FString::toUInt() const
//---------------------------------------------------------------------- //----------------------------------------------------------------------
long FString::toLong() const long FString::toLong() const
{ {
register bool neg;
register long num; register long num;
register long tenth_limit; register long tenth_limit;
register long tenth_limit_digit; register long tenth_limit_digit;
register wchar_t* p; register wchar_t* p;
FString s; FString s;
neg = false;
num = 0; num = 0;
tenth_limit = LONG_MAX / 10; tenth_limit = LONG_MAX / 10;
tenth_limit_digit = LONG_MAX % 10; tenth_limit_digit = LONG_MAX % 10;
@ -703,6 +705,7 @@ long FString::toLong() const
if ( *p == L'-' ) if ( *p == L'-' )
{ {
p++; p++;
neg = true;
tenth_limit = -(LONG_MIN / 10); tenth_limit = -(LONG_MIN / 10);
tenth_limit_digit += 1; tenth_limit_digit += 1;
} }
@ -721,13 +724,16 @@ long FString::toLong() const
throw std::overflow_error ("overflow"); throw std::overflow_error ("overflow");
} }
num = (num<<3)+(num<<1) + d; // (10 * num) + d num = (num << 3) + (num << 1) + d; // (10 * num) + d
p++; p++;
} }
if ( *p != L'\0' && ! std::iswdigit(wint_t(*p)) ) if ( *p != L'\0' && ! std::iswdigit(wint_t(*p)) )
throw std::invalid_argument ("no valid number"); throw std::invalid_argument ("no valid number");
if ( neg )
num = (~num) + 1;
return num; 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()); tmp._insert (1, uInt(s.length()), s.c_str());
return (tmp); 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);
}

View File

@ -152,6 +152,7 @@ class FString
friend const FString operator + (const wchar_t, const FString&); friend const FString operator + (const wchar_t, const FString&);
friend const FString operator + (const char, 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 wchar_t, const std::wstring&);
friend const FString operator + (const FString&, const char);
// inquiries // inquiries
bool isNull() const; bool isNull() const;

View File

@ -3699,7 +3699,7 @@ void FTerm::init()
std::abort(); std::abort();
// Get pathname of the terminal device // 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'; term_name[0] = '\0';
#if defined(__linux__) #if defined(__linux__)

View File

@ -533,7 +533,7 @@ MyDialog::MyDialog (FWidget* parent)
FLabel* sum_count = new FLabel (this); FLabel* sum_count = new FLabel (this);
sum_count->setGeometry(29, 5, 5, 3); sum_count->setGeometry(29, 5, 5, 3);
sum_count->setNumber (myList->getCount()); sum_count->setNumber (long(myList->getCount()));
// Statusbar at the bottom // Statusbar at the bottom
FStatusBar* statusbar = new FStatusBar (this); FStatusBar* statusbar = new FStatusBar (this);