Problem with stripping white spaces and references!?

  • Thread starter delyan.nestorov
  • Start date
D

delyan.nestorov

Hi All,

I have the following problem:
I read lines from DXF file ( AutoCAD format file ). Then I need to
remove white spaces from lines to continue working on data i.e.
converting from string to int and so on. My StripWhiteSpace function
works in test program:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void StripWhiteSpace( string& Destination )
{
for( int i = 0, j = Destination.length()-1; i<j; i++) {
if( Destination == ' ' || Destination == '\t' ) {
Destination.erase( i, 1 );
j--;
}
}
}

bool StringToInt( string& Source, int *Dest )
{
cout<<"Calling StringToInt with:"<<Source<<endl;

unsigned int i = 0;
bool isNegative = false;

*Dest = 0;

if( Source[0] == '-' ) {
isNegative = true;
i = 1;
}

for( ; i<Source.length(); i++ ) {
if( Source >= '0' && Source <= '9' ) {
if( ( i == 0 && isNegative == false ) || ( i == 1 && isNegative ==
true ) ){
*Dest = Source - '0';
}else{
*Dest = (*Dest)*10 + Source - '0';
}
}else{
return false;
}
}

if( isNegative )
*Dest = (*Dest)*-1;

cout<<"(StringToInt): Source:"<<Source<<" Dest: "<<*Dest<<endl;
return true;
}

int main()
{
ifstream fin("test.dxf");
string a;
while( !fin.eof() ) {
fin>>a;
StripWhiteSpace( a );
cout<<a<<endl;
}

return 0;
}





but doesn't work in my real program ( part of it below )





#ifndef DXFFILEREADER_HPP_
#define DXFFILEREADER_HPP_

#include <string>
#include <fstream>
#include <iostream>
#include "DXFToken.hpp"

using namespace std;

class DXFFileReader
{
private:
ifstream dxfFileStream;
private:
void StripWhiteSpace( string& Destination );
bool StringToInt( string& Source, int *Dest );
bool GetDXFLine( string& Destination );
public:
DXFFileReader( string dxfFileToRead );
bool IsOpened();
bool GetDXFToken( DXFToken *dxfToken );
};

#endif /*DXFFILEREADER_HPP_*/



void DXFFileReader::StripWhiteSpace( string& Destination )
{
int j = Destination.length();
for( int i = 0; i<j; i++) {
if( Destination == ' ' || Destination == '\t' ) {
Destination.erase( i, 1 );
j--;
}
}
cout<<"StripWhiteSpace:"<<Destination<<endl;
}

bool DXFFileReader::StringToInt( string& Source, int *Dest )
{
cout<<"Calling StringToInt with: "<<Source<<endl;
unsigned int i = 0;
bool isNegative = false;

*Dest = 0;

if( Source[0] == '-' ) {
isNegative = true;
i = 1;
}

for( ; i<Source.length(); i++ ) {
if( Source >= '0' && Source <= '9' ) {
if( ( i == 0 && isNegative == false ) || ( i == 1 && isNegative ==
true ) ){
*Dest = Source - '0';
}else{
*Dest = (*Dest)*10 + Source - '0';
}
}else{
cout<<"return false: "<<Source<<" string legth:
"<<Source.length()<<endl;
cout<<"last char: "<<Source[1]<<endl;
return false;
}
}

if( isNegative )
*Dest = (*Dest)*-1;

cout<<"(StringToInt): Source:"<<Source<<" Dest: "<<*Dest<<endl;
return true;
}

bool DXFFileReader::GetDXFLine( string& Destination )
{
if( !dxfFileStream.is_open() || dxfFileStream.eof() )
return false;

getline( dxfFileStream, Destination, '\n');
StripWhiteSpace( Destination );
cout<<"GetDXFLine: "<<Destination<<endl;

return true;
}

bool DXFFileReader::GetDXFToken( DXFToken *dxfToken )
{
string CodeLine, ValueLine;
int Code = 0;

if( GetDXFLine( CodeLine ) && GetDXFLine( ValueLine ) ) {
if( !StringToInt( CodeLine, &Code ) ) {
return false;
}else{
if( Code == 999 ) {
//This is comment :)
} else if( (Code >=0 && Code <= 9) ||
(Code >=300 && Code <= 309) ||
(Code >=1000 && Code <= 1009) ||
Code == 100 ||
Code == 102 ) {
dxfToken->AssignToken( Code, ValueLine );
}else if( (Code >=60 && Code <= 79) ||
(Code >=90 && Code <= 99) ||
(Code >=170 && Code <= 175) ||
(Code >=1060 && Code <= 1071) ||
(Code >=280 && Code <= 289) ) {

int Value = 0;
if( !StringToInt( ValueLine, &Value ) )
return false;

dxfToken->AssignToken( Code, Value );
}else{
return false;
}
}
}else{
return false;
}
return true;
}



the output is as follows:

C:\Documents and Settings\nestorovd\workspace\DXFReader\Debug>"C:
\Documents and Settings\nestorovd\workspace\DXFReader\Debug
\DXFReader.exe"
success openning file
StripWhiteSpace: 0
GetDXFLine: 0
StripWhiteSpace:SECTION
GetDXFLine: SECTION
Calling StringToInt with: 0
return false: string legth: 2
last char: 0



What I am doing wrong? Please help!
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hi All,

I have the following problem:
I read lines from DXF file ( AutoCAD format file ). Then I need to
remove white spaces from lines to continue working on data i.e.
converting from string to int and so on. My StripWhiteSpace function
works in test program:

Can't help you with your code but I can give you something else that
might be usedful, the following code can be used to convert strings to
a number of types (all the native builtin) even if there are spaces:

#include <iostream>
#include <sstream>
#include <string>

/*
* Converts a string to some other type. The type converted to must
* have a std::eek:stream& operator>>(std::eek:stream&, T) method declared.
* Works for all built-in types.
*/
template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstream iss(s);
if (!(iss >> t))
throw std::string("Can't convert '") + s +
"' to '" + typeid(t).name() + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::string& s) {
std::cout << s;
}
return 0;
}

It's my own adoption of the code from the FAQ:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
 
D

delyan.nestorov

Erik Wikstrom íàïèñà:
Can't help you with your code but I can give you something else that
might be usedful, the following code can be used to convert strings to
a number of types (all the native builtin) even if there are spaces:

#include <iostream>
#include <sstream>
#include <string>

/*
* Converts a string to some other type. The type converted to must
* have a std::eek:stream& operator>>(std::eek:stream&, T) method declared.
* Works for all built-in types.
*/
template <class T>
T stoa(const std::string& s)
{
T t;
std::istringstream iss(s);
if (!(iss >> t))
throw std::string("Can't convert '") + s +
"' to '" + typeid(t).name() + "'\n";
return t;
}

int main()
{
std::string s = " -54 ";
try {
int d = stoa<int>(s);
} catch(std::string& s) {
std::cout << s;
}
return 0;
}

It's my own adoption of the code from the FAQ:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2

Hi again,

Thanks Erik this was usefull for me!

And I found my STUPID error:

if( Destination == ' ' || Destination == '\t' ) {
Destination.erase( i, 1 );
j--; i--; <----HERE this "i--" missed
hahaha, horror! Today my head is full with s***s
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top