Pages: [1] 2
  Print  
Author Topic: lol streams  (Read 5225 times)
Offline (Male) RetroX
Posted on: February 24, 2010, 01:47:59 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Something useful for ENIGMA:

Code: [Select]
std::ostream &operator <<(std::ostream &outstream,__variant &variable)
 {
 if (variable.type)
  {
  return outstream << variable.doubleval;
  }
 return outstream << variable.stringval;
 }
std::istream &operator >>(std::istream &instream,__variant &variable)
 {
 char c,c2;
 c=instream.get();
 if (c>=48 && c<=57)
  {
  instream.putback(c);
  return instream >> variable.doubleval;
  }
 else if (c==46)
  {
  c2=instream.get();
  if (c2>=48 && c2<=57)
   {
   instream.putback(c2);
   instream.putback(c);
   return instream >> variable.doubleval;
   }
  }
 instream.putback(c);
 return instream >> variable.stringval;
 }

You probably should add a typecast operator for var to cast to __variant.

This is from r3's source, though, so, IDK what you've changed.

Also, because I wouldn't know, would this also work for iostream, fstream, and stringstream because they are inherited from istream and ostream, or would it not because this is defined after that?
« Last Edit: February 24, 2010, 01:52:48 PM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #1 Posted on: February 24, 2010, 07:38:55 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
Does... that work? It should error that your function should be a member of std::ostream rather than declared in the global scope...
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #2 Posted on: February 25, 2010, 08:15:42 AM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
No, you can declare new versions of << without it being a member. That was the whole point of switching to the brain-dead system.
Logged
Offline (Male) Josh @ Dreamland
Reply #3 Posted on: February 25, 2010, 08:23:50 AM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
Duly noted.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #4 Posted on: February 25, 2010, 07:59:47 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Duly noted.
Yeah, I figured that out recently, and decided to mess around with it.

Code: [Select]
typedef char int8;
typedef unsigned char uint8;
std::ostream &operator <<(std::ostream &outstream,int8 value)
 {
 return outstream << (int)value;
 }
std::ostream &operator <<(std::ostream &outstream,uint8 value)
 {
 return outstream << (unsigned int)value;
 }
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) RetroX
Reply #5 Posted on: March 04, 2010, 06:38:54 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Additionally, here's something else that might be useful:
Code: [Select]
// Operators for strings (addition)
string &operator +(string &str1,string &str2)
 {
 string temp=str1;
 return temp+=str2;
 }
string &operator +(string &str1,cstring str2)
 {
 string temp=str1;
 return temp+=str2;
 }

// Operators for strings (comparison)
bool operator ==(string &str1,string &str2)
 {
 string temp=str1;
 return (temp.compare(str2));
 }
bool operator ==(string &str1,cstring str2)
 {
 string temp=str1;
 return (temp.compare(str2));
 }

// Operators for strings (comparison)
bool operator !=(string &str1,string &str2)
 {
 return !(str1==str2);
 }
bool operator !=(string &str1,cstring str2)
 {
 return !(str1==str2);
 }

Simple operators for strings.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #6 Posted on: March 04, 2010, 06:44:39 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
String already defines those...
And then some...
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #7 Posted on: March 04, 2010, 06:52:23 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
I hate it when cplusplus.com doesn't provide an accurate reference.

I learned something new, today, I guess.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #8 Posted on: March 04, 2010, 07:18:52 PM

Resident Troll
Joined: Feb 2008
Posts: 960
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/
http://cplusplus.com/reference/string/operators/
Logged
Offline (Male) RetroX
Reply #9 Posted on: March 04, 2010, 07:23:09 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Those functions are on cplusplus.com:
http://cplusplus.com/reference/string/operator+/
http://cplusplus.com/reference/string/operators/
I was looking at <cstring>; for whatever reason, it didn't occur to me to look at <string>.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) Micah
Reply #10 Posted on: March 04, 2010, 09:06:41 PM

Resident Troll
Joined: May 2008
Posts: 129

View Profile
Could you implement those operators for char *s?
Logged
Offline (Male) RetroX
Reply #11 Posted on: March 04, 2010, 09:19:37 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Could you implement those operators for char *s?
string operator+(string str,char *str2)
 {
 return str+const_cast<const char*>(str2);
 }

?
« Last Edit: March 04, 2010, 09:36:53 PM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #12 Posted on: March 05, 2010, 02:48:17 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Not for two char*s though. C++ doesn't allow that.
Logged
Offline (Male) RetroX
Reply #13 Posted on: March 05, 2010, 09:30:16 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Erm, why would you want to do that?  That's kind of the point of string - to add things like that.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #14 Posted on: March 05, 2010, 09:33:35 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
He's thinking about 'situations where you need to quote "' + "that's" + '," or other situations requiring both "' + " and '".
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Pages: [1] 2
  Print