blob: b4d970a97d0cd084bef5976b3b24b3da98bcfbae [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
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000020Input::Input(size_t count, const char* const string[], const int length[]) :
alokp@chromium.org98eec912012-05-01 10:04:08 +000021 mCount(count),
alokp@chromium.org30a487c2012-05-02 17:30:46 +000022 mString(string)
alokp@chromium.org98eec912012-05-01 10:04:08 +000023{
alokp@chromium.org30a487c2012-05-02 17:30:46 +000024 mLength.reserve(mCount);
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000025 for (size_t i = 0; i < mCount; ++i)
alokp@chromium.org98eec912012-05-01 10:04:08 +000026 {
alokp@chromium.org30a487c2012-05-02 17:30:46 +000027 int len = length ? length[i] : -1;
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000028 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
alokp@chromium.org98eec912012-05-01 10:04:08 +000029 }
30}
31
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000032size_t Input::read(char* buf, size_t maxSize)
alokp@chromium.org98eec912012-05-01 10:04:08 +000033{
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000034 size_t nRead = 0;
alokp@chromium.org98eec912012-05-01 10:04:08 +000035 while ((nRead < maxSize) && (mReadLoc.sIndex < mCount))
36 {
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000037 size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
alokp@chromium.org98eec912012-05-01 10:04:08 +000038 size = std::min(size, maxSize);
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000039 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
alokp@chromium.org98eec912012-05-01 10:04:08 +000040 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
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000053} // namespace pp
54