-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinput.cpp
56 lines (44 loc) · 1.12 KB
/
input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "faasm/input.h"
#include "faasm/core.h"
#include <stdint.h>
#include <string.h>
#include <string>
namespace faasm {
const char* getStringInput(const char* defaultValue)
{
long inputSize = faasmGetInputSize();
if (inputSize == 0) {
return defaultValue;
}
auto inputBuffer = new uint8_t[inputSize + 1];
faasmGetInput(inputBuffer, inputSize);
// Force null-termination
inputBuffer[inputSize] = 0;
char* strIn = reinterpret_cast<char*>(inputBuffer);
return strIn;
}
int getIntInput()
{
const char* inputStr = faasm::getStringInput("0");
int intVal = std::stoi(inputStr);
return intVal;
}
void setStringOutput(const char* val)
{
faasmSetOutput(val, strlen(val));
}
int* parseStringToIntArray(const char* strIn, int nInts)
{
char* strCopy = new char[strlen(strIn)];
strcpy(strCopy, strIn);
char* nextSubstr = strtok(strCopy, " ");
int* result = new int[nInts];
int i = 0;
while (nextSubstr != NULL) {
result[i] = std::stoi(nextSubstr);
nextSubstr = strtok(NULL, " ");
i++;
}
return result;
}
} // namespace faasm