| 943 | } |
| 944 | |
| 945 | std::string XPathIo::writeDataToFile(const std::string& orgPath) { |
| 946 | Protocol prot = fileProtocol(orgPath); |
| 947 | |
| 948 | // generating the name for temp file. |
| 949 | std::time_t timestamp = std::time(nullptr); |
| 950 | std::stringstream ss; |
| 951 | ss << timestamp << XPathIo::TEMP_FILE_EXT; |
| 952 | std::string path = ss.str(); |
| 953 | |
| 954 | if (prot == pStdin) { |
| 955 | if (isatty(fileno(stdin))) |
| 956 | throw Error(ErrorCode::kerInputDataReadFailed); |
| 957 | #ifdef _WIN32 |
| 958 | // convert stdin to binary |
| 959 | if (_setmode(_fileno(stdin), _O_BINARY) == -1) |
| 960 | throw Error(ErrorCode::kerInputDataReadFailed); |
| 961 | #endif |
| 962 | std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); |
| 963 | // read stdin and write to the temp file. |
| 964 | char readBuf[100 * 1024]; |
| 965 | std::streamsize readBufSize = 0; |
| 966 | do { |
| 967 | std::cin.read(readBuf, sizeof(readBuf)); |
| 968 | readBufSize = std::cin.gcount(); |
| 969 | if (readBufSize > 0) { |
| 970 | fs.write(readBuf, readBufSize); |
| 971 | } |
| 972 | } while (readBufSize); |
| 973 | fs.close(); |
| 974 | } else if (prot == pDataUri) { |
| 975 | std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); |
| 976 | // read data uri and write to the temp file. |
| 977 | size_t base64Pos = orgPath.find("base64,"); |
| 978 | if (base64Pos == std::string::npos) { |
| 979 | fs.close(); |
| 980 | throw Error(ErrorCode::kerErrorMessage, "No base64 data"); |
| 981 | } |
| 982 | |
| 983 | std::string data = orgPath.substr(base64Pos + 7); |
| 984 | std::vector<char> decodeData(data.length()); |
| 985 | auto size = base64decode(data.c_str(), decodeData.data(), data.length()); |
| 986 | if (size > 0) { |
| 987 | fs.write(decodeData.data(), size); |
| 988 | fs.close(); |
| 989 | } else { |
| 990 | fs.close(); |
| 991 | throw Error(ErrorCode::kerErrorMessage, "Unable to decode base 64."); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | return path; |
| 996 | } |
| 997 | |
| 998 | #endif |
| 999 |
nothing calls this directly
no test coverage detected