Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*      Copyright (C) 2004 Garrett A. Kajmowicz
  2.  
  3.         This file is part of the uClibc++ Library.
  4.  
  5.         This library is free software; you can redistribute it and/or
  6.         modify it under the terms of the GNU Lesser General Public
  7.         License as published by the Free Software Foundation; either
  8.         version 2.1 of the License, or (at your option) any later version.
  9.  
  10.         This library is distributed in the hope that it will be useful,
  11.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.         Lesser General Public License for more details.
  14.  
  15.         You should have received a copy of the GNU Lesser General Public
  16.         License along with this library; if not, write to the Free Software
  17.         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include <basic_definitions>
  21.  
  22. #ifndef HEADER_STD_SSTREAM
  23. #define HEADER_STD_SSTREAM 1
  24.  
  25. #include <iosfwd>
  26. #include <ios>
  27. #include <istream>
  28. #include <ostream>
  29. #include <iostream>
  30. #include <string>
  31.  
  32. #pragma GCC visibility push(default)
  33.  
  34. namespace std{
  35.  
  36.         template <class charT, class traits, class Allocator>
  37.                 class _UCXXEXPORT basic_stringbuf : public basic_streambuf<charT,traits>
  38.         {
  39.         public:
  40.                 typedef charT char_type;
  41.                 typedef typename traits::int_type int_type;
  42.                 typedef typename traits::pos_type pos_type;
  43.                 typedef typename traits::off_type off_type;
  44.                 typedef typename Allocator::size_type size_type;
  45.  
  46.                 explicit _UCXXEXPORT basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out)
  47.                         : data(), ielement(0), oelement(0)
  48.                 {
  49.                         basic_streambuf<charT,traits>::openedFor = which;
  50.                 }
  51.  
  52.                 explicit _UCXXEXPORT basic_stringbuf(const basic_string<charT,traits,Allocator>& str,
  53.                         ios_base::openmode which = ios_base::in | ios_base::out)
  54.                         : data(str), ielement(0), oelement(0)
  55.                 {
  56.                         if(which & ios_base::ate){
  57.                                 oelement = data.length();
  58.                         }
  59.                         basic_streambuf<charT,traits>::openedFor = which;
  60.                 }
  61.  
  62.                 virtual _UCXXEXPORT ~basic_stringbuf() { }
  63.  
  64.                 _UCXXEXPORT basic_string<charT,traits,Allocator> str() const{
  65.                         return data;
  66.                 }
  67.  
  68.                 _UCXXEXPORT void str(const basic_string<charT,traits,Allocator>& s){
  69.                         data = s;
  70.                         ielement = 0;
  71.                         if(basic_streambuf<charT,traits>::openedFor & ios_base::ate){
  72.                                 oelement = data.length();
  73.                         }else{
  74.                                 oelement = 0;
  75.                         }
  76.                 }
  77.  
  78.         protected:
  79.                 virtual _UCXXEXPORT int sync(){
  80.                         return 0;
  81.                 }
  82.                 virtual _UCXXEXPORT int_type underflow(){
  83.                         if(ielement >= data.length()){
  84.                                 return traits::eof();
  85.                         }
  86.                         return traits::to_int_type(data[ielement]);
  87.                 }
  88.  
  89.                 virtual _UCXXEXPORT int_type uflow(){
  90.                         int_type retval = underflow();
  91.                         if(retval != traits::eof()){
  92.                                 ++ielement;
  93.                         }
  94.                         return retval; 
  95.                 }
  96.  
  97.                 virtual _UCXXEXPORT int_type   pbackfail(int_type c = traits::eof()){
  98.                         //Error possibilities
  99.                         if(ielement == 0){
  100.                                 return traits::eof();
  101.                         }
  102.                         if(ielement > data.length()){
  103.                                 ielement = data.length();
  104.                                 return traits::eof();
  105.                         }
  106.                         //eof passed in
  107.                         if(traits::eq_int_type(c,traits::eof())==true){
  108.                                 --ielement;
  109.                                 return traits::not_eof(c);
  110.                         }
  111.                         if(traits::eq(traits::to_char_type(c),data[ielement-1]) == true){
  112.                                 --ielement;
  113.                                 return c;
  114.                         }
  115.                         if(basic_streambuf<charT,traits>::openedFor & ios_base::out){
  116.                                 --ielement;
  117.                                 data[ielement] = c;
  118.                                 return c;
  119.                         }
  120.                         return traits::eof();
  121.                 }
  122.  
  123.                 virtual _UCXXEXPORT int showmanyc(){
  124.                         return data.length() - ielement;
  125.                 }
  126.                 virtual _UCXXEXPORT streamsize xsgetn(char_type* c, streamsize n){
  127.                         streamsize i = 0;
  128.                         while(ielement < data.length() && i < n ){
  129.                                 c[i] = data[ielement];
  130.                                 ++i;
  131.                                 ++ielement;
  132.                         }
  133.                         return i;
  134.                 }
  135.  
  136.                 virtual _UCXXEXPORT int_type overflow (int_type c = traits::eof()){
  137.                         //Nothing to do
  138.                         if(traits::eq_int_type(c,traits::eof())){
  139.                                 return traits::not_eof(c);
  140.                         }
  141.  
  142.                         //Actually add character, if possible
  143.                         if(basic_streambuf<charT,traits>::openedFor & ios_base::out){
  144.                                 if(oelement >= data.length()){
  145.                                         data.push_back(c);
  146.                                 }else{
  147.                                         data[oelement] = c;
  148.                                 }
  149.                                 ++oelement;
  150.                                 return c;
  151.                         }
  152.                         //Not possible
  153.                         return traits::eof();
  154.                 }
  155.  
  156.                 virtual _UCXXEXPORT basic_streambuf<charT,traits>* setbuf(charT*, streamsize){
  157.                         //This function does nothing
  158.                         return this;
  159.                 }
  160.  
  161.                 virtual _UCXXEXPORT streamsize xsputn(const char_type* s, streamsize n){
  162.                         data.replace(oelement, n, s, n);
  163.                         oelement += n;
  164.                         return n;
  165.                 }
  166.  
  167.                 virtual _UCXXEXPORT pos_type seekoff(off_type off, ios_base::seekdir way,
  168.                         ios_base::openmode which = ios_base::in | ios_base::out)
  169.                 {
  170.                         //Test for invalid option
  171.                         if( (which & ios_base::in) && (which & ios_base::out) && (way == ios_base::cur)){
  172.                                 return -1;
  173.                         }
  174.  
  175.                         //Calculate new location
  176.                         size_type newpos = 0;
  177.  
  178.                         if(way == ios_base::beg){
  179.                                 newpos = off;
  180.                         }else if(way == ios_base::cur){
  181.                                 if(which & ios_base::out){
  182.                                         newpos = data.length() + off;
  183.                                 }
  184.                                 if(which & ios_base::in){
  185.                                         newpos = ielement + off;
  186.                                 }
  187.                                
  188.                         }else{
  189.                                 newpos = data.length() + off;
  190.                         }
  191.  
  192.                         //Test for error conditions
  193.                         if(newpos > data.length()){
  194.                                 return -1;
  195.                         }
  196.  
  197.                         //Shuffle pointers
  198.  
  199.                         if(which & ios_base::in){
  200.                                 ielement = newpos;
  201.                         }
  202.                         if(which & ios_base::out){
  203.                                 data.resize(newpos);
  204.                                 if(ielement > data.length()){
  205.                                         ielement = data.length();
  206.                                 }
  207.                         }
  208.  
  209.                         return newpos;
  210.                 }
  211.                        
  212.                 virtual _UCXXEXPORT pos_type seekpos(pos_type sp,
  213.                         ios_base::openmode which = ios_base::in | ios_base::out)
  214.                 {
  215.                         return seekoff(sp, ios_base::beg, which);
  216.                 }
  217.  
  218.                 basic_string<charT,traits,Allocator> data;
  219.                 size_type ielement;
  220.                 size_type oelement;
  221.         };
  222.  
  223.  
  224.         template <class charT, class traits, class Allocator> class _UCXXEXPORT basic_istringstream
  225.                 : public basic_istream<charT,traits>
  226.         {
  227.         public:
  228.                 typedef charT char_type;
  229.                 typedef typename traits::int_type int_type;
  230.                 typedef typename traits::pos_type pos_type;
  231.                 typedef typename traits::off_type off_type;
  232.  
  233.  
  234.                 explicit _UCXXEXPORT basic_istringstream(ios_base::openmode m = ios_base::in)
  235.                         : basic_ios<charT, traits>(&sb), basic_istream<charT,traits>(&sb), sb(m)
  236.                 {
  237.                 }
  238.                 explicit _UCXXEXPORT basic_istringstream( const basic_string<charT,traits,Allocator>& str,
  239.                         ios_base::openmode which = ios_base::in)
  240.                         : basic_ios<charT, traits>(&sb), basic_istream<charT,traits>(&sb), sb(str, which)
  241.                 {
  242.                 }
  243.                 virtual _UCXXEXPORT ~basic_istringstream() {  }
  244.                 _UCXXEXPORT basic_stringbuf<charT,traits,Allocator>* rdbuf() const{
  245.                         return &sb;
  246.                 }
  247.                 _UCXXEXPORT basic_string<charT,traits,Allocator> str() const{
  248.                         return sb.str();
  249.                 }
  250.                 _UCXXEXPORT void str(const basic_string<charT,traits,Allocator>& s){
  251.                         sb.str(s);
  252.                         basic_istream<charT,traits>::clear();
  253.                 }
  254.         private:
  255.                 basic_stringbuf<charT,traits,Allocator> sb;
  256.         };
  257.  
  258.  
  259.         template <class charT, class traits, class Allocator> class _UCXXEXPORT basic_ostringstream
  260.                 : public basic_ostream<charT,traits>
  261.         {
  262.         public:
  263.  
  264.                 typedef charT char_type;
  265.                 typedef typename traits::int_type int_type;
  266.                 typedef typename traits::pos_type pos_type;
  267.                 typedef typename traits::off_type off_type;
  268.  
  269.                 explicit _UCXXEXPORT basic_ostringstream(ios_base::openmode m = ios_base::out)
  270.                         : basic_ios<charT, traits>(&sb), basic_ostream<charT,traits>(&sb), sb(m)
  271.                 {
  272.                 }
  273.                 explicit _UCXXEXPORT basic_ostringstream(const basic_string<charT,traits,Allocator>& str,
  274.                         ios_base::openmode which = ios_base::out)
  275.                         : basic_ios<charT, traits>(&sb), basic_ostream<charT,traits>(&sb), sb(str, which)
  276.                 {
  277.                 }
  278.                 virtual _UCXXEXPORT ~basic_ostringstream() {  }
  279.  
  280.                 _UCXXEXPORT basic_stringbuf<charT,traits,Allocator>* rdbuf() const{
  281.                         return &sb;
  282.                 }
  283.                 _UCXXEXPORT basic_string<charT,traits,Allocator> str() const{
  284.                         return sb.str();
  285.                 }
  286.                 _UCXXEXPORT void str(const basic_string<charT,traits,Allocator>& s){
  287.                         sb.str(s);
  288.                         basic_ostream<charT,traits>::clear();
  289.                 }
  290.         private:
  291.                 basic_stringbuf<charT,traits,Allocator> sb;
  292.         };
  293.  
  294.  
  295.         template <class charT, class traits, class Allocator> class _UCXXEXPORT basic_stringstream
  296.                  : public basic_iostream<charT,traits>
  297.         {
  298.         public:
  299.  
  300.                 typedef charT char_type;
  301.                 typedef typename traits::int_type int_type;
  302.                 typedef typename traits::pos_type pos_type;
  303.                 typedef typename traits::off_type off_type;
  304.  
  305.                 explicit _UCXXEXPORT basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in)
  306.                         : basic_ios<charT, traits>(&sb), basic_iostream<charT,traits>(&sb), sb(which)
  307.                 {
  308.                 }
  309.  
  310.                 explicit _UCXXEXPORT basic_stringstream(const basic_string<charT,traits,Allocator>& str,
  311.                         ios_base::openmode which = ios_base::out|ios_base::in)
  312.                         : basic_ios<charT, traits>(&sb), basic_iostream<charT,traits>(&sb), sb(str, which)
  313.                 {
  314.                 }
  315.                 virtual _UCXXEXPORT ~basic_stringstream(){  }
  316.  
  317.                 _UCXXEXPORT basic_stringbuf<charT,traits,Allocator>* rdbuf(){
  318.                         return &sb;
  319.                 }
  320.                 _UCXXEXPORT basic_string<charT,traits,Allocator> str() const{
  321.                         return sb.str();
  322.                 }
  323.                 _UCXXEXPORT void str(const basic_string<charT,traits,Allocator>& s){
  324.                         sb.str(s);
  325.                         basic_iostream<charT,traits>::clear();
  326.                 }
  327.         private:
  328.                 basic_stringbuf<charT, traits> sb;
  329.         };
  330.  
  331. #ifdef __UCLIBCXX_EXPAND_SSTREAM_CHAR__
  332. #ifndef __UCLIBCXX_COMPILE_SSTREAM__
  333.  
  334. #ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
  335.  
  336.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::
  337.                 basic_stringbuf(ios_base::openmode which);
  338.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::~basic_stringbuf();
  339.  
  340. #endif // __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
  341.  
  342.         template <> _UCXXEXPORT basic_string<char, char_traits<char>, allocator<char> >
  343.                 basic_stringbuf<char, char_traits<char>, allocator<char> >::str() const;
  344.  
  345.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::int_type
  346.                 basic_stringbuf<char, char_traits<char>, allocator<char> >::
  347.                         pbackfail(basic_stringbuf<char, char_traits<char>, allocator<char> >::int_type c);
  348.  
  349.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::pos_type
  350.                 basic_stringbuf<char, char_traits<char>, allocator<char> >::
  351.                         seekoff (basic_stringbuf<char, char_traits<char>, allocator<char> >::off_type off,
  352.                                 ios_base::seekdir way,
  353.                                 ios_base::openmode which
  354.                         );
  355.  
  356.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::int_type
  357.                 basic_stringbuf<char, char_traits<char>, allocator<char> >::
  358.                         overflow (basic_stringbuf<char, char_traits<char>, allocator<char> >::int_type c);
  359.  
  360.         template <> _UCXXEXPORT basic_stringbuf<char, char_traits<char>, allocator<char> >::int_type
  361.                 basic_stringbuf<char, char_traits<char>, allocator<char> >::underflow ();
  362.  
  363.         template <> _UCXXEXPORT streamsize basic_stringbuf<char, char_traits<char>, allocator<char> >::
  364.                 xsputn(const char* s, streamsize n);
  365.  
  366. #ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
  367.  
  368.         template <> _UCXXEXPORT basic_stringstream<char, char_traits<char>, allocator<char> >::
  369.                 basic_stringstream(ios_base::openmode which);
  370.         template <> _UCXXEXPORT basic_stringstream<char, char_traits<char>, allocator<char> >::~basic_stringstream();
  371.         template <> _UCXXEXPORT basic_istringstream<char, char_traits<char>, allocator<char> >::~basic_istringstream();
  372.         template <> _UCXXEXPORT basic_ostringstream<char, char_traits<char>, allocator<char> >::~basic_ostringstream();
  373.  
  374. #endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
  375.  
  376. #endif
  377. #endif
  378.  
  379. #pragma GCC visibility pop
  380.  
  381. }
  382.  
  383.  
  384. #endif
  385.