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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
#include <sys/time.h> // for gettimeofday
#include <algorithm> // for std::transform
#include <ctype.h> // for tolower
#include "lifepoll.h" // for lifepoll
#include "util.h" // for linereader
#include "prefs.h" // for allowbeep, tempdir
#include "utils.h"
#ifdef ANDROID_GUI
#include "jnicalls.h" // for AndroidWarning, AndroidBeep, UpdateStatus, etc
#endif
#ifdef WEB_GUI
#include "webcalls.h" // for WebWarning, WebBeep, UpdateStatus, etc
#endif
#ifdef IOS_GUI
#import <AudioToolbox/AudioToolbox.h> // for AudioServicesPlaySystemSound, etc
#import "PatternViewController.h" // for UpdateStatus, etc
#endif
// -----------------------------------------------------------------------------
int event_checker = 0; // if > 0 then we're in gollypoller.checkevents()
// -----------------------------------------------------------------------------
void SetColor(gColor& color, unsigned char red, unsigned char green, unsigned char blue)
{
color.r = red;
color.g = green;
color.b = blue;
}
// -----------------------------------------------------------------------------
void SetRect(gRect& rect, int x, int y, int width, int height)
{
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = height;
}
// -----------------------------------------------------------------------------
#ifdef IOS_GUI
// need the following to make YesNo/Warning/Fatal dialogs modal:
@interface ModalAlertDelegate : NSObject <UIAlertViewDelegate>
{
NSInteger returnButt;
}
@property () NSInteger returnButt;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@implementation ModalAlertDelegate
@synthesize returnButt;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
returnButt = buttonIndex;
}
@end
#endif // IOS_GUI
// -----------------------------------------------------------------------------
bool YesNo(const char* msg)
{
Beep();
#ifdef ANDROID_GUI
return AndroidYesNo(msg);
#endif
#ifdef WEB_GUI
return WebYesNo(msg);
#endif
#ifdef IOS_GUI
ModalAlertDelegate *md = [[ModalAlertDelegate alloc] init];
md.returnButt = -1;
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Warning"
message:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]
delegate:md
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[a show];
// wait for user to hit button
while (md.returnButt == -1) {
event_checker++;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
event_checker--;
}
return md.returnButt != 0;
#endif // IOS_GUI
}
// -----------------------------------------------------------------------------
void Warning(const char* msg)
{
Beep();
#ifdef ANDROID_GUI
AndroidWarning(msg);
#endif
#ifdef WEB_GUI
WebWarning(msg);
#endif
#ifdef IOS_GUI
ModalAlertDelegate *md = [[ModalAlertDelegate alloc] init];
md.returnButt = -1;
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Warning"
message:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]
delegate:md
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[a show];
// wait for user to hit button
while (md.returnButt == -1) {
event_checker++;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
event_checker--;
}
#endif // IOS_GUI
}
// -----------------------------------------------------------------------------
void Fatal(const char* msg)
{
Beep();
#ifdef ANDROID_GUI
AndroidFatal(msg);
#endif
#ifdef WEB_GUI
WebFatal(msg);
#endif
#ifdef IOS_GUI
ModalAlertDelegate *md = [[ModalAlertDelegate alloc] init];
md.returnButt = -1;
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Fatal Error"
message:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]
delegate:md
cancelButtonTitle:@"Quit"
otherButtonTitles:nil];
[a show];
// wait for user to hit button
while (md.returnButt == -1) {
event_checker++;
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
event_checker--;
}
exit(1);
#endif // IOS_GUI
}
// -----------------------------------------------------------------------------
void Beep()
{
if (!allowbeep) return;
#ifdef ANDROID_GUI
AndroidBeep();
#endif
#ifdef WEB_GUI
WebBeep();
#endif
#ifdef IOS_GUI
static SystemSoundID beepID = 0;
if (beepID == 0) {
// get the path to the sound file
NSString* path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"aiff"];
if (path) {
NSURL* url = [NSURL fileURLWithPath:path];
OSStatus err = AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &beepID);
if (err == kAudioServicesNoError && beepID > 0) {
// play the sound
AudioServicesPlaySystemSound(beepID);
}
}
} else {
// assume we got the sound
AudioServicesPlaySystemSound(beepID);
}
#endif // IOS_GUI
}
// -----------------------------------------------------------------------------
double TimeInSeconds()
{
struct timeval trec;
gettimeofday(&trec, 0);
return double(trec.tv_sec) + double(trec.tv_usec) / 1.0e6;
}
// -----------------------------------------------------------------------------
std::string CreateTempFileName(const char* prefix)
{
/*
std::string tmplate = tempdir;
tmplate += prefix;
tmplate += ".XXXXXX";
std::string path = mktemp((char*)tmplate.c_str());
*/
// simpler to ignore prefix and create /tmp/0, /tmp/1, /tmp/2, etc
char n[32];
static int nextname = 0;
sprintf(n, "%d", nextname++);
std::string path = tempdir + n;
return path;
}
// -----------------------------------------------------------------------------
bool FileExists(const std::string& filepath)
{
FILE* f = fopen(filepath.c_str(), "r");
if (f) {
fclose(f);
return true;
} else {
return false;
}
}
// -----------------------------------------------------------------------------
void RemoveFile(const std::string& filepath)
{
#ifdef ANDROID_GUI
AndroidRemoveFile(filepath);
#endif
#ifdef WEB_GUI
WebRemoveFile(filepath);
#endif
#ifdef IOS_GUI
if ([[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithCString:filepath.c_str() encoding:NSUTF8StringEncoding]
error:NULL] == NO) {
// should never happen
Warning("RemoveFile failed!");
};
#endif
}
// -----------------------------------------------------------------------------
bool CopyFile(const std::string& inpath, const std::string& outpath)
{
#if defined(ANDROID_GUI) || defined(WEB_GUI)
FILE* infile = fopen(inpath.c_str(), "r");
if (infile) {
// read entire file into contents
std::string contents;
const int MAXLINELEN = 4095;
char linebuf[MAXLINELEN + 1];
linereader reader(infile);
while (true) {
if (reader.fgets(linebuf, MAXLINELEN) == 0) break;
contents += linebuf;
contents += "\n";
}
reader.close();
// fclose(infile) has been called
// write contents to outpath
FILE* outfile = fopen(outpath.c_str(), "w");
if (outfile) {
if (fputs(contents.c_str(), outfile) == EOF) {
fclose(outfile);
Warning("CopyFile failed to copy contents to output file!");
return false;
}
fclose(outfile);
} else {
Warning("CopyFile failed to open output file!");
return false;
}
return true;
} else {
Warning("CopyFile failed to open input file!");
return false;
}
#endif // ANDROID_GUI or WEB_GUI
#ifdef IOS_GUI
if (FileExists(outpath)) {
RemoveFile(outpath);
}
return [[NSFileManager defaultManager] copyItemAtPath:[NSString stringWithCString:inpath.c_str() encoding:NSUTF8StringEncoding]
toPath:[NSString stringWithCString:outpath.c_str() encoding:NSUTF8StringEncoding]
error:NULL];
#endif // IOS_GUI
}
// -----------------------------------------------------------------------------
bool MoveFile(const std::string& inpath, const std::string& outpath)
{
#ifdef ANDROID_GUI
return AndroidMoveFile(inpath, outpath);
#endif
#ifdef WEB_GUI
return WebMoveFile(inpath, outpath);
#endif
#ifdef IOS_GUI
if (FileExists(outpath)) {
RemoveFile(outpath);
}
return [[NSFileManager defaultManager] moveItemAtPath:[NSString stringWithCString:inpath.c_str() encoding:NSUTF8StringEncoding]
toPath:[NSString stringWithCString:outpath.c_str() encoding:NSUTF8StringEncoding]
error:NULL];
#endif
}
// -----------------------------------------------------------------------------
void FixURLPath(std::string& path)
{
// replace "%..." with suitable chars for a file path (eg. %20 is changed to space)
#ifdef ANDROID_GUI
AndroidFixURLPath(path);
#endif
#ifdef WEB_GUI
WebFixURLPath(path);
#endif
#ifdef IOS_GUI
NSString* newpath = [[NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (newpath) path = [newpath cStringUsingEncoding:NSUTF8StringEncoding];
#endif
}
// -----------------------------------------------------------------------------
bool IsHTMLFile(const std::string& filename)
{
size_t dotpos = filename.rfind('.');
if (dotpos == std::string::npos) return false;
std::string ext = filename.substr(dotpos+1);
return ( strcasecmp(ext.c_str(),"htm") == 0 ||
strcasecmp(ext.c_str(),"html") == 0 );
}
// -----------------------------------------------------------------------------
bool IsTextFile(const std::string& filename)
{
if (!IsHTMLFile(filename)) {
// if non-html file name contains "readme" then assume it's a text file
std::string basename = filename;
size_t lastsep = basename.rfind('/');
if (lastsep != std::string::npos) {
basename = basename.substr(lastsep+1);
}
std::transform(basename.begin(), basename.end(), basename.begin(), tolower);
if (basename.find("readme") != std::string::npos) return true;
}
size_t dotpos = filename.rfind('.');
if (dotpos == std::string::npos) return false;
std::string ext = filename.substr(dotpos+1);
return ( strcasecmp(ext.c_str(),"txt") == 0 ||
strcasecmp(ext.c_str(),"doc") == 0 );
}
// -----------------------------------------------------------------------------
bool IsZipFile(const std::string& filename)
{
size_t dotpos = filename.rfind('.');
if (dotpos == std::string::npos) return false;
std::string ext = filename.substr(dotpos+1);
return ( strcasecmp(ext.c_str(),"zip") == 0 ||
strcasecmp(ext.c_str(),"gar") == 0 );
}
// -----------------------------------------------------------------------------
bool IsRuleFile(const std::string& filename)
{
size_t dotpos = filename.rfind('.');
if (dotpos == std::string::npos) return false;
std::string ext = filename.substr(dotpos+1);
return ( strcasecmp(ext.c_str(),"rule") == 0 ||
strcasecmp(ext.c_str(),"table") == 0 ||
strcasecmp(ext.c_str(),"tree") == 0 ||
strcasecmp(ext.c_str(),"colors") == 0 ||
strcasecmp(ext.c_str(),"icons") == 0 );
}
// -----------------------------------------------------------------------------
bool IsScriptFile(const std::string& filename)
{
size_t dotpos = filename.rfind('.');
if (dotpos == std::string::npos) return false;
std::string ext = filename.substr(dotpos+1);
return ( strcasecmp(ext.c_str(),"lua") == 0 ||
strcasecmp(ext.c_str(),"pl") == 0 ||
strcasecmp(ext.c_str(),"py") == 0 );
}
// -----------------------------------------------------------------------------
bool EndsWith(const std::string& str, const std::string& suffix)
{
// return true if str ends with suffix
size_t strlen = str.length();
size_t sufflen = suffix.length();
return (strlen >= sufflen) && (str.rfind(suffix) == strlen - sufflen);
}
// -----------------------------------------------------------------------------
// let gollybase modules process events
class golly_poll : public lifepoll
{
public:
virtual int checkevents();
virtual void updatePop();
};
int golly_poll::checkevents()
{
if (event_checker > 0) return isInterrupted();
event_checker++;
#ifdef ANDROID_GUI
AndroidCheckEvents();
#endif
#ifdef WEB_GUI
WebCheckEvents();
#endif
#ifdef IOS_GUI
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
#endif
event_checker--;
return isInterrupted();
}
void golly_poll::updatePop()
{
UpdateStatus();
}
// -----------------------------------------------------------------------------
golly_poll gollypoller; // create instance
lifepoll* Poller()
{
return &gollypoller;
}
void PollerReset()
{
gollypoller.resetInterrupted();
}
void PollerInterrupt()
{
gollypoller.setInterrupted();
}
|