1 /******************************************************************************
2 * Copyright (c) 2014, Hobu Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include "charbuf.hpp"
36 
37 namespace lazperf
38 {
39 
initialize(char * buf,size_t count,std::ios::pos_type bufOffset)40 void charbuf::initialize(char *buf, size_t count, std::ios::pos_type bufOffset)
41 {
42     m_bufOffset = bufOffset;
43     m_buf = buf;
44     setg(buf, buf, buf + count);
45     setp(buf, buf + count);
46 }
47 
48 
seekpos(std::ios::pos_type pos,std::ios_base::openmode which)49 std::ios::pos_type charbuf::seekpos(std::ios::pos_type pos,
50     std::ios_base::openmode which)
51 {
52     pos -= m_bufOffset;
53     if (which & std::ios_base::in)
54     {
55         if (pos >= egptr() - eback())
56             return -1;
57         char *cpos = eback() + pos;
58         setg(eback(), cpos, egptr());
59     }
60     if (which & std::ios_base::out)
61     {
62         if (pos > epptr() - m_buf)
63             return -1;
64         char *cpos = m_buf + pos;
65         setp(cpos, epptr());
66     }
67     return pos;
68 }
69 
70 std::ios::pos_type
seekoff(std::ios::off_type off,std::ios_base::seekdir dir,std::ios_base::openmode which)71 charbuf::seekoff(std::ios::off_type off, std::ios_base::seekdir dir,
72     std::ios_base::openmode which)
73 {
74     std::ios::pos_type pos;
75     char *cpos = nullptr;
76     if (which & std::ios_base::in)
77     {
78         switch (dir)
79         {
80         case std::ios::beg:
81             cpos = eback() + off - m_bufOffset;
82             break;
83         case std::ios::cur:
84             cpos = gptr() + off;
85             break;
86         case std::ios::end:
87             cpos = egptr() - off;
88             break;
89         default:
90             break;  // Should never happen.
91         }
92         if (cpos < eback() || cpos > egptr())
93             return -1;
94         setg(eback(), cpos, egptr());
95         pos = cpos - eback();
96     }
97     if (which & std::ios_base::out)
98     {
99         switch (dir)
100         {
101         case std::ios::beg:
102             cpos = m_buf + off - m_bufOffset;
103             break;
104         case std::ios::cur:
105             cpos = pptr() + off;
106             break;
107         case std::ios::end:
108             cpos = egptr() - off;
109             break;
110         default:
111             break;  // Should never happen.
112         }
113         if (cpos < m_buf || cpos > epptr())
114             return -1;
115         setp(cpos, epptr());
116         pos = cpos - m_buf;
117     }
118     return pos;
119 }
120 
121 } //namespace lazperf
122