blob: 11868c1cd5b995ff84f1181871452e14fe79e50c [file] [log] [blame]
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00001//
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.org98eec912012-05-01 10:04:08 +00009#include <algorithm>
10#include <cassert>
alokp@chromium.org99b5c0c2012-05-17 20:44:52 +000011#include <cstring>
alokp@chromium.org98eec912012-05-01 10:04:08 +000012
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000013namespace pp
14{
15
alokp@chromium.org30a487c2012-05-02 17:30:46 +000016Input::Input() : mCount(0), mString(0)
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000017{
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000018}
19
alokp@chromium.org98eec912012-05-01 10:04:08 +000020Input::Input(int count, const char* const string[], const int length[]) :
21 mCount(count),
alokp@chromium.org30a487c2012-05-02 17:30:46 +000022 mString(string)
alokp@chromium.org98eec912012-05-01 10:04:08 +000023{
24 assert(mCount >= 0);
alokp@chromium.org30a487c2012-05-02 17:30:46 +000025 mLength.reserve(mCount);
alokp@chromium.org98eec912012-05-01 10:04:08 +000026 for (int i = 0; i < mCount; ++i)
27 {
alokp@chromium.org30a487c2012-05-02 17:30:46 +000028 int len = length ? length[i] : -1;
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000029 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
alokp@chromium.org98eec912012-05-01 10:04:08 +000030 }
31}
32
alokp@chromium.org98eec912012-05-01 10:04:08 +000033int 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.coma16a55f2012-12-20 20:51:54 +000040 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
alokp@chromium.org98eec912012-05-01 10:04:08 +000041 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.org4e4b8072011-08-07 05:36:04 +000054} // namespace pp
55