Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: experimental/conways_life/scripting/scripting_bridge.cc

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "experimental/conways_life/scripting/scripting_bridge.h"
6
7 #include <string>
8
9 namespace {
10 const char* const kWhiteSpaceCharacters = " \t";
11
12 // Helper function to pull out the next token in |token_string|. A token is
13 // delimited by whitespace. Scanning begins at |*pos|, if pos goes beyond the
14 // end of |token_string|, it is set to std::string::npos and an empty string
15 // is returned. On return, |*pos| will point to the beginning of the next
16 // token. |pos| must not be NULL.
17 const std::string ScanToken(const std::string& token_string, size_t* pos) {
18 std::string token;
19 if (*pos == std::string::npos) {
20 return token;
21 }
22 size_t token_start_pos = token_string.find_first_not_of(kWhiteSpaceCharacters,
23 *pos);
24 size_t token_end_pos = token_string.find_first_of(kWhiteSpaceCharacters,
25 token_start_pos);
26 if (token_start_pos != std::string::npos) {
27 token = token_string.substr(token_start_pos, token_end_pos);
28 }
29 *pos = token_end_pos;
30 return token;
31 }
32
33 // Take a string of the form 'name:value' and split it into two strings, one
34 // containing 'name' and the other 'value'. If the ':' separator is missing,
35 // or is the last character in |parameter|, |parameter| is copied to
36 // |param_name|, |param_value| is left unchanged and false is returned.
37 bool ParseParameter(const std::string& parameter,
38 std::string* param_name,
39 std::string* param_value) {
40 bool success = false;
41 size_t sep_pos = parameter.find_first_of(':');
42 if (sep_pos != std::string::npos) {
43 *param_name = parameter.substr(0, sep_pos);
44 if (sep_pos < parameter.length() - 1) {
45 *param_value = parameter.substr(sep_pos + 1);
46 success = true;
47 } else {
48 success = false;
49 }
50 } else {
51 *param_name = parameter;
52 success = false;
53 }
54 return success;
55 }
56 } // namespace
57
58 namespace scripting {
59
60 bool ScriptingBridge::AddMethodNamed(const std::string& method_name,
61 SharedMethodCallbackExecutor method) {
62 if (method_name.size() == 0 || method == NULL)
63 return false;
64 method_dictionary_.insert(
65 std::pair<std::string, SharedMethodCallbackExecutor>(method_name,
66 method));
67 return true;
68 }
69
70 bool ScriptingBridge::InvokeMethod(const std::string& method) {
71 size_t current_pos = 0;
72 const std::string method_name = ScanToken(method, &current_pos);
73 MethodDictionary::iterator method_iter;
74 method_iter = method_dictionary_.find(method_name);
75 if (method_iter != method_dictionary_.end()) {
76 // Pull out the method parameters and build a dictionary that maps
77 // parameter names to values.
78 std::map<std::string, std::string> param_dict;
79 while (current_pos != std::string::npos) {
80 const std::string parameter = ScanToken(method, &current_pos);
81 if (parameter.length()) {
82 std::string param_name;
83 std::string param_value;
84 if (ParseParameter(parameter, &param_name, &param_value)) {
85 // Note that duplicate parameter names will override each other. The
86 // last one in the method string will be used.
87 param_dict[param_name] = param_value;
88 }
89 }
90 }
91 (*method_iter->second).Execute(*this, param_dict);
92 return true;
93 }
94 return false;
95 }
96
97 } // namespace scripting
OLDNEW
« no previous file with comments | « experimental/conways_life/scripting/scripting_bridge.h ('k') | experimental/conways_life/sounds/ahem_x.wav » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698