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. #include <cstddef>
  22. #include <locale>
  23. #include <iosfwd>
  24.  
  25. #ifndef __HEADER_STD_IOS
  26. #define __HEADER_STD_IOS 1
  27.  
  28. #pragma GCC visibility push(default)
  29.  
  30. namespace std{
  31.         typedef signed long int streamoff;
  32.  
  33.         template <class stateT> class fpos;
  34.  
  35.         class _UCXXEXPORT ios_base {
  36.         public:
  37.                 class failure;
  38. #ifdef __UCLIBCXX_EXCEPTION_SUPPORT__
  39.                 class failure : public exception {
  40.                 public:
  41.                         explicit failure(const std::string&) { }
  42.                         explicit failure() { }
  43.                         virtual const char* what() const throw() {
  44.                                 return "std::ios_base failure exception";
  45.                         }
  46.                 };
  47. #endif
  48. #ifdef __UCLIBCXX_SUPPORT_CDIR__
  49.                 class _UCXXLOCAL Init{
  50.                 public:
  51.                         _UCXXEXPORT Init();
  52.                         _UCXXEXPORT ~Init();
  53.                 private:
  54.                         static int init_cnt;
  55.                 };
  56. #endif
  57.  
  58.         public:
  59.  
  60.                 typedef unsigned short int fmtflags;
  61.  
  62.                 static const fmtflags skipws            = 0x0001;
  63.  
  64.                 static const fmtflags left              = 0x0002;
  65.                 static const fmtflags right             = 0x0004;
  66.                 static const fmtflags internal          = 0x0008;
  67.  
  68.                 static const fmtflags boolalpha         = 0x0010;
  69.  
  70.                 static const fmtflags dec               = 0x0020;
  71.                 static const fmtflags oct               = 0x0040;
  72.                 static const fmtflags hex               = 0x0080;
  73.  
  74.                 static const fmtflags scientific        = 0x0100;
  75.                 static const fmtflags fixed             = 0x0200;
  76.  
  77.                 static const fmtflags showbase          = 0x0400;
  78.                 static const fmtflags showpoint         = 0x0800;
  79.                 static const fmtflags showpos           = 0x1000;
  80.                 static const fmtflags uppercase         = 0x2000;
  81.  
  82.                 static const fmtflags adjustfield       = left | right | internal;
  83.                 static const fmtflags basefield         = dec | oct | hex;
  84.                 static const fmtflags floatfield        = fixed | scientific;
  85.  
  86.                 static const fmtflags unitbuf           = 0x4000;
  87.  
  88.                 typedef unsigned char iostate;
  89.                 static const iostate goodbit            = 0x00;
  90.                 static const iostate badbit             = 0x01;
  91.                 static const iostate eofbit             = 0x02;
  92.                 static const iostate failbit            = 0x04;
  93.  
  94.                 typedef unsigned char openmode;
  95.                 static const openmode app               = 0x01;
  96.                 static const openmode ate               = 0x02;
  97.                 static const openmode binary            = 0x04;
  98.                 static const openmode in                = 0x08;
  99.                 static const openmode out               = 0x10;
  100.                 static const openmode trunc             = 0x20;
  101.  
  102.                 typedef unsigned char seekdir;
  103.                 static const seekdir beg                = 0x01;
  104.                 static const seekdir cur                = 0x02;
  105.                 static const seekdir end                = 0x04;
  106.  
  107.                 _UCXXEXPORT fmtflags flags() const{
  108.                         return mformat;
  109.                 }
  110.                 _UCXXEXPORT fmtflags flags(fmtflags fmtfl);
  111.  
  112.                 fmtflags setf(fmtflags fmtfl);
  113.                 fmtflags setf(fmtflags fmtfl, fmtflags mask );
  114.  
  115.                 _UCXXEXPORT void unsetf(fmtflags mask){
  116.                         mformat&= ~mask;
  117.                 }
  118.  
  119.                 _UCXXEXPORT streamsize precision() const{
  120.                         return mprecision;
  121.                 }
  122.  
  123.                 _UCXXEXPORT streamsize precision(streamsize prec);
  124.  
  125.                 _UCXXEXPORT streamsize width() const{
  126.                         return mwidth;
  127.                 }
  128.  
  129.                 _UCXXEXPORT streamsize width(streamsize wide);
  130.  
  131.                 _UCXXEXPORT locale imbue(const locale& loc);
  132.  
  133.                 _UCXXEXPORT locale getloc() const{
  134.                         return mLocale;
  135.                 }
  136.  
  137. //              FIXME - These need to be implemented
  138. //              static int xalloc();
  139. //              long&  iword(int index);
  140. //              void*& pword(int index);
  141.  
  142.                 _UCXXEXPORT ~ios_base() { }
  143.  
  144.                 enum event { erase_event, imbue_event, copyfmt_event };
  145.  
  146.                 typedef void (*event_callback)(event, ios_base&, int index);
  147. //              void register_callback(event_call_back fn, int index);
  148.  
  149.                 //We are going to wrap stdio so we don't need implementation of the following:
  150.                 inline static bool sync_with_stdio(bool = true) { return true; }
  151.  
  152.         protected:
  153.                 _UCXXEXPORT ios_base() : mLocale(), mformat(dec | skipws ), mstate(goodbit),
  154.                         mmode(), mdir(), mprecision(6), mwidth(0)
  155. #ifdef __UCLIBCXX_SUPPORT_CDIR__
  156.                         ,mInit()
  157. #endif
  158.                 {
  159.  
  160.                 }
  161.                 locale mLocale;
  162.                 fmtflags mformat;
  163.                 iostate mstate;
  164.                 openmode mmode;
  165.                 seekdir mdir;
  166.                 streamsize mprecision;
  167.                 streamsize mwidth;
  168. #ifdef __UCLIBCXX_SUPPORT_CDIR__
  169.                 Init mInit;
  170. #endif
  171.         };
  172.  
  173.  
  174.         //ios_base manipulators
  175.  
  176.  
  177.         inline ios_base& boolalpha  (ios_base& str){
  178.                 str.setf(ios_base::boolalpha);
  179.                 return str;
  180.         }
  181.         inline ios_base& noboolalpha(ios_base& str){
  182.                 str.unsetf(ios_base::boolalpha);
  183.                 return str;
  184.         }
  185.         inline ios_base& showbase   (ios_base& str){
  186.                 str.setf(ios_base::showbase);
  187.                 return str;
  188.         }
  189.         inline ios_base& noshowbase (ios_base& str){
  190.                 str.unsetf(ios_base::showbase);
  191.                 return str;
  192.         }
  193.         inline ios_base& showpoint  (ios_base& str){
  194.                 str.setf(ios_base::showpoint);
  195.                 return str;
  196.         }
  197.         inline ios_base& noshowpoint(ios_base& str){
  198.                 str.unsetf(ios_base::showpoint);
  199.                 return str;
  200.         }
  201.         inline ios_base& showpos    (ios_base& str){
  202.                 str.setf(ios_base::showpos);
  203.                 return str;
  204.         }
  205.         inline ios_base& noshowpos  (ios_base& str){
  206.                 str.unsetf(ios_base::showpos);
  207.                 return str;
  208.         }
  209.         inline ios_base& skipws     (ios_base& str){
  210.                 str.setf(ios_base::skipws);
  211.                 return str;
  212.         }
  213.         inline ios_base& noskipws   (ios_base& str){
  214.                 str.unsetf(ios_base::skipws);
  215.                 return str;
  216.         }
  217.         inline ios_base& uppercase  (ios_base& str){
  218.                 str.setf(ios_base::uppercase);
  219.                 return str;
  220.         }
  221.         inline ios_base& nouppercase(ios_base& str){
  222.                 str.unsetf(ios_base::uppercase);
  223.                 return str;
  224.         }
  225.  
  226.         inline ios_base& unitbuf    (ios_base& str){
  227.                 str.setf(ios_base::unitbuf);
  228.                 return str;
  229.         }
  230.         inline ios_base& nounitbuf  (ios_base& str){
  231.                 str.unsetf(ios_base::unitbuf);
  232.                 return str;
  233.         }
  234.         inline ios_base& internal   (ios_base& str){
  235.                 str.setf(ios_base::internal, ios_base::adjustfield);
  236.                 return str;
  237.         }
  238.         inline ios_base& left       (ios_base& str){
  239.                 str.setf(ios_base::left, ios_base::adjustfield);
  240.                 return str;
  241.         }
  242.         inline ios_base& right      (ios_base& str){
  243.                 str.setf(ios_base::right, ios_base::adjustfield);
  244.                 return str;
  245.         }
  246.  
  247.         inline ios_base& dec        (ios_base& str){
  248.                 str.setf(ios_base::dec, ios_base::basefield);
  249.                 return str;
  250.         }
  251.         inline ios_base& hex        (ios_base& str){
  252.                 str.setf(ios_base::hex, ios_base::basefield);
  253.                 return str;
  254.         }
  255.         inline ios_base& oct        (ios_base& str){
  256.                 str.setf(ios_base::oct, ios_base::basefield);
  257.                 return str;
  258.         }
  259.  
  260.         inline ios_base& fixed      (ios_base& str){
  261.                 str.setf(ios_base::fixed, ios_base::floatfield);
  262.                 return str;
  263.         }
  264.         inline ios_base& scientific (ios_base& str){
  265.                 str.setf(ios_base::scientific, ios_base::floatfield);
  266.                 return str;
  267.         }
  268.  
  269.  
  270.         //basic_ios class definition
  271.  
  272.  
  273.         template <class charT, class traits > class _UCXXEXPORT basic_ios
  274.                 : public ios_base
  275.         {
  276.         public:
  277.         // Types:
  278.                 typedef charT char_type;
  279.                 typedef typename traits::int_type int_type;
  280.                 typedef typename traits::pos_type pos_type;
  281.                 typedef typename traits::off_type off_type;
  282.                 typedef traits traits_type;
  283.  
  284.                 _UCXXEXPORT operator void*() const{
  285.                         if(fail() ){
  286.                                 return 0;
  287.                         }
  288.                         return (void *)(1);     //Must return a non-NULL pointer (though it can be *any* pointer)
  289.                 }
  290.  
  291.                 _UCXXEXPORT bool operator!() const{
  292.                         return fail();
  293.                 }
  294.                 _UCXXEXPORT iostate rdstate() const{
  295.                         return mstate;
  296.                 }
  297.                 _UCXXEXPORT void clear(iostate state = goodbit){
  298.                         if(rdbuf()!=0){
  299.                                 mstate = state;
  300.                         }else{
  301.                                 mstate = state|ios_base::badbit;
  302.                         }
  303.                 }
  304.                 _UCXXEXPORT void setstate(iostate state) {
  305.                         clear(rdstate()  | state);
  306. #ifdef __UCLIBCXX_EXCEPTION_SUPPORT__
  307.                         if(rdstate() & throw_mask){
  308.                                 throw failure();
  309.                         }
  310. #endif
  311.                 }
  312.  
  313.                 _UCXXEXPORT bool good() const{
  314.                         return (rdstate() == 0);
  315.                 }
  316.                 _UCXXEXPORT bool eof()  const{
  317.                         if(rdstate() & eofbit){
  318.                                 return true;
  319.                         }
  320.                         return false;
  321.                 }
  322.                 _UCXXEXPORT bool fail() const{
  323.                         if( mstate & (failbit | badbit) ){
  324.                                 return true;
  325.                         }
  326.                         return false;
  327.                 }
  328.  
  329.                 _UCXXEXPORT bool bad()  const{
  330.                         if(mstate & badbit){
  331.                                 return true;
  332.                         }
  333.                         return false;
  334.                 }
  335.  
  336.                 _UCXXEXPORT iostate exceptions() const{
  337.                         return throw_mask;
  338.                 }
  339.                 _UCXXEXPORT void exceptions(iostate except){
  340.                         throw_mask = except;
  341.                 }
  342.  
  343.                 explicit _UCXXEXPORT basic_ios(basic_streambuf<charT,traits>* sb) : fill_char(' '), mtied(0), mstreambuf(0){
  344.                         init(sb);
  345.                 }
  346.  
  347.                 basic_ios() : mtied(0), mstreambuf(0){ }
  348.  
  349.                 virtual _UCXXEXPORT ~basic_ios(){
  350.                 }
  351.  
  352.                 _UCXXEXPORT basic_ostream<charT,traits>* tie() const{
  353.                         return mtied;
  354.                 }
  355.                 _UCXXEXPORT basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr){
  356.                         basic_ostream<charT,traits>* retval= mtied;
  357.                         mtied = tiestr;
  358.                         return retval;
  359.                 }
  360.                 _UCXXEXPORT basic_streambuf<charT,traits>* rdbuf() const{
  361.                         return mstreambuf;
  362.                 }
  363.                 _UCXXEXPORT basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb){
  364.                         basic_streambuf<charT,traits>* retval = mstreambuf;
  365.                         mstreambuf = sb;
  366.                         return retval;
  367.                 }
  368.                 _UCXXEXPORT basic_ios& copyfmt(const basic_ios& rhs);
  369.                 _UCXXEXPORT char_type fill() const{
  370.                         return fill_char;
  371.                 }
  372.                 _UCXXEXPORT char_type fill(char_type ch){
  373.                         char_type temp = fill_char;
  374.                         fill_char = ch;
  375.                         return temp;
  376.                 }
  377.  
  378.                 _UCXXEXPORT locale imbue(const locale& loc){
  379.                         return ios_base::imbue(loc);
  380.                 }
  381.                 _UCXXEXPORT char narrow(char_type c, char dfault) const;
  382.                 _UCXXEXPORT char_type widen(char c) const;
  383.  
  384.         protected:
  385.                 char_type fill_char;
  386.                 basic_ostream<charT,traits>* mtied;
  387.                 basic_streambuf<charT,traits>* mstreambuf;
  388.                 iostate throw_mask;
  389.                 _UCXXEXPORT basic_ios(const basic_ios<charT,traits> &){ }
  390.                 _UCXXEXPORT basic_ios<charT,traits> & operator=(const basic_ios<charT,traits> &){ return *this; }
  391.                 _UCXXEXPORT void init(basic_streambuf<charT,traits>* sb){
  392.                         ios_base::mformat = skipws|dec;
  393.                         mstreambuf = sb;
  394.                         mstate = goodbit;
  395.                         throw_mask = goodbit;
  396.                 }
  397.         };
  398.  
  399. #ifdef __UCLIBCXX_EXPAND_IOS_CHAR__
  400. #ifndef __UCLIBCXX_COMPILE_IOS__
  401.  
  402.         template <> _UCXXEXPORT void basic_ios<char, char_traits<char> >::clear(iostate state);
  403.         template <> _UCXXEXPORT void basic_ios<char, char_traits<char> >::setstate(iostate state);
  404.  
  405. #endif
  406. #endif
  407.  
  408.  
  409.         template <class charT, class traits>
  410.                 inline char basic_ios<charT, traits>::narrow(char_type c, char dfault) const
  411.         {
  412.                 return dfault;
  413.         }
  414.  
  415.         template <>
  416.                 inline char basic_ios<char, char_traits<char> >::narrow(char_type c, char) const
  417.         {
  418.                 return c;
  419.         }
  420.  
  421. #ifdef __UCLIBCXX_HAS_WCHAR__
  422.  
  423.         template <>
  424.                 inline char basic_ios<wchar_t, char_traits<wchar_t> >::narrow(char_type c, char dfault) const
  425.         {
  426.                 char retval = wctob (c);
  427.                 if(retval == EOF){
  428.                         retval = dfault;
  429.                 }
  430.                 return retval;
  431.         }
  432.  
  433. #endif  //__UCLIBCXX_HAS_WCHAR__
  434.  
  435.         template <class charT, class traits>
  436.                 inline typename basic_ios<charT, traits>::char_type
  437.                 basic_ios<charT, traits>::widen(char c) const
  438.         {
  439.                 return c;
  440.         }
  441.  
  442.         template <>
  443.                 inline basic_ios<char, char_traits<char> >::char_type
  444.                 basic_ios<char, char_traits<char> >::widen(char c) const
  445.         {
  446.                 return c;
  447.         }
  448.  
  449. #ifdef __UCLIBCXX_HAS_WCHAR__
  450.  
  451.         template <>
  452.                 inline basic_ios<wchar_t, char_traits<wchar_t> >::char_type
  453.                 basic_ios<wchar_t, char_traits<wchar_t> >::widen(char c) const
  454.         {
  455.                 return btowc(c);
  456.         }
  457.  
  458. #endif //__UCLIBCXX_HAS_WCHAR__
  459.  
  460.  
  461.         template <class stateT> class _UCXXEXPORT fpos{
  462.         public:
  463.                 _UCXXEXPORT fpos(stateT s){
  464.                         st = s;
  465.                 }
  466.                 _UCXXEXPORT stateT state() const{
  467.                         return st;
  468.                 }
  469.                 _UCXXEXPORT void state(stateT s){
  470.                         st = s;
  471.                 }
  472.                 _UCXXEXPORT bool operator==(const fpos &rhs){
  473.                         return st == rhs.st;
  474.                 }
  475.                 _UCXXEXPORT bool operator!=(const fpos &rhs){
  476.                         return st != rhs.st;
  477.                 }
  478.                 _UCXXEXPORT fpos & operator+(const streamoff & o){
  479.                         st += o;
  480.                         return *this;
  481.                 }
  482.                 _UCXXEXPORT fpos & operator-(const streamoff & o){
  483.                         st -= o;
  484.                         return *this;
  485.                 }
  486.                 _UCXXEXPORT streamoff operator-(const fpos & rhs){
  487.                         return st - rhs.st;
  488.                 }
  489.  
  490.         private:
  491.                 stateT st;
  492.         };
  493.  
  494.  
  495. }
  496.  
  497. #pragma GCC visibility pop
  498.  
  499. #endif
  500.  
  501.