cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / ppapi / native_client / src / trusted / plugin / utility.cc
blob1458e185ce08abc46e8b1dde6e5efd8d948b8708
1 /*
2 * Copyright (c) 2011 The Chromium 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 */
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "ppapi/native_client/src/trusted/plugin/utility.h"
13 namespace plugin {
15 int gNaClPluginDebugPrintEnabled = -1;
16 FILE* gNaClPluginLogFile = NULL;
19 * Prints formatted message to the log.
21 int NaClPluginPrintLog(const char *format, ...) {
22 if (NULL == gNaClPluginLogFile) {
23 return 0;
25 va_list arg;
26 int done;
27 va_start(arg, format);
28 done = vfprintf(gNaClPluginLogFile, format, arg);
29 va_end(arg);
30 fflush(gNaClPluginLogFile);
31 return done;
35 * Opens file where plugin log should be written. The file name is
36 * taken from NACL_PLUGIN_LOG environment variable.
37 * If environment variable doesn't exist or file can't be opened,
38 * the function returns stdout.
40 FILE* NaClPluginLogFileEnv() {
41 char* file = getenv("NACL_PLUGIN_LOG");
42 if (NULL != file) {
43 FILE* log_file = fopen(file, "w+");
44 if (NULL == log_file) {
45 return stdout;
47 return log_file;
49 return stdout;
53 * Currently looks for presence of NACL_PLUGIN_DEBUG and returns
54 * 0 if absent and 1 if present. In the future we may include notions
55 * of verbosity level.
57 int NaClPluginDebugPrintCheckEnv() {
58 char* env = getenv("NACL_PLUGIN_DEBUG");
59 return (NULL != env);
62 bool IsValidIdentifierString(const char* strval, uint32_t* length) {
63 // This function is supposed to recognize valid ECMAScript identifiers,
64 // as described in
65 // http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
66 // It is currently restricted to only the ASCII subset.
67 // TODO(sehr): Recognize the full Unicode formulation of identifiers.
68 // TODO(sehr): Make this table-driven if efficiency becomes a problem.
69 if (NULL != length) {
70 *length = 0;
72 if (NULL == strval) {
73 return false;
75 static const char* kValidFirstChars =
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_";
77 static const char* kValidOtherChars =
78 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz$_"
79 "0123456789";
80 if (NULL == strchr(kValidFirstChars, strval[0])) {
81 return false;
83 uint32_t pos;
84 for (pos = 1; ; ++pos) {
85 if (0 == pos) {
86 // Unsigned overflow.
87 return false;
89 int c = strval[pos];
90 if (0 == c) {
91 break;
93 if (NULL == strchr(kValidOtherChars, c)) {
94 return false;
97 if (NULL != length) {
98 *length = pos;
100 return true;
103 } // namespace plugin