Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Conversion : string to any-type (?!)

7 views
Skip to first unread message

Alex Vinokur

unread,
Sep 13, 1999, 3:00:00 AM9/13/99
to
Hi,

Here is template function str_to_any.
I would like to hope it converts string to any type.

Thanks,
Alex

//#########################################################
//------------------- C++ code : BEGIN -------------------
#include <iostream.h>
#include <fstream.h>
#include <strstream.h>
#include <iomanip.h>
#include <assert.h>
#include <string>
#include <vector>

//====================================================
class AAA // test class
{
friend istream& operator>> (istream& istr_io, AAA& inst_o);
friend ostream& operator<< (ostream& ostr_io, const AAA& inst_i);
public :
string value1_;
int value2_;
};

istream& operator>> (istream& istr_io, AAA& inst_o)
{
istr_io >> inst_o.value1_;
istr_io >> inst_o.value2_;
return istr_io;
}

ostream& operator<< (ostream& ostr_io, const AAA& inst_i)
{
ostr_io << inst_i.value1_ << "#" << inst_i.value2_;
return ostr_io;
}

//====================================================
class BBB // test class
{
friend istream& operator>> (istream& istr_io, BBB& inst_o);
friend ostream& operator<< (ostream& ostr_io, const BBB& inst_i);
public :
int value1_;
string value2_;
};

istream& operator>> (istream& istr_io, BBB& inst_o)
{
istr_io >> inst_o.value1_;
istr_io >> inst_o.value2_;
return istr_io;
}

ostream& operator<< (ostream& ostr_io, const BBB& inst_i)
{
ostr_io << inst_i.value1_ << "#" << inst_i.value2_;
return ostr_io;
}

//====================================================
//==================== str_to_any ====================
//====================================================
template <class T1>
bool str_to_any (T1& tvalue_o, const string& string_i)
{
bool ret_booValue;
istrstream tmp_istrstream (string_i.c_str ());

tmp_istrstream >> tvalue_o;
if (tmp_istrstream.eof ())
{
ret_booValue = !tmp_istrstream.fail ();
}
else
{
string dummy;
while (!tmp_istrstream.eof () & !tmp_istrstream.fail ())
{
tmp_istrstream >> dummy;
}

ret_booValue = (
(tmp_istrstream.eof ()) ?
(tmp_istrstream.fail ()) :
(!tmp_istrstream.fail ())
);
}

tmp_istrstream.rdbuf()->freeze (0);
return ret_booValue;
}
//====================================================
//====================================================
//====================================================


#define OK_MSG(type) \
cout.setf (ios::left, ios::adjustfield); \
work_str = "<" + the_str + ">"; \
cout << "string " \
<< setw (25) \
<< work_str.c_str () \
<< " to " \
<< #type \
<< " : OK -> result = " \
<< type##_result \
<< " [" \
<< __FILE__ \
<< ",#" \
<< __LINE__ \
<< "]" \
<< endl;

#define FAILURE_MSG(type) \
cout.setf (ios::left, ios::adjustfield); \
work_str = "<" + the_str + ">"; \
cout << "string " \
<< setw (25) \
<< work_str.c_str () \
<< " to " \
<< #type \
<< " : FAILURE" \
<< " [" \
<< __FILE__ \
<< ",#" \
<< __LINE__ \
<< "]" \
<< endl;

