| 1 | // OVUtility.h: Inline utilities |
| 2 | // |
| 3 | // Copyright (c) 2004-2007 The OpenVanilla Project (http://openvanilla.org) |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions |
| 8 | // are met: |
| 9 | // |
| 10 | // 1. Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer in the |
| 14 | // documentation and/or other materials provided with the distribution. |
| 15 | // 3. Neither the name of OpenVanilla nor the names of its contributors |
| 16 | // may be used to endorse or promote products derived from this software |
| 17 | // without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | // POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | #ifndef __OVUtility_h |
| 32 | #define __OVUtility_h |
| 33 | |
| 34 | #include <ctime> |
| 35 | using namespace std; |
| 36 | |
| 37 | class Watch |
| 38 | { |
| 39 | public: |
| 40 | void start() |
| 41 | { |
| 42 | tick_ = 0L; |
| 43 | sec_ = 0.0; |
| 44 | #ifdef OV_DEBUG |
| 45 | from_ = clock(); |
| 46 | #else |
| 47 | from_ = 0L; |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | void stop() |
| 52 | { |
| 53 | #ifdef OV_DEBUG |
| 54 | tick_ = clock() - from_; |
| 55 | sec_ = static_cast<double>(tick_) / CLOCKS_PER_SEC; |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | clock_t getTick() { return tick_; } |
| 60 | double getSec() { return sec_; } |
| 61 | |
| 62 | private: |
| 63 | clock_t from_; |
| 64 | clock_t tick_; |
| 65 | double sec_; |
| 66 | }; |
| 67 | |
| 68 | //<comment author='b6s'> |
| 69 | //The following three headers are for WIN32 specific debug log. |
| 70 | #if defined(WIN32) && defined(OV_DEBUG) |
| 71 | #include <windows.h> |
| 72 | #include <shlobj.h> |
| 73 | #include <io.h> |
| 74 | #endif |
| 75 | //</comment> |
| 76 | |
| 77 | // debug routine |
| 78 | #ifdef OV_DEBUG |
| 79 | #include <stdarg.h> |
| 80 | #include <stdio.h> |
| 81 | |
| 82 | // Thanks to modern C++ compiler, we can write this in the header file. |
| 83 | // No extra compiling/library making/linking is even needed! |
| 84 | |
| 85 | inline static void murmur(const char* format,...) { |
| 86 | #ifdef WIN32 |
| 87 | //@warning OV-specific paths here! |
| 88 | //<comment author='b6s'> |
| 89 | //The following codes of paths are redundant to AVConfig.cpp, |
| 90 | //however, they're necessary... |
| 91 | char OV_BASEDIR_PRE[MAX_PATH]; |
| 92 | char OV_USERDIR_PRE[MAX_PATH]; |
| 93 | |
| 94 | //GetWindowsDirectoryA(OV_BASEDIR_PRE, MAX_PATH - 14); |
| 95 | //sprintf(OV_BASEDIR_PRE, "%s\\%s", OV_BASEDIR_PRE, "OpenVanilla\\"); |
| 96 | strcpy(OV_BASEDIR_PRE, "C:\\OpenVanilla\\"); |
| 97 | |
| 98 | if (SHGetFolderPathA( |
| 99 | NULL, |
| 100 | CSIDL_APPDATA|CSIDL_FLAG_CREATE, |
| 101 | NULL, |
| 102 | 0, |
| 103 | OV_USERDIR_PRE) >= 0) { |
| 104 | sprintf(OV_USERDIR_PRE, "%s\\%s", OV_USERDIR_PRE, "OpenVanilla\\"); |
| 105 | if (_access (OV_USERDIR_PRE, 0)) |
| 106 | CreateDirectoryA(OV_USERDIR_PRE, NULL); |
| 107 | } else |
| 108 | sprintf(OV_USERDIR_PRE, "%s\\%s", OV_BASEDIR_PRE, "User\\"); |
| 109 | |
| 110 | char OV_WIN32_DEBUG_LOG_PATH[MAX_PATH]; |
| 111 | sprintf(OV_WIN32_DEBUG_LOG_PATH, "%s\\%s", OV_USERDIR_PRE, "orz.txt"); |
| 112 | //</comment> |
| 113 | |
| 114 | FILE *fp; |
| 115 | if (fp=fopen(OV_WIN32_DEBUG_LOG_PATH, "a")) { |
| 116 | va_list args; |
| 117 | va_start (args, format); |
| 118 | vfprintf (fp, format, args); |
| 119 | va_end (args); |
| 120 | fprintf (fp, "\n"); |
| 121 | fclose(fp); |
| 122 | } |
| 123 | #else |
| 124 | va_list args; |
| 125 | va_start (args, format); |
| 126 | vfprintf (stderr, format, args); |
| 127 | va_end (args); |
| 128 | fprintf (stderr, "\n"); |
| 129 | #endif |
| 130 | } |
| 131 | #else |
| 132 | inline static void murmur(const char* format,...) {} |
| 133 | #endif |
| 134 | |
| 135 | #endif |