Getting C compiler to recognize OS

AlexWade

Member
Sep 27, 2003
89
0
0
Here is my problem:

I'm writting a class to convert to strings and numbers and vice-versa Java style. I'm doing this because I really like the way Java handles data conversions (i.e. INT.parseInt(...)).

The problem is, I cannot find any numbers to strings functions in the ANSI library. So, instead of taking to task writing these myself, I found some OS specific functions.

Solaris UNIX, for instance, has lltostr. Visual Studio .NET has _ltostr. Each has different parameters. What I want is something that I can use on UNIX, Linux, Windows since I'll be using all 3 regularlly.

Now, this is how I am implimenting my method.

char* parse__::STRING(long value) {
#ifdef MICROSOFT__DOT_NET
// use _ltostr
#endif
#elif defined(UNIX_SOLARIS)
// use lltostr
#else
throw exception("Unsupported compiler");
#endif
return NULL;
}

And I want to have something
#if using Solaris #define UNIX_SOLARIS
#elif using .NET #define MICROSOFT__DOT_NET

The only directives I've found for doing this sort of thing is OS specific. That is no good. Any ideas? I have to precompiler directives for some parts because if I don't, the compiler balks about undeclared identifier. Better yet, is there an ANSI number to string like atoi or atof?

Thanks
Wade
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I believe strings are part of the C++ standard now, if you're using C you can't use classes at all.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: AlexWade
Here is my problem:
The problem is, I cannot find any numbers to strings functions in the ANSI library. So, instead of taking to task writing these myself, I found some OS specific functions.

Forget C, use C++.



 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: Nothinman
I believe strings are part of the C++ standard now, if you're using C you can't use classes at all.

Sure you can... if you are tricky.

 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: AlexWade
Here is my problem:

I'm writting a class to convert to strings and numbers and vice-versa Java style. I'm doing this because I really like the way Java handles data conversions (i.e. INT.parseInt(...)).

The problem is, I cannot find any numbers to strings functions in the ANSI library. So, instead of taking to task writing these myself, I found some OS specific functions.

Solaris UNIX, for instance, has lltostr. Visual Studio .NET has _ltostr. Each has different parameters. What I want is something that I can use on UNIX, Linux, Windows since I'll be using all 3 regularlly.

Now, this is how I am implimenting my method.

char* parse__::STRING(long value) {
#ifdef MICROSOFT__DOT_NET
// use _ltostr
#endif
#elif defined(UNIX_SOLARIS)
// use lltostr
#else
throw exception("Unsupported compiler");
#endif
return NULL;
}

And I want to have something
#if using Solaris #define UNIX_SOLARIS
#elif using .NET #define MICROSOFT__DOT_NET

The only directives I've found for doing this sort of thing is OS specific. That is no good. Any ideas? I have to precompiler directives for some parts because if I don't, the compiler balks about undeclared identifier. Better yet, is there an ANSI number to string like atoi or atof?

Thanks
Wade

Your use of the preprocesser like that hurts, it hurts bad.

 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: AlexWade
Here is my problem:

I'm writting a class to convert to strings and numbers and vice-versa Java style. I'm doing this because I really like the way Java handles data conversions (i.e. INT.parseInt(...)).

The problem is, I cannot find any numbers to strings functions in the ANSI library. So, instead of taking to task writing these myself, I found some OS specific functions.

Solaris UNIX, for instance, has lltostr. Visual Studio .NET has _ltostr. Each has different parameters. What I want is something that I can use on UNIX, Linux, Windows since I'll be using all 3 regularlly.

Now, this is how I am implimenting my method.

char* parse__::STRING(long value) {
#ifdef MICROSOFT__DOT_NET
// use _ltostr
#endif
#elif defined(UNIX_SOLARIS)
// use lltostr
#else
throw exception("Unsupported compiler");
#endif
return NULL;
}

And I want to have something
#if using Solaris #define UNIX_SOLARIS
#elif using .NET #define MICROSOFT__DOT_NET

The only directives I've found for doing this sort of thing is OS specific. That is no good. Any ideas? I have to precompiler directives for some parts because if I don't, the compiler balks about undeclared identifier. Better yet, is there an ANSI number to string like atoi or atof?

Thanks
Wade


Seriously though, couldn't you wrap up a CString for most of the functionality?
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
um, try atoi/atol for ints....
edit: um oops, thats the wrong direction...

or sprintf for any type....



sprintf is the function you are looking for.

If it's C++ you can send the numbers into a stringstream.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Sure you can... if you are tricky.

structs with function pointers aren't classes =)

Seriously though, couldn't you wrap up a CString for most of the functionality?

Isn't CString a part of MFC or ATL which would make it only work in VS?
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: Nothinman
Sure you can... if you are tricky.

structs with function pointers aren't classes =)

Seriously though, couldn't you wrap up a CString for most of the functionality?

Isn't CString a part of MFC or ATL which would make it only work in VS?

Yea... but I thought it was made part of the ansi standard a little while back.

There's this nice class out there on the web somewhere called CStdString. It's exactly like CString except, it'll compile on just about anything. I think... codeproject.com might have it.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
There is a string class in the C++ standard libs, why use something like CString or CStdString?
 

dolemitetx

Junior Member
Feb 21, 2001
5
0
0
If using C++:

#include <sstream>

int i = 5; // Number to stringify
stringstream s;

s << i;
string myNum(s.str());