Chromium Code Reviews| Index: native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.cc |
| diff --git a/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.cc b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a905d075e8a741853e1b4d36ff3f65d2107631ac |
| --- /dev/null |
| +++ b/native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "fake_ppapi/fake_util.h" |
| + |
| +#include <string.h> |
| + |
| +#include <algorithm> |
| + |
| +#include <ppapi/c/pp_completion_callback.h> |
| +#include <ppapi/c/pp_errors.h> |
| + |
| +#include "gtest/gtest.h" |
| + |
| +// Helper function to call the completion callback if it is defined (an |
| +// asynchronous call), or return the result directly if it isn't (a synchronous |
| +// call). |
| +// |
| +// Use like this: |
| +// if (<some error condition>) |
| +// return RunCompletionCallback(callback, PP_ERROR_FUBAR); |
| +// |
| +// /* Everything worked OK */ |
| +// return RunCompletionCallback(callback, PP_OK); |
| +int32_t RunCompletionCallback(PP_CompletionCallback* callback, int32_t result) { |
| + if (callback->func) { |
| + PP_RunCompletionCallback(callback, result); |
| + return PP_OK_COMPLETIONPENDING; |
| + } |
| + return result; |
| +} |
| + |
| +bool GetHeaderValue(const std::string& headers, |
| + const std::string& key, |
| + std::string* out_value) { |
| + out_value->clear(); |
| + |
| + size_t offset = 0; |
| + while (offset != std::string::npos) { |
| + // Find the next colon; this separates the key from the value. |
| + size_t colon = headers.find(':', offset); |
| + if (colon == std::string::npos) |
| + return false; |
| + |
| + // Find the newline; this separates the value from the next header. |
| + size_t newline = headers.find('\n', colon); |
| + if (strncasecmp(key.c_str(), &headers.data()[offset], key.size()) != 0) { |
| + // Key doesn't match, skip to next header. |
| + offset = newline + 1; |
| + continue; |
| + } |
| + |
| + // Key matches, extract value. First, skip leading spaces. |
| + size_t nonspace = headers.find_first_not_of(' ', colon + 1); |
| + if (nonspace == std::string::npos) |
| + return false; |
| + |
| + out_value->assign(headers, nonspace, newline - nonspace); |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +int32_t IntegerToString(size_t integer, std::string* out_string) { |
|
chanpatorikku
2016/08/29 18:10:39
Sorry. The function was not meant to be in the pat
binji
2016/08/30 01:39:35
You can just upload a new patch with these removed
chanpatorikku
2016/09/06 14:49:38
Done. Some more confidence about not having code
|
| + out_string->clear(); |
| + |
| + out_string->resize(33); |
| + |
| + int char_written = |
| + snprintf(&(*out_string)[0], out_string->size(), "%i", integer); |
| + |
| + // cast signed int on size but not signed long long int on both operands |
| + // because size <= INT_MAX. |
| + if (char_written < 0 || char_written >= (signed int) out_string->size()) { |
| + return PP_ERROR_FAILED; |
| + } |
| + return PP_OK; |
| +} |
| + |