|
1 // |
|
2 // Copyright (c) 2011 The ANGLE Project Authors. All rights reserved. |
|
3 // Use of this source code is governed by a BSD-style license that can be |
|
4 // found in the LICENSE file. |
|
5 // |
|
6 |
|
7 #include "Input.h" |
|
8 |
|
9 #include <algorithm> |
|
10 #include <cassert> |
|
11 #include <cstring> |
|
12 |
|
13 namespace pp |
|
14 { |
|
15 |
|
16 Input::Input() : mCount(0), mString(0) |
|
17 { |
|
18 } |
|
19 |
|
20 Input::Input(size_t count, const char* const string[], const int length[]) : |
|
21 mCount(count), |
|
22 mString(string) |
|
23 { |
|
24 mLength.reserve(mCount); |
|
25 for (size_t i = 0; i < mCount; ++i) |
|
26 { |
|
27 int len = length ? length[i] : -1; |
|
28 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len); |
|
29 } |
|
30 } |
|
31 |
|
32 size_t Input::read(char* buf, size_t maxSize) |
|
33 { |
|
34 size_t nRead = 0; |
|
35 while ((nRead < maxSize) && (mReadLoc.sIndex < mCount)) |
|
36 { |
|
37 size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; |
|
38 size = std::min(size, maxSize); |
|
39 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size); |
|
40 nRead += size; |
|
41 mReadLoc.cIndex += size; |
|
42 |
|
43 // Advance string if we reached the end of current string. |
|
44 if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
|
45 { |
|
46 ++mReadLoc.sIndex; |
|
47 mReadLoc.cIndex = 0; |
|
48 } |
|
49 } |
|
50 return nRead; |
|
51 } |
|
52 |
|
53 } // namespace pp |
|
54 |