1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Input.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <cstring>
20 
21 namespace pp
22 {
23 
Input()24 Input::Input() : mCount(0), mString(0)
25 {
26 }
27 
Input(int count,const char * const string[],const int length[])28 Input::Input(int count, const char* const string[], const int length[]) :
29 	mCount(count),
30 	mString(string)
31 {
32 	assert(mCount >= 0);
33 	mLength.reserve(mCount);
34 	for (int i = 0; i < mCount; ++i)
35 	{
36 		int len = length ? length[i] : -1;
37 		mLength.push_back(len < 0 ? strlen(mString[i]) : len);
38 	}
39 }
40 
read(char * buf,int maxSize)41 int Input::read(char* buf, int maxSize)
42 {
43 	int nRead = 0;
44 	while ((nRead < maxSize) && (mReadLoc.sIndex < mCount))
45 	{
46 		int size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
47 		size = std::min(size, maxSize);
48 		memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
49 		nRead += size;
50 		mReadLoc.cIndex += size;
51 
52 		// Advance string if we reached the end of current string.
53 		if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
54 		{
55 			++mReadLoc.sIndex;
56 			mReadLoc.cIndex = 0;
57 		}
58 	}
59 	return nRead;
60 }
61 
62 }  // namespace pp
63 
64