Clapkit
Loading...
Searching...
No Matches
ckUtils.h
Go to the documentation of this file.
1/*
2 *
3 * Clapkit
4 * ----------------------------------------------------------------------
5 * A wrapper for creating a 'generalized' app for Classic MacOS
6 * that (hopefully) can be ported easily to other platforms.
7 *
8 * CKUtils
9 * ----------------------------------------------------------------------
10 * Utilies for commonly done actions.
11 *
12 */
13
14#pragma once
15
16#include "ckMemory.h"
17#include "dlmalloc.h"
18#include <cstddef>
19#include <cstdlib>
20#include <cstring>
21#include <stdarg.h>
22#include <stdio.h>
23
24#ifdef kCKAPPDEBUG
25#define __FILENAME__ __FILE__
26#else
27#define __FILENAME__ ""
28#endif
29
34#define CKMSToTicks(milliseconds) ((int)((milliseconds) / 16.66))
35
36#ifdef kCKAPPDEBUG
37
45#define CKLog(s, ...) __CKDebugLog(0, s __VA_OPT__(, ) __VA_ARGS__)
46#define CKDebugLog(l, s, ...) __CKDebugLog(l, s __VA_OPT__(, ) __VA_ARGS__)
47#else
48
53#define CKLog(s, ...) \
54 do { \
55 } while (0)
56#define CKDebugLog(l, s, ...) \
57 do { \
58 } while (0)
59#endif
60
61unsigned char* __CKC2P(const char* src, const char* func, int line, const char* file);
62char* __CKP2C(const unsigned char* src, const char* func, int line, const char* file);
63
64#ifdef kCKAPPDEBUG
70#define CKC2P(s) __CKC2P(s, __func__, __LINE__, __FILENAME__)
71
77#define CKP2C(s) __CKP2C(s, __func__, __LINE__, __FILENAME__)
78#else
83#define CKC2P(s) __CKC2P(s, "", 0, "")
84
90#define CKP2C(s) __CKP2C(s, "", 0, "")
91#endif
92
93void __CKDebugLog(int level, const char* s, ...);
94void CKConsolePrint(const char* toPrint);
95
99#ifdef kCKAPPDEBUG
100#include <Timer.h>
101#include <vector>
103 public:
104 char* name;
106 int calls;
107};
108extern std::vector<CKProfilerData*> _profilerData;
109extern bool _profilerDataInit;
111 public:
112 CKScopeProfiler(const char* fName) {
113 this->name = (char*)dlmalloc(strlen(fName) + 1);
114 strcpy(this->name, fName);
115 // CKLog("PROFILE: %s started.", this->name);
116 this->start = TickCount();
117 }
119 long time = (TickCount() - this->start);
120 // CKLog("PROFILE: %s ended. Took %lu ticks.", this->name, time);
121 bool found = false;
122 if (!_profilerDataInit) {
123 _profilerData = std::vector<CKProfilerData*>();
124 _profilerDataInit = true;
125 }
126 for (auto& d : _profilerData) {
127 if (strcmp(d->name, this->name) == 0) {
128 d->totalTime += time;
129 d->calls++;
130 found = true;
131 }
132 }
133 if (!found) {
134 CKProfilerData* nd = new CKProfilerData();
135 nd->name = (char*)dlmalloc(strlen(this->name) + 1);
136 strcpy(nd->name, this->name);
137 nd->totalTime = time;
138 nd->calls = 1;
139 _profilerData.push_back(nd);
140 }
141 dlfree(this->name);
142 }
143
144 private:
145 char* name;
146 long start;
147};
148void CKPrintProfileData();
150void __CKWriteToExitFile(const char* s, ...);
151#define CKPROFILE CKScopeProfiler __p(__PRETTY_FUNCTION__);
152#else
153#define CKPROFILE
154#endif
155
158bool CKHasIconUtilities();
159bool CKHasDebugger();
160UInt32 CKMillis();
void __CKWriteToExitFile(const char *s,...)
Definition ckUtils.cpp:177
std::vector< CKProfilerData * > _profilerData
Definition ckUtils.cpp:21
void __CKDebugLog(int level, const char *s,...)
Definition ckUtils.cpp:78
void CKPrintExitDebugData()
Definition ckUtils.cpp:215
bool _profilerDataInit
Definition ckUtils.cpp:22
void CKConsolePrint(const char *toPrint)
Definition ckUtils.cpp:150
char * __CKP2C(const unsigned char *src, const char *func, int line, const char *file)
Definition ckUtils.cpp:55
unsigned char * __CKC2P(const char *src, const char *func, int line, const char *file)
Definition ckUtils.cpp:29
void CKPrintProfileData()
Definition ckUtils.cpp:246
Definition ckUtils.h:102
long totalTime
Definition ckUtils.h:105
int calls
Definition ckUtils.h:106
char * name
Definition ckUtils.h:104
Definition ckUtils.h:110
~CKScopeProfiler()
Definition ckUtils.h:118
CKScopeProfiler(const char *fName)
Definition ckUtils.h:112
bool CKHasIconUtilities()
Checks if Icon Utilities are available (PlotIcon/GetCIcon, etc.)
Definition ckUtils.cpp:300
bool CKHasAppearanceManager()
Checks if Appearance Manager is present (8.x+)
Definition ckUtils.cpp:274
bool CKHasDebugger()
Checks if a low-level debugger (like MacsBug) is installed and callable.
Definition ckUtils.cpp:315
bool CKHasColorQuickDraw()
Checks if Color QuickDraw is available.
Definition ckUtils.cpp:285
UInt32 CKMillis()
Return the number of milliseconds since computer booted up.
Definition ckUtils.cpp:335