aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/stream/PreallocatedStreamBuf.cpp
blob: f656fc8613879b9a53955a33fe3aa94eceedd011 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/core/utils/stream/PreallocatedStreamBuf.h>
#include <cassert>

namespace Aws
{
    namespace Utils
    {
        namespace Stream
        {
            PreallocatedStreamBuf::PreallocatedStreamBuf(unsigned char* buffer, uint64_t lengthToRead) :
                m_underlyingBuffer(buffer), m_lengthToRead(lengthToRead)
            {
                char* end = reinterpret_cast<char*>(m_underlyingBuffer + m_lengthToRead);
                char* begin = reinterpret_cast<char*>(m_underlyingBuffer);
                setp(begin, end);
                setg(begin, begin, end);
            }

            PreallocatedStreamBuf::pos_type PreallocatedStreamBuf::seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which)
            {
                if (dir == std::ios_base::beg)
                {
                    return seekpos(off, which);
                }
                else if (dir == std::ios_base::end)
                {
                    return seekpos(m_lengthToRead - off, which);
                }
                else if (dir == std::ios_base::cur)
                {
                    if(which == std::ios_base::in)
                    { 
                        return seekpos((gptr() - reinterpret_cast<char*>(m_underlyingBuffer)) + off, which);
                    }
                    else
                    {
                        return seekpos((pptr() - reinterpret_cast<char*>(m_underlyingBuffer)) + off, which);
                    }
                }

                return off_type(-1);
            }

            PreallocatedStreamBuf::pos_type PreallocatedStreamBuf::seekpos(pos_type pos, std::ios_base::openmode which)
            {
                assert(static_cast<size_t>(pos) <= m_lengthToRead);
                if (static_cast<size_t>(pos) > m_lengthToRead)
                {
                    return pos_type(off_type(-1));
                }

                char* end = reinterpret_cast<char*>(m_underlyingBuffer + m_lengthToRead);
                char* begin = reinterpret_cast<char*>(m_underlyingBuffer);

                if (which == std::ios_base::in)
                {
                    setg(begin, begin + static_cast<size_t>(pos), end);
                }

                if (which == std::ios_base::out)
                {
                    setp(begin + static_cast<size_t>(pos), end);
                }

                return pos;
            }
        }
    }
}