-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtil.cpp
179 lines (137 loc) · 3.42 KB
/
Util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <cctype>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <stdio.h>
#include "Util.h"
#define kTwoPower32 (4294967296.0) /* 2^32 */
char* Util::PtoCStr(unsigned char *s)
{
int theLen;
int t;
theLen = s[0];
for (t = 0; t<theLen; t++)
s[t] = s[t + 1];
s[theLen] = '\0';
return (char *)s;
}
std::string Util::PtoStr(unsigned char *s)
{
return std::string(PtoCStr(s));
}
unsigned char* Util::CtoPStr(char *s)
{
int theLen;
int t;
theLen = strlen(s);
for (t = theLen; t >= 1; t--)
s[t] = s[t - 1];
s[0] = (char)theLen;
return (unsigned char *)s;
}
unsigned char* Util::StrToPStr(const string& str)
{
// Work off a copy
string text = str.c_str();
char* s = (char*)text.c_str();
int theLen;
int t;
theLen = strlen(s);
for (t = theLen; t >= 1; t--)
s[t] = s[t - 1];
s[0] = (char)theLen;
return (unsigned char *)s;
}
void Util::Debug(std::string msg)
{
FILE *fp;
fp = fopen("Mac HD (68K):spot.txt", "a");
if (fp)
{
fprintf(fp, msg.c_str());
fflush(fp);
}
fclose(fp);
}
void Util::FrameDefaultButton(DialogPtr dialog, short itemNo, bool active)
{
DialogItemType type;
ControlRef control;
Handle itemH;
Rect box;
GetDialogItem(dialog, itemNo, &type, &itemH, &box);
InsetRect(&box, -4, -4);
PenSize(3, 3);
if (!active)
{
RGBColor color;
PixPatHandle pp;
color.red = 0x8000;
color.green = 0x8000;
color.blue = 0x8000;
pp = NewPixPat();
MakeRGBPat(pp, &color);
PenPixPat(pp);
FrameRoundRect(&box, 16, 16);
DisposePixPat(pp);
PenNormal();
HiliteControl((ControlRef)itemH, 255);
}
else
{
FrameRoundRect(&box, 16, 16);
HiliteControl((ControlRef)itemH, 0);
}
}
bool Util::IsControlHilited(DialogPtr dialog, short itemNo)
{
DialogItemType type;
Handle itemH;
Rect box;
GetDialogItem(dialog, itemNo, &type, &itemH, &box);
ControlRef ctrlRef = (ControlRef)itemH;
ControlPtr ctrlPtr = *ctrlRef;
ControlRecord control = *ctrlPtr;
return control.contrlHilite == 0;
}
string Util::UrlEncode(string &value)
{
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char)c);
escaped << nouppercase;
}
return escaped.str();
}
// From: http://www.mactech.com/articles/develop/issue_26/minow.html
double Util::MicrosecondToDouble(register const UnsignedWide *epochPtr)
{
register double result;
result = (((double)epochPtr->hi) * kTwoPower32) + epochPtr->lo;
return (result);
}
// From: https://www.fenestrated.net/mirrors/Apple%20Technotes%20(As%20of%202002)/fl/fl_510.html
OSErr Util::GetDirectoryId(short parentFolderVRefNum, long parentFolderDirID, Str255 folderName, long* directoryId)
{
CInfoPBRec myCInfoPBRec;
OSErr retCode;
myCInfoPBRec.dirInfo.ioCompletion = nil;
myCInfoPBRec.dirInfo.ioNamePtr = folderName;
myCInfoPBRec.dirInfo.ioVRefNum = parentFolderVRefNum;
myCInfoPBRec.dirInfo.ioFDirIndex = 0; // use name, vRefNum, dirID
myCInfoPBRec.dirInfo.ioDrDirID = parentFolderDirID; // will be changed
retCode = PBGetCatInfoSync(&myCInfoPBRec); // IM IV - 155
if(retCode == noErr)
*directoryId = myCInfoPBRec.dirInfo.ioDrDirID;
return retCode;
}