//====================================================
int main ()
{
char char_result;
int int_result;
AAA AAA_result;
BBB BBB_result;
string the_str;
string work_str;

//======================================
vector<string> char_string;
char_string.push_back ("");
char_string.push_back ("x");
char_string.push_back ("xy");
char_string.push_back ("xyz");
char_string.push_back ("x yz");
char_string.push_back ("xy z");

cout << endl;
for (unsigned int i = 0; i < char_string.size (); i++)
{
the_str = char_string [i];
if (str_to_any (char_result, the_str))
{
OK_MSG (char);
}
else
{
FAILURE_MSG (char);
}
}


//======================================
vector<string> int_string;
int_string.push_back ("");
int_string.push_back ("x");
int_string.push_back ("xy");
int_string.push_back ("xyz");
int_string.push_back ("x yz");
int_string.push_back ("xy z");
int_string.push_back ("1");
int_string.push_back ("12");
int_string.push_back ("123");
int_string.push_back (" 123");
int_string.push_back ("1 23");
int_string.push_back ("12 3");
int_string.push_back ("+123");
int_string.push_back ("-123");
int_string.push_back ("--123");
int_string.push_back ("#123");

cout << endl;
for (unsigned int i = 0; i < int_string.size (); i++)
{
the_str = int_string [i];
if (str_to_any (int_result, the_str))
{
OK_MSG (int);
}
else
{
FAILURE_MSG (int);
}
}


//======================================
vector<string> AAA_string;
AAA_string.push_back ("");
AAA_string.push_back ("x");
AAA_string.push_back ("xy");
AAA_string.push_back ("xyz");
AAA_string.push_back ("x yz");
AAA_string.push_back ("xy z");
AAA_string.push_back ("xyz");
AAA_string.push_back ("123");
AAA_string.push_back ("+123");
AAA_string.push_back ("-123");
AAA_string.push_back ("--123");
AAA_string.push_back ("#123");
AAA_string.push_back (" 123");
AAA_string.push_back ("xyz 123");
AAA_string.push_back ("xyz 123 ");
AAA_string.push_back (" xyz 123");
AAA_string.push_back ("xyz 123 345");
AAA_string.push_back ("xyz 123 345 678");
AAA_string.push_back ("xyz prt 123");
AAA_string.push_back ("xyz prt 123 345");
AAA_string.push_back ("xyz prt 123 345 678");
AAA_string.push_back ("xyz prt");
AAA_string.push_back ("xyz prt 345");
AAA_string.push_back ("xyz prt 345 678");
AAA_string.push_back ("123 uvw");
AAA_string.push_back (" 123 uvw");
AAA_string.push_back ("123 uvw ");
AAA_string.push_back ("123 uvw klm");
AAA_string.push_back ("123 prt uvw klm");
AAA_string.push_back ("123 prt uvw klm ghf");

cout << endl;
for (unsigned int i = 0; i < AAA_string.size (); i++)
{
the_str = AAA_string [i];
if (str_to_any (AAA_result, the_str))
{
OK_MSG (AAA);
}
else
{
FAILURE_MSG (AAA);
}
}

//======================================
vector<string> BBB_string;
BBB_string.push_back ("");
BBB_string.push_back ("x");
BBB_string.push_back ("xy");
BBB_string.push_back ("xyz");
BBB_string.push_back ("x yz");
BBB_string.push_back ("xy z");
BBB_string.push_back ("xyz");
BBB_string.push_back ("123");
BBB_string.push_back ("123 ");
BBB_string.push_back (" 123");
BBB_string.push_back ("+123");
BBB_string.push_back ("-123");
BBB_string.push_back ("--123");
BBB_string.push_back ("#123");
BBB_string.push_back ("xyz 123");
BBB_string.push_back ("xyz 123 ");
BBB_string.push_back (" xyz 123");
BBB_string.push_back ("xyz 123 345");
BBB_string.push_back ("xyz 123 345 678");
BBB_string.push_back ("xyz prt 123");
BBB_string.push_back ("xyz prt 123 345");
BBB_string.push_back ("xyz prt 123 345 678");
BBB_string.push_back ("xyz prt");
BBB_string.push_back ("xyz prt 345");
BBB_string.push_back ("xyz prt 345 678");
BBB_string.push_back ("123 uvw");
BBB_string.push_back (" 123 uvw");
BBB_string.push_back ("123 uvw ");
BBB_string.push_back ("123 uvw klm");
BBB_string.push_back ("123 uvw klm ghf");

cout << endl;
for (unsigned int i = 0; i < BBB_string.size (); i++)
{
the_str = BBB_string [i];
if (str_to_any (BBB_result, the_str))
{
OK_MSG (BBB);
}
else
{
FAILURE_MSG (BBB);
}
}


return 0;
}

//------------------- C++ code : END ----------------------

//#########################################################
//------------------- Running Results : BEGIN -------------

