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!
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!