alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <cassert> |
alokp@chromium.org | 99b5c0c | 2012-05-17 20:44:52 +0000 | [diff] [blame] | 11 | #include <cstring> |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 12 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 13 | namespace pp |
| 14 | { |
| 15 | |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 16 | Input::Input() : mCount(0), mString(0) |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 17 | { |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 18 | } |
| 19 | |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 20 | Input::Input(int count, const char* const string[], const int length[]) : |
| 21 | mCount(count), |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 22 | mString(string) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 23 | { |
| 24 | assert(mCount >= 0); |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 25 | mLength.reserve(mCount); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 26 | for (int i = 0; i < mCount; ++i) |
| 27 | { |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 28 | int len = length ? length[i] : -1; |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 29 | mLength.push_back(len < 0 ? std::strlen(mString[i]) : len); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 33 | int Input::read(char* buf, int maxSize) |
| 34 | { |
| 35 | int nRead = 0; |
| 36 | while ((nRead < maxSize) && (mReadLoc.sIndex < mCount)) |
| 37 | { |
| 38 | int size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; |
| 39 | size = std::min(size, maxSize); |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 40 | std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 41 | nRead += size; |
| 42 | mReadLoc.cIndex += size; |
| 43 | |
| 44 | // Advance string if we reached the end of current string. |
| 45 | if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
| 46 | { |
| 47 | ++mReadLoc.sIndex; |
| 48 | mReadLoc.cIndex = 0; |
| 49 | } |
| 50 | } |
| 51 | return nRead; |
| 52 | } |
| 53 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 54 | } // namespace pp |
| 55 | |