string <> to char : FAILURE [bbb.C,#149]
string <x> to char : OK -> result = x
[bbb.C,#145]
string <xy> to char : FAILURE [bbb.C,#149]
string <xyz> to char : FAILURE [bbb.C,#149]
string <x yz> to char : FAILURE [bbb.C,#149]
string <xy z> to char : FAILURE [bbb.C,#149]

string <> to int : FAILURE [bbb.C,#183]
string <x> to int : FAILURE [bbb.C,#183]
string <xy> to int : FAILURE [bbb.C,#183]
string <xyz> to int : FAILURE [bbb.C,#183]
string <x yz> to int : FAILURE [bbb.C,#183]
string <xy z> to int : FAILURE [bbb.C,#183]
string <1> to int : OK -> result = 1
[bbb.C,#179]
string <12> to int : OK -> result = 12
[bbb.C,#179]
string <123> to int : OK -> result = 123
[bbb.C,#179]
string < 123> to int : OK -> result = 123
[bbb.C,#179]
string <1 23> to int : FAILURE [bbb.C,#183]
string <12 3> to int : FAILURE [bbb.C,#183]
string <+123> to int : OK -> result = 123
[bbb.C,#179]
string <-123> to int : OK -> result = -123
[bbb.C,#179]
string <--123> to int : FAILURE [bbb.C,#183]
string <#123> to int : FAILURE [bbb.C,#183]

string <> to AAA : FAILURE [bbb.C,#231]
string <x> to AAA : FAILURE [bbb.C,#231]
string <xy> to AAA : FAILURE [bbb.C,#231]
string <xyz> to AAA : FAILURE [bbb.C,#231]
string <x yz> to AAA : FAILURE [bbb.C,#231]
string <xy z> to AAA : FAILURE [bbb.C,#231]
string <xyz> to AAA : FAILURE [bbb.C,#231]
string <123> to AAA : FAILURE [bbb.C,#231]
string <+123> to AAA : FAILURE [bbb.C,#231]
string <-123> to AAA : FAILURE [bbb.C,#231]
string <--123> to AAA : FAILURE [bbb.C,#231]
string <#123> to AAA : FAILURE [bbb.C,#231]
string < 123> to AAA : FAILURE [bbb.C,#231]
string <xyz 123> to AAA : OK -> result = xyz#123
[bbb.C,#227]
string <xyz 123 > to AAA : OK -> result = xyz#123
[bbb.C,#227]
string < xyz 123> to AAA : OK -> result = xyz#123
[bbb.C,#227]
string <xyz 123 345> to AAA : FAILURE [bbb.C,#231]
string <xyz 123 345 678> to AAA : FAILURE [bbb.C,#231]
string <xyz prt 123> to AAA : FAILURE [bbb.C,#231]
string <xyz prt 123 345> to AAA : FAILURE [bbb.C,#231]
string <xyz prt 123 345 678> to AAA : FAILURE [bbb.C,#231]
string <xyz prt> to AAA : FAILURE [bbb.C,#231]
string <xyz prt 345> to AAA : FAILURE [bbb.C,#231]
string <xyz prt 345 678> to AAA : FAILURE [bbb.C,#231]
string <123 uvw> to AAA : FAILURE [bbb.C,#231]
string < 123 uvw> to AAA : FAILURE [bbb.C,#231]
string <123 uvw > to AAA : FAILURE [bbb.C,#231]
string <123 uvw klm> to AAA : FAILURE [bbb.C,#231]
string <123 prt uvw klm> to AAA : FAILURE [bbb.C,#231]
string <123 prt uvw klm ghf> to AAA : FAILURE [bbb.C,#231]

string <> to BBB : FAILURE [bbb.C,#280]
string <x> to BBB : FAILURE [bbb.C,#280]
string <xy> to BBB : FAILURE [bbb.C,#280]
string <xyz> to BBB : FAILURE [bbb.C,#280]
string <x yz> to BBB : FAILURE [bbb.C,#280]
string <xy z> to BBB : FAILURE [bbb.C,#280]
string <xyz> to BBB : FAILURE [bbb.C,#280]
string <123> to BBB : FAILURE [bbb.C,#280]
string <123 > to BBB : FAILURE [bbb.C,#280]
string < 123> to BBB : FAILURE [bbb.C,#280]
string <+123> to BBB : FAILURE [bbb.C,#280]
string <-123> to BBB : FAILURE [bbb.C,#280]
string <--123> to BBB : FAILURE [bbb.C,#280]
string <#123> to BBB : FAILURE [bbb.C,#280]
string <xyz 123> to BBB : FAILURE [bbb.C,#280]
string <xyz 123 > to BBB : FAILURE [bbb.C,#280]
string < xyz 123> to BBB : FAILURE [bbb.C,#280]
string <xyz 123 345> to BBB : FAILURE [bbb.C,#280]
string <xyz 123 345 678> to BBB : FAILURE [bbb.C,#280]
string <xyz prt 123> to BBB : FAILURE [bbb.C,#280]
string <xyz prt 123 345> to BBB : FAILURE [bbb.C,#280]
string <xyz prt 123 345 678> to BBB : FAILURE [bbb.C,#280]
string <xyz prt> to BBB : FAILURE [bbb.C,#280]
string <xyz prt 345> to BBB : FAILURE [bbb.C,#280]
string <xyz prt 345 678> to BBB : FAILURE [bbb.C,#280]
string <123 uvw> to BBB : OK -> result = 123#uvw
[bbb.C,#276]
string < 123 uvw> to BBB : OK -> result = 123#uvw
[bbb.C,#276]
string <123 uvw > to BBB : OK -> result = 123#uvw
[bbb.C,#276]
string <123 uvw klm> to BBB : FAILURE [bbb.C,#280]
string <123 uvw klm ghf> to BBB : FAILURE [bbb.C,#280]

//------------------- Running Results : END ---------------


//#########################################################
//------------------- Compiler & System ------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -sr : SunOS 5.6

//---------------------------------------------------------

//#########################################################


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

B. v Ingen Schenau

unread,
Sep 13, 1999, 3:00:00 AM9/13/99
to
On Mon, 13 Sep 1999 13:49:42 GMT, Alex Vinokur
<alexande...@telrad.co.il> wrote:

>Hi,
>
>Here is template function str_to_any.
>I would like to hope it converts string to any type.
>
> Thanks,
> Alex

<snip - lengthy code >

Please tell us:
1. What does the program do.
2. What do you want the program to do.
3. How do 1 and 2 differ.

Bart v Ingen Schenau


Mumit Khan

unread,
Sep 13, 1999, 3:00:00 AM9/13/99
to
In article <37df0d02...@news.student.utwente.nl>,

B. v Ingen Schenau <b.vaning...@student.utwente.nl> wrote:
>On Mon, 13 Sep 1999 13:49:42 GMT, Alex Vinokur
><alexande...@telrad.co.il> wrote:
>
>>Hi,
>>
>>Here is template function str_to_any.
>>I would like to hope it converts string to any type.
>>
>> Thanks,
>> Alex
>
><snip - lengthy code >
>
>Please tell us:
>1. What does the program do.
>2. What do you want the program to do.
>3. How do 1 and 2 differ.
>

Before you get to these questions, answer one for me please -- why
in the world do you keep sending these to gnu.g++.help!

Please, please only use appropriate forums.

Regards,
Mumit


Paul Lutus

unread,
Sep 13, 1999, 3:00:00 AM9/13/99
to
> Hi,
>
> Here is template function str_to_any.
> I would like to hope it converts string to any type.
>
> Thanks,
> Alex
>

Here's what you have to do:

1. Tell us what you hoped your program would do.
2. Tell us what your program did instead.
3. Tell us how (2) differed from (1).

None of this information is in your message.

--

Paul Lutus
www.arachnoid.com


Alex Vinokur <alexande...@telrad.co.il> wrote in message
news:7rivdh$kc2$1...@nnrp1.deja.com...

<snip>


Pablo

unread,
Sep 13, 1999, 3:00:00 AM9/13/99
to
You shouldn't be mixing old and new style C++ headers in your program. It's
just asking for trouble. Many compiler vendors are using the old style
headers for computability with previous releases and mixing new and old
style headers is usually recommended against in the documentation and can
cause *lots* of difficult to resolve problems.

-Pablo

Alex Vinokur wrote in message <7rivdh$kc2$1...@nnrp1.deja.com>...


>Hi,
>
>Here is template function str_to_any.
>I would like to hope it converts string to any type.
>
> Thanks,
> Alex
>
>//#########################################################
>//------------------- C++ code : BEGIN -------------------
>#include <iostream.h>
>#include <fstream.h>
>#include <strstream.h>
>#include <iomanip.h>
>#include <assert.h>
>#include <string>
>#include <vector>

you don't.


Alex Vinokur

unread,
Sep 14, 1999, 3:00:00 AM9/14/99
to
In article <37df0d02...@news.student.utwente.nl>,

b.vaning...@student.utwente.nl (B. v Ingen Schenau) wrote:
> On Mon, 13 Sep 1999 13:49:42 GMT, Alex Vinokur
> <alexande...@telrad.co.il> wrote:
>
> >Hi,
> >
> >Here is template function str_to_any.
> >I would like to hope it converts string to any type.
> >
> > Thanks,
> > Alex
>
> <snip - lengthy code >
>
> Please tell us:
> 1. What does the program do.
> 2. What do you want the program to do.
> 3. How do 1 and 2 differ.

I hope (2) does not differ from (1) - Please see below.

>
> Bart v Ingen Schenau
>
>


Paul Lutus wrote :

> > Hi,
> >
> > Here is template function str_to_any.
> > I would like to hope it converts string to any type.
> >
> > Thanks,
> > Alex
> >
>

> Here's what you have to do:
>
> 1. Tell us what you hoped your program would do.
> 2. Tell us what your program did instead.
> 3. Tell us how (2) differed from (1).

[snip]

I hope (2) does not differ from (1) - Please see below.


//====================================================
template <typename T>
bool str_to_any (T& tvalue_o, const string& string_i);
//====================================================

I want the str_to_any function
- to convert string_i to any (built-in or user-defined) type - if it
is possible,
- to recognaize situation when it is not possible.

My original message
http://www.deja.com/=dnc/[ST_rn=ps]/getdoc.xp?AN=524442361
contains definition of this function.

Upon successful completion, str_to_any returns true,
and tvalue_o is converted value.
Upon unsuccessful completion, str_to_any returns false.

---------------------------------------------------------
Examples.
1. convert string to int (i.e. like atoi)
string_i = "135" -> return_value = true, tvalue_o = 135
string_i = "135 789" -> return_value = false
string_i = "xyz" -> return_value = false

2. convert string to char
string_i = "x" -> return_value = true, tvalue_o = 'x'
string_i = "xy" -> return_value = false

3. convert string to class AAA
class AAA
{
int value1_;
string value2_;
};
string_i = "357 apz" -> return_value = true,
tvalue_o = AAA-instance <357, apz>
string_i = "apz 357" -> return_value = false
string_i = "357 apz 711" -> return_value = false
string_i = "357 apz 711 mlk" -> return_value = false
---------------------------------------------------------

My original message also contains some tests
(see main program).

The tests were successful,
so I hope the function does what I want it to do.

But I'm not sure that the function always does
what I want it to do,
because only limited set of the tests was tested.


Thanks,
Alex

Paul Lutus

unread,
Sep 14, 1999, 3:00:00 AM9/14/99
to
> But I'm not sure that the function always does
> what I want it to do,
> because only limited set of the tests was tested.

So do some more tests. Do you realize what you are asking?

There is a basic theorem in computer science called the "Halting problem."
It asks whether it is possible to determine, for an arbitrary computer
program above a certain minimum complexity level, whether the program will
halt (in other words, do what is expected).

It is now known that this question cannot be answered with certainly. This
doesn't mean "more research may provide a different outcome." The question
cannot be answered.

Design the algorithm as best you can. Test all the conditions your program
is expected to encounter. That is all one can do. This is a built-in limit
of logic and computer science.

--

Paul Lutus
www.arachnoid.com


Alex Vinokur <alexande...@telrad.co.il> wrote in message

news:7rl79t$9h1$1...@nnrp1.deja.com...


> In article <37df0d02...@news.student.utwente.nl>,
> b.vaning...@student.utwente.nl (B. v Ingen Schenau) wrote:
> > On Mon, 13 Sep 1999 13:49:42 GMT, Alex Vinokur
> > <alexande...@telrad.co.il> wrote:
> >

> > >Hi,
> > >
> > >Here is template function str_to_any.
> > >I would like to hope it converts string to any type.
> > >
> > > Thanks,
> > > Alex
> >

> > <snip - lengthy code >
> >
> > Please tell us:
> > 1. What does the program do.
> > 2. What do you want the program to do.
> > 3. How do 1 and 2 differ.
>
> I hope (2) does not differ from (1) - Please see below.
>
> >
> > Bart v Ingen Schenau
> >
> >
>
>
> Paul Lutus wrote :
>

> > > Hi,
> > >
> > > Here is template function str_to_any.
> > > I would like to hope it converts string to any type.
> > >
> > > Thanks,
> > > Alex
> > >
> >

> > Here's what you have to do:
> >
> > 1. Tell us what you hoped your program would do.
> > 2. Tell us what your program did instead.
> > 3. Tell us how (2) differed from (1).
>
> [snip]
>
> I hope (2) does not differ from (1) - Please see below.
>
>
>
>

> file://====================================================


> template <typename T>
> bool str_to_any (T& tvalue_o, const string& string_i);

> file://====================================================

0 new messages