Changeset 990b85d in mainline


Ignore:
Timestamp:
2018-07-05T21:41:18Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f7aaffe0
Parents:
4d30bcd
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-11-04 20:26:38)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: implemented the rest of streambuf

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/streambuf.hpp

    r4d30bcd r990b85d  
    5252
    5353            virtual ~basic_streambuf()
    54             {
    55                 // TODO: implement
    56             }
     54            { /* DUMMY BODY */ }
    5755
    5856            /**
     
    6260            locale pubimbue(const locale& loc)
    6361            {
    64                 // TODO: implement
     62                return imbue(loc);
    6563            }
    6664
    6765            locale& getloc() const
    6866            {
    69                 // TODO: implement
     67                return locale_;
    7068            }
    7169
     
    7674            basic_streambuf<Char, Traits>* pubsetbuf(char_type* s, streamsize n)
    7775            {
    78                 // TODO: implement
     76                return setbuf(s, n);
    7977            }
    8078
     
    8280                                ios_base::openmode which = ios_base::in | ios_base::out)
    8381            {
    84                 // TODO: implement
     82                return seekoff(off, way, which);
    8583            }
    8684
     
    8886                                ios_base::in | ios_base::out)
    8987            {
    90                 // TODO: implement
     88                return seekpos(pos, which);
    9189            }
    9290
    9391            int pubsync()
    9492            {
    95                 // TODO: implement
     93                return sync();
    9694            }
    9795
     
    102100            streamsize in_avail()
    103101            {
    104                 // TODO: implement
     102                if (read_avail_())
     103                    return egptr() - gptr();
     104                else
     105                    return showmanyc();
    105106            }
    106107
    107108            int_type snextc()
    108109            {
    109                 // TODO: implement
     110                if (traits_type::eq(sbumpc, traits_type::eof()))
     111                    return traits_type::eof();
     112                else
     113                    return sgetc();
    110114            }
    111115
    112116            int_type sbumpc()
    113117            {
    114                 // TODO: implement
     118                if (read_avail_())
     119                    return traits_type::to_int_type(*input_next++);
     120                else
     121                    return uflow();
    115122            }
    116123
    117124            int_type sgetc()
    118125            {
    119                 // TODO: implement
     126                if (read_avail_())
     127                    return traits_type::to_int_type(*input_next++);
     128                else
     129                    return underflow();
    120130            }
    121131
    122132            streamsize sgetn(char_type* s, streamsize n)
    123133            {
    124                 // TODO: implement
     134                return xsgetn(s, n);
    125135            }
    126136
     
    131141            int_type sputbackc(char_type c)
    132142            {
    133                 // TODO: implement
     143                if (!putback_avail_() || traits_type::eq(c, gptr()[-1]))
     144                    return pbackfail(traits_type::to_int_type(c));
     145                else
     146                    return traits_type::to_int_type(*(--input_next));
    134147            }
    135148
    136149            int_type sungetc()
    137150            {
    138                 // TODO: implement
     151                if (!putback_avail_())
     152                    return pbackfail();
     153                else
     154                    return traits_type::to_int_type(*(--input_next));
    139155            }
    140156
     
    145161            int_type sputc(char_type c)
    146162            {
    147                 // TODO: implement
     163                if (!write_avail_())
     164                    return overflow(traits_type::to_int_type(c));
     165                else
     166                {
     167                    traits_type::assign(*output_next_++, c);
     168
     169                    return traits_type::to_int_type(c);
     170                }
    148171            }
    149172
    150173            streamsize sputn(const char_type* s, streamsize n)
    151174            {
    152                 // TODO: implement
     175                return xsputn(s, n);
    153176            }
    154177
    155178        protected:
    156179            basic_streambuf()
    157             {
    158                 // TODO: implement
    159             }
     180                : input_begin_{}, input_next_{}, input_end_{},
     181                  output_begin_{}, output_next_{}, output_end_{},
     182                  locale_{locale()}
     183            { /* DUMMY BODY */ }
    160184
    161185            basic_streambuf(const basic_streambuf& rhs)
    162186            {
    163                 // TODO: implement
     187                input_begin_ = rhs.input_begin_;
     188                input_next_ = rhs.input_next_;
     189                input_end_ = rhs.input_end_;
     190
     191                output_begin_ = rhs.output_begin_;
     192                output_next_ = rhs.output_next_;
     193                output_end_ = rhs.output_end_;
     194
     195                locale_ = rhs.locale_;
    164196            }
    165197
    166198            basic_strambuf& operator=(const basic_streambuf& rhs)
    167199            {
    168                 // TODO: implement
     200                input_begin_ = rhs.input_begin_;
     201                input_next_  = rhs.input_next_;
     202                input_end_   = rhs.input_end_;
     203
     204                output_begin_ = rhs.output_begin_;
     205                output_next_  = rhs.output_next_;
     206                output_end_   = rhs.output_end_;
     207
     208                locale_ = rhs.locale_;
     209
     210                return *this;
    169211            }
    170212
    171213            void swap(basic_streambuf& rhs)
    172214            {
    173                 // TODO: implement
     215                swap(input_begin_, rhs.input_begin_);
     216                swap(input_next_, rhs.input_next_);
     217                swap(input_end_, rhs.input_end_);
     218
     219                swap(output_begin_, rhs.output_begin_);
     220                swap(output_next_, rhs.output_next_);
     221                swap(output_end_, rhs.output_end_);
     222
     223                swap(locale_, rhs.locale_);
    174224            }
    175225
     
    180230            char_type* eback() const
    181231            {
    182                 // TODO: implement
     232                return input_begin_;
    183233            }
    184234
    185235            char_type* gptr() const
    186236            {
    187                 // TODO: implement
     237                return input_next_;
    188238            }
    189239
    190240            char_type* egptr() const
    191241            {
    192                 // TODO: implement
     242                return input_end_;
    193243            }
    194244
    195245            void gbump(int n)
    196246            {
    197                 // TODO: implement
     247                input_next_ += n;
    198248            }
    199249
    200250            void setg(char_type* gbeg, char_type* gnext, char_type* gend)
    201251            {
    202                 // TODO: implement
     252                input_begin_ = gbeg;
     253                input_next_  = gnext;
     254                input_end_   = gend;
    203255            }
    204256
     
    209261            char_type* pbase() const
    210262            {
    211                 // TODO: implement
     263                return output_begin_;
    212264            }
    213265
    214266            char_type* pptr() const
    215267            {
    216                 // TODO: implement
     268                return output_next_;
    217269            }
    218270
    219271            char_type* epptr() const
    220272            {
    221                 // TODO: implement
     273                return output_end_;
    222274            }
    223275
    224276            void pbump(int n)
    225277            {
    226                 // TODO: implement
     278                output_next_ += n;
    227279            }
    228280
    229281            void setp(char_type* pbeg, char_type* pend)
    230282            {
    231                 // TODO: implement
     283                output_begin_ = pbeg;
     284                output_next_  = pbeg;
     285                output_end_   = pend;
    232286            }
    233287
     
    237291
    238292            virtual void imbue(const locale& loc)
    239             {
    240                 // TODO: implement
    241             }
     293            { /* DUMMY BODY */ }
    242294
    243295            /**
     
    248300            setbuf(char_type* s, streamsize n)
    249301            {
    250                 // TODO: implement
     302                return *this;
    251303            }
    252304
     
    254306                                     ios_base::openmode which = ios_base::in | ops_base::out)
    255307            {
    256                 // TODO: implement
     308                return pos_type(off_type(-1));
    257309            }
    258310
     
    260312                                     ios_base::in | ios_base::out)
    261313            {
    262                 // TODO: implement
     314                return pos_type(off_type(-1));
    263315            }
    264316
    265317            virtual int sync()
    266318            {
    267                 // TODO: implement
     319                return 0;
    268320            }
    269321
     
    274326            virtual streamsize showmanyc()
    275327            {
    276                 // TODO: implement
     328                return 0;
    277329            }
    278330
    279331            virtual streamsize xsgetn(char_type* s, streamsize n)
    280332            {
    281                 // TODO: implement
     333                if (!s || n == 0)
     334                    return 0;
     335
     336                streamsize i{0};
     337                auto eof = traits_type::eof();
     338                for (; i <= n; ++i)
     339                {
     340                    if (!read_avail_() && traits_type::eq_int_type(uflow(), eof))
     341                        break;
     342
     343                    traits_type::asign(*s++, *input_next_++);
     344                }
     345
     346                return i;
    282347            }
    283348
    284349            virtual int_type underflow()
    285350            {
    286                 // TODO: implement
     351                return traits_type::eof();
    287352            }
    288353
    289354            virtual int_type uflow()
    290355            {
    291                 // TODO: implement
     356                auto res = underflow();
     357                if (traits_type::eq_int_type(res, traits_type::eof()))
     358                    return traits_type::eof();
     359                else
     360                    return traits_type::to_int_type(*input_next_++);
    292361            }
    293362
     
    298367            virtual int_type pbackfail(int_type c = traits_type::eof())
    299368            {
    300                 // TODO: implement
     369                return traits_type::eof();
    301370            }
    302371
     
    307376            virtual streamsize xsputn(const char_type* s, streamsize n)
    308377            {
    309                 // TODO: implement
     378                if (!s || n == 0)
     379                    return 0;
     380
     381                char* = &s[0];
     382                streamsize i{0};
     383                for (; i <= n; ++i)
     384                {
     385                    if (!write_avail_() && traits_type::eq_int_type(overflow(), eof))
     386                        break;
     387
     388                    traits_type::assign(*s++, *output_next_++);
     389                }
     390
     391                return i;
    310392            }
    311393
    312394            virtual int_type overflow(int_type c = traits_type::eof())
    313395            {
    314                 // TODO: implement
     396                return traits_type::eof();
    315397            }
    316398
    317399        private:
    318             // TODO: implement
     400            value_type* input_begin_;
     401            value_type* input_next_;
     402            value_type* input_end_;
     403
     404            value_type* output_begin_;
     405            value_type* output_next_;
     406            value_type* output_end_;
     407
     408            locale locale_;
     409
     410            bool write_avail_() const
     411            {
     412                return output_next_ && output_next_ < output_end_;
     413            }
     414
     415            bool putback_avail_() const
     416            {
     417                return input_next_ && input_begin_ < input_next_;
     418            }
     419
     420            bool read_avail_() const
     421            {
     422                return input_next_ && input_next_ < input_end_;
     423            }
    319424    };
    320425
Note: See TracChangeset for help on using the changeset viewer.