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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
/********************************************************************************
* *
* U t i l i t y F u n c t i o n s *
* *
*********************************************************************************
* Copyright (C) 1998,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxchar.h"
#include "fxmath.h"
#include "fxendian.h"
#include "fxascii.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXElement.h"
/*
Notes:
- Handy global utility functions.
*/
// Initial space allocated for message; it will grow if needed
#ifndef INITIALMESSAGESIZE
#define INITIALMESSAGESIZE 512
#endif
using namespace FX;
/*******************************************************************************/
namespace FX {
// Furnish our own versions
extern FXAPI FXuint __strtoul(const FXchar *beg,const FXchar** end=nullptr,FXint base=0,FXbool* ok=nullptr);
// Allows GNU autoconfigure to find FOX
extern "C" FXAPI void fxfindfox(void){ }
// Version number that the library has been compiled with
const FXuchar fxversion[3]={FOX_MAJOR,FOX_MINOR,FOX_LEVEL};
/*******************************************************************************/
#if defined(WIN32)
// Return true if console application
FXbool fxisconsole(const FXchar *path){
IMAGE_OPTIONAL_HEADER optional_header;
IMAGE_FILE_HEADER file_header;
IMAGE_DOS_HEADER dos_header;
DWORD dwCoffHeaderOffset;
DWORD dwNewOffset;
DWORD dwMoreDosHeader[16];
ULONG ulNTSignature;
HANDLE hImage;
DWORD dwBytes;
FXbool flag=false; // Assume false on Windows is safest!
// Open the application file.
hImage=CreateFileA(path,GENERIC_READ,FILE_SHARE_READ,nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nullptr);
if(hImage!=INVALID_HANDLE_VALUE){
// Read MS-Dos image header.
if(ReadFile(hImage,&dos_header,sizeof(IMAGE_DOS_HEADER),&dwBytes,nullptr)==0) goto x;
// Test bytes read
if(dwBytes!=sizeof(IMAGE_DOS_HEADER)) goto x;
// Test signature
if(dos_header.e_magic!=IMAGE_DOS_SIGNATURE) goto x;
// Read more MS-Dos header.
if(ReadFile(hImage,dwMoreDosHeader,sizeof(dwMoreDosHeader),&dwBytes,nullptr)==0) goto x;
// Test bytes read
if(dwBytes!=sizeof(dwMoreDosHeader)) goto x;
// Move the file pointer to get the actual COFF header.
dwNewOffset=SetFilePointer(hImage,dos_header.e_lfanew,nullptr,FILE_BEGIN);
dwCoffHeaderOffset=dwNewOffset+sizeof(ULONG);
if(dwCoffHeaderOffset==0xFFFFFFFF) goto x;
// Read NT signature of the file.
if(ReadFile(hImage,&ulNTSignature,sizeof(ULONG),&dwBytes,nullptr)==0) goto x;
// Test bytes read
if(dwBytes!=sizeof(ULONG)) goto x;
// Test NT signature
if(ulNTSignature!=IMAGE_NT_SIGNATURE) goto x;
if(ReadFile(hImage,&file_header,IMAGE_SIZEOF_FILE_HEADER,&dwBytes,nullptr)==0) goto x;
// Test bytes read
if(dwBytes!=IMAGE_SIZEOF_FILE_HEADER) goto x;
// Read the optional header of file.
if(ReadFile(hImage,&optional_header,sizeof(IMAGE_OPTIONAL_HEADER),&dwBytes,nullptr)==0) goto x;
// Test bytes read
if(dwBytes!=sizeof(IMAGE_OPTIONAL_HEADER)) goto x;
// Switch on systems
switch(optional_header.Subsystem){
case IMAGE_SUBSYSTEM_WINDOWS_GUI: // Windows GUI (2)
case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: // Windows CE GUI (9)
flag=false;
break;
case IMAGE_SUBSYSTEM_WINDOWS_CUI: // Windows Console (3)
case IMAGE_SUBSYSTEM_OS2_CUI: // OS/2 Console (5)
case IMAGE_SUBSYSTEM_POSIX_CUI: // Posix Console (7)
flag=true;
break;
case IMAGE_SUBSYSTEM_NATIVE: // Native (1)
case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: // Native Win9x (8)
case IMAGE_SUBSYSTEM_UNKNOWN: // Unknown (0)
default:
flag=false;
break;
}
x: CloseHandle(hImage);
}
return flag;
}
#else
// Return true if console application
FXbool fxisconsole(const FXchar*){
return true;
}
#endif
/*******************************************************************************/
// Log message to [typically] stderr
void fxmessage(const FXchar* format,...){
FXString message('\0',INITIALMESSAGESIZE);
va_list arguments;
va_start(arguments,format);
message.vformat(format,arguments);
va_end(arguments);
#if defined(WIN32)
#if defined(_WINDOWS)
OutputDebugStringA(message.text());
fputs(message.text(),stderr); // if a console is available
fflush(stderr);
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
}
// Error routine
void fxerror(const FXchar* format,...){
FXString message('\0',INITIALMESSAGESIZE);
va_list arguments;
va_start(arguments,format);
message.vformat(format,arguments);
va_end(arguments);
#if defined(WIN32)
#if defined(_WINDOWS)
OutputDebugStringA(message.text());
fputs(message.text(),stderr); // if a console is available
fflush(stderr);
MessageBoxA(nullptr,message.text(),nullptr,MB_OK|MB_ICONEXCLAMATION|MB_APPLMODAL);
DebugBreak();
#else
fputs(message.text(),stderr);
fflush(stderr);
abort();
#endif
#else
fputs(message.text(),stderr);
fflush(stderr);
abort();
#endif
}
// Warning routine
void fxwarning(const FXchar* format,...){
FXString message('\0',INITIALMESSAGESIZE);
va_list arguments;
va_start(arguments,format);
message.vformat(format,arguments);
va_end(arguments);
#if defined(WIN32)
#if defined(_WINDOWS)
OutputDebugStringA(message.text());
fputs(message.text(),stderr); // if a console is available
fflush(stderr);
MessageBoxA(nullptr,message.text(),nullptr,MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
}
/*******************************************************************************/
// Assert failed routine
void fxassert(const FXchar* expression,const FXchar* filename,unsigned int lineno){
#if defined(WIN32)
fxmessage("%s(%d): FXASSERT(%s) failed.\n",filename,lineno,expression);
#else
if(isatty(fileno(stderr))){
fxmessage("%s:%d: \033[1;33mFXASSERT(%s)\033[0m failed.\n",filename,lineno,expression);
}
else{
fxmessage("%s:%d: FXASSERT(%s) failed.\n",filename,lineno,expression);
}
#endif
}
// Verify failed routine
void fxverify(const FXchar* expression,const FXchar* filename,unsigned int lineno){
#if defined(WIN32)
fxmessage("%s(%d): FXVERIFY(%s) failed.\n",filename,lineno,expression);
#else
if(isatty(fileno(stderr))){
fxmessage("%s:%d: \033[1;33mFXVERIFY(%s)\033[0m failed.\n",filename,lineno,expression);
}
else{
fxmessage("%s:%d: FXVERIFY(%s) failed.\n",filename,lineno,expression);
}
#endif
}
/*******************************************************************************/
// Trace level environment variable
static const FXchar* fxTraceVariable=nullptr;
// Room for lots of topics
static FXuchar fxTopicArray[1024];
// Set trace topic
void setTraceTopic(FXuint topic,FXbool flag){
fxTopicArray[topic&(ARRAYNUMBER(fxTopicArray)-1)]=flag;
}
// Get trace topic setting
FXbool getTraceTopic(FXuint topic){
return fxTopicArray[topic&(ARRAYNUMBER(fxTopicArray)-1)];
}
// Set trace level
void setTraceLevel(FXuint level,FXbool flag){
level=FXMIN(level,ARRAYNUMBER(fxTopicArray)-1);
fillElms(fxTopicArray,0,ARRAYNUMBER(fxTopicArray));
fillElms(fxTopicArray,flag,level+1);
}
// Parse trace topics from string of the form:
//
// <topic-list> : <topic-range> [ ',' <topic-range> ]*
//
// <topic-range> : <topic> [':' [ <topic> ]? ]?
//
// : ':' [<topic> ]?
//
// <topic> : <digit> [ <digits> ]*
//
FXbool setTraceTopics(const FXchar* topics,FXbool flag){
if(__likely(topics)){
FXuint f,t;
while((*topics==':') || ('0'<=*topics && *topics<='9')){
f=0;
while('0'<=*topics && *topics<='9'){
f=f*10+*topics++-'0';
}
f=FXMIN(f,ARRAYNUMBER(fxTopicArray)-1);
t=f;
if(*topics==':'){
topics++;
t=ARRAYNUMBER(fxTopicArray)-1;
if('0'<=*topics && *topics<='9'){
t=0;
while('0'<=*topics && *topics<='9'){
t=t*10+*topics++-'0';
}
t=FXMIN(t,ARRAYNUMBER(fxTopicArray)-1);
if(f>t) swap(f,t);
}
fillElms(&fxTopicArray[f],flag,t-f);
}
fxTopicArray[t]=flag;
if(*topics!=',') break;
topics++;
}
return true;
}
return false;
}
// Trace printout routine
void fxtrace(FXuint level,const FXchar* format,...){
if(__unlikely(fxTraceVariable==nullptr)){
const FXchar* str;
fxTraceVariable="";
if((str=getenv("FOX_TRACE_TOPICS"))!=nullptr){
fxTraceVariable=str;
setTraceTopics(fxTraceVariable,true);
}
else if((str=getenv("FOX_TRACE_LEVEL"))!=nullptr){
fxTraceVariable=str;
setTraceLevel(__strtoul(fxTraceVariable,nullptr,10),true);
}
}
if(__likely(fxTopicArray[level&(ARRAYNUMBER(fxTopicArray)-1)])){
FXString message('\0',INITIALMESSAGESIZE);
va_list arguments;
va_start(arguments,format);
message.vformat(format,arguments);
va_end(arguments);
#if defined(WIN32)
#if defined(_WINDOWS)
OutputDebugStringA(message.text());
fputs(message.text(),stderr); // if a console is available
fflush(stderr);
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
#else
fputs(message.text(),stderr);
fflush(stderr);
#endif
}
}
/*******************************************************************************/
// Safe string copy
FXival fxstrlcpy(FXchar* dst,const FXchar* src,FXival len){
const FXchar* s=src;
FXchar* d=dst;
FXival n=len;
FXchar c;
while((c=*s)!='\0'){
if(1<n){ *d++=c; n--; }
s++;
}
if(0<n){
*d='\0';
}
return s-src;
}
// Safe string concat
FXival fxstrlcat(FXchar* dst,const FXchar* src,FXival len){
const FXchar* s=src;
FXchar* d=dst;
FXival n=len;
FXival m;
FXchar c;
while(0<n && *d!='\0'){
d++;
n--;
}
m=d-dst;
while((c=*s)!='\0'){
if(1<n){ *d++=c; n--; }
s++;
}
if(0<n){
*d='\0';
}
return (s-src)+m;
}
/*******************************************************************************/
// Convert string of length len to MSDOS; return new string and new length
FXbool fxtoDOS(FXchar*& string,FXint& len){
FXint f=0,t=0;
while(f<len){
if(string[f++]=='\n') t++;
t++;
}
if(resizeElms(string,t+1)){
len=t;
while(0<t){
if((string[--t]=string[--f])=='\n') string[--t]='\r';
}
string[len]='\0';
return true;
}
return false;
}
// Convert string of length len from MSDOS; return new string and new length
FXbool fxfromDOS(FXchar*& string,FXint& len){
FXint f=0,t=0,c;
while(f<len){
if((c=string[f++])!='\r') string[t++]=c;
}
if(resizeElms(string,t+1)){
len=t;
string[len]='\0';
return true;
}
return false;
}
extern FXAPI FILE *fxopen(const char *filename,const char *mode);
FILE *fxopen(const char *filename,const char *mode){
#if defined(WIN32) && defined(UNICODE)
FXnchar unifile[MAXPATHLEN],unimode[8];
utf2ncs(unifile,filename,ARRAYNUMBER(unifile));
utf2ncs(unimode,mode,ARRAYNUMBER(unimode));
return _wfopen(unifile,unimode);
#else
return fopen(filename,mode);
#endif
}
extern FXAPI FILE *fxreopen(const char *filename,const char *mode,FILE * stream);
FILE *fxreopen(const char *filename,const char *mode,FILE * stream){
#if defined(WIN32) && defined(UNICODE)
FXnchar unifile[MAXPATHLEN],unimode[8];
utf2ncs(unifile,filename,ARRAYNUMBER(unifile));
utf2ncs(unimode,mode,ARRAYNUMBER(unimode));
return _wfreopen(unifile,unimode,stream);
#else
return freopen(filename,mode,stream);
#endif
}
/*******************************************************************************/
// Convert RGB to HSV
void fxrgb_to_hsv(FXfloat& h,FXfloat& s,FXfloat& v,FXfloat r,FXfloat g,FXfloat b){
FXfloat mx=FXMAX3(r,g,b);
FXfloat mn=FXMIN3(r,g,b);
FXfloat d=mx-mn;
h=0.0f;
s=0.0f;
v=mx;
if(__likely(mx>0.0f)){
s=d/mx;
if(__likely(s>0.0f)){
if(r==mx) h=(g-b)/d;
else if(g==mx) h=2.0f+(b-r)/d;
else if(b==mx) h=4.0f+(r-g)/d;
h*=60.0f;
if(h<0.0f) h+=360.0f;
}
}
}
// Convert HSV to RGB
void fxhsv_to_rgb(FXfloat& r,FXfloat& g,FXfloat& b,FXfloat h,FXfloat s,FXfloat v){
FXfloat f,w,q,t;
FXint i;
r=g=b=v;
if(__likely(s>0.0f)){
if(h==360.0f) h=0.0f;
else h=h/60.0f;
i=(FXint)h;
f=h-i;
w=v*(1.0f-s);
q=v*(1.0f-(s*f));
t=v*(1.0f-(s*(1.0f-f)));
switch(i){
case 0: r=v; g=t; b=w; break;
case 1: r=q; g=v; b=w; break;
case 2: r=w; g=v; b=t; break;
case 3: r=w; g=q; b=v; break;
case 4: r=t; g=w; b=v; break;
case 5: r=v; g=w; b=q; break;
}
}
}
// Convert RGB to HSL
void fxrgb_to_hsl(FXfloat& h,FXfloat& s,FXfloat& l,FXfloat r,FXfloat g,FXfloat b){
FXfloat mx=FXMAX3(r,g,b);
FXfloat mn=FXMIN3(r,g,b);
FXfloat d=mx-mn;
h=0.0f;
s=0.0f;
l=(mx+mn)*0.5f;
if(__likely(d>0.0f)){
if(l<=0.5f)
s=d/(mx+mn);
else
s=d/(2.0f-mx-mn);
if(r==mx) h=(g-b)/d;
else if(g==mx) h=2.0f+(b-r)/d;
else if(b==mx) h=4.0f+(r-g)/d;
h*=60.0f;
if(h<0.0f) h+=360.0f;
}
}
// Convert HSL to RGB
void fxhsl_to_rgb(FXfloat& r,FXfloat& g,FXfloat& b,FXfloat h,FXfloat s,FXfloat l){
FXfloat f,v,min,mid1,mid2,sv,vsf;
FXint i;
r=g=b=l;
if(l<0.5f)
v=l+l*s;
else
v=l+s-l*s;
if(v<=0.0f){
r=g=b=0.0f;
}
else{
if(h==360.0f) h=0.0f;
else h=h/60.0f;
min=2.0f*l-v;
sv=(v-min)/v;
i=(FXint)h;
f=h-i;
vsf=v*sv*f;
mid1=min+vsf;
mid2=v-vsf;
switch(i){
case 0: r=v; g=mid1; b=min; break;
case 1: r=mid2; g=v; b=min; break;
case 2: r=min; g=v; b=mid1; break;
case 3: r=min; g=mid2; b=v; break;
case 4: r=mid1; g=min; b=v; break;
case 5: r=v; g=min; b=mid2; break;
}
}
}
}
|