1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_BIN_UTILS_H_ |
6 | #define RUNTIME_BIN_UTILS_H_ |
7 | |
8 | #include <stdlib.h> |
9 | #include <string.h> |
10 | |
11 | #include "include/dart_api.h" |
12 | #include "platform/globals.h" |
13 | #include "platform/utils.h" |
14 | |
15 | namespace dart { |
16 | namespace bin { |
17 | |
18 | class OSError { |
19 | public: |
20 | enum SubSystem { kSystem, kGetAddressInfo, kBoringSSL, kUnknown = -1 }; |
21 | |
22 | OSError(); |
23 | OSError(int code, const char* message, SubSystem sub_system) { |
24 | sub_system_ = sub_system; |
25 | code_ = code; |
26 | message_ = NULL; // SetMessage will free existing message. |
27 | SetMessage(message); |
28 | } |
29 | virtual ~OSError() { free(message_); } |
30 | |
31 | // Reload this OSError with the current OS error, discarding the previous. |
32 | void Reload(); |
33 | |
34 | SubSystem sub_system() { return sub_system_; } |
35 | int code() { return code_; } |
36 | char* message() { return message_; } |
37 | void SetCodeAndMessage(SubSystem sub_system, int code); |
38 | |
39 | private: |
40 | void set_sub_system(SubSystem sub_system) { sub_system_ = sub_system; } |
41 | void set_code(int code) { code_ = code; } |
42 | void SetMessage(const char* message) { |
43 | free(message_); |
44 | if (message == NULL) { |
45 | message_ = NULL; |
46 | } else { |
47 | message_ = Utils::StrDup(message); |
48 | } |
49 | } |
50 | |
51 | SubSystem sub_system_; |
52 | int code_; |
53 | char* message_; |
54 | |
55 | DISALLOW_COPY_AND_ASSIGN(OSError); |
56 | }; |
57 | |
58 | class StringUtils { |
59 | public: |
60 | // The following methods convert the argument if needed. The |
61 | // conversions are only needed on Windows. If the methods returns a |
62 | // pointer that is different from the input pointer, the returned |
63 | // pointer is allocated with malloc and should be freed using free. |
64 | // |
65 | // If the len argument is passed then that number of characters are |
66 | // converted. If len is -1, conversion will stop at the first NUL |
67 | // character. If result_len is not NUL, it is used to set the number |
68 | // of characters in the result. |
69 | // |
70 | // These conversion functions are only implemented on Windows as the |
71 | // Dart code only hit this path on Windows. |
72 | static const char* ConsoleStringToUtf8(const char* str, |
73 | intptr_t len = -1, |
74 | intptr_t* result_len = NULL); |
75 | static char* ConsoleStringToUtf8(char* str, |
76 | intptr_t len = -1, |
77 | intptr_t* result_len = NULL); |
78 | static const char* Utf8ToConsoleString(const char* utf8, |
79 | intptr_t len = -1, |
80 | intptr_t* result_len = NULL); |
81 | static char* Utf8ToConsoleString(char* utf8, |
82 | intptr_t len = -1, |
83 | intptr_t* result_len = NULL); |
84 | |
85 | private: |
86 | DISALLOW_ALLOCATION(); |
87 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringUtils); |
88 | }; |
89 | |
90 | class ShellUtils { |
91 | public: |
92 | // Convert all the arguments to UTF8. On Windows, the arguments are |
93 | // encoded in the current code page and not UTF8. |
94 | // |
95 | // Returns true if the arguments are converted. In that case |
96 | // each of the arguments need to be deallocated using free. |
97 | static bool GetUtf8Argv(int argc, char** argv); |
98 | |
99 | private: |
100 | DISALLOW_ALLOCATION(); |
101 | DISALLOW_IMPLICIT_CONSTRUCTORS(ShellUtils); |
102 | }; |
103 | |
104 | class TimerUtils { |
105 | public: |
106 | static void InitOnce(); |
107 | static int64_t GetCurrentMonotonicMicros(); |
108 | static int64_t GetCurrentMonotonicMillis(); |
109 | static void Sleep(int64_t millis); |
110 | |
111 | private: |
112 | DISALLOW_ALLOCATION(); |
113 | DISALLOW_IMPLICIT_CONSTRUCTORS(TimerUtils); |
114 | }; |
115 | |
116 | } // namespace bin |
117 | } // namespace dart |
118 | |
119 | #endif // RUNTIME_BIN_UTILS_H_ |
120 | |