My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include "Core.h"

#ifdef PLATFORM_WIN32
#define Ptr Ptr_
#define byte byte_
#define CY win32_CY_

#include <shellapi.h>
#include <wincon.h>
#include <shlobj.h>

#undef Ptr
#undef byte
#undef CY
#endif

NAMESPACE_UPP

#ifdef PLATFORM_WIN32


String GetEnv(const char *id)
{
return WString(_wgetenv(WString(id))).ToString();
}

String GetExeFilePath()
{
return GetModuleFileName();
}

String GetHomeDirectory() {
return GetEnv("HOMEDRIVE") + GetEnv("HOMEPATH");
}

#endif

#ifdef PLATFORM_POSIX

String GetEnv(const char *id)
{
return FromSystemCharset(getenv(id));
}

char Argv0__[_MAX_PATH + 1];

static void sSetArgv0__(const char *title)
{
strcpy(Argv0__, title);
}

extern char Argv0__[];

const char *procexepath_() {
static char h[_MAX_PATH + 1];
ONCELOCK {
char link[100];
#ifdef PLATFORM_BSD
sprintf(link, "/proc/%d/file", getpid());
#else
sprintf(link, "/proc/%d/exe", getpid());
#endif
int ret = readlink(link, h, _MAX_PATH);
if(ret > 0 && ret < _MAX_PATH)
h[ret] = '\0';
else
*h = '\0';
}
return h;
}

String GetExeFilePath()
{
static String exepath;
ONCELOCK {
const char *exe = procexepath_();
if(*exe)
exepath = exe;
else {
String x = Argv0__;
if(IsFullPath(x) && FileExists(x))
exepath = x;
else {
exepath = GetHomeDirFile("upp");
Vector<String> p = Split(FromSystemCharset(Environment().Get("PATH")), ':');
if(x.Find('/') >= 0)
p.Add(GetCurrentDirectory());
for(int i = 0; i < p.GetCount(); i++) {
String ep = NormalizePath(AppendFileName(p[i], x));
if(FileExists(ep))
exepath = ep;
}
}
}
}
return exepath;
}

#endif

String GetExeDirFile(const char *filename)
{
return AppendFileName(GetFileFolder(GetExeFilePath()), filename);
}

String GetExeTitle()
{
return GetFileTitle(GetExeFilePath());
}

#ifdef PLATFORM_POSIX

static StaticCriticalSection sHlock;

String& sHomeDir() {
static String s;
return s;
}

String GetHomeDirectory() {
String r;
INTERLOCKED_(sHlock) {
String& s = sHomeDir();
if(s.IsEmpty()) {
s = Nvl(GetEnv("HOME"), "/root");
}
r = s;
}
return r;
}

void SetHomeDirectory(const char *dir)
{
INTERLOCKED_(sHlock) {
sHomeDir() = dir;
}
}

#endif//PLATFORM_POSIX

String GetHomeDirFile(const char *fp) {
return AppendFileName(GetHomeDirectory(), fp);
}

static bool sHomecfg;

void UseHomeDirectoryConfig(bool b)
{
sHomecfg = b;
}

String ConfigFile(const char *file) {
#if defined(PLATFORM_WIN32)
if(sHomecfg) {
String p = GetHomeDirFile(GetExeTitle());
ONCELOCK
RealizeDirectory(p);
return AppendFileName(p, file);
}
return GetExeDirFile(file);
#elif defined(PLATFORM_POSIX)
String p = GetHomeDirFile(".upp/" + GetExeTitle());
ONCELOCK
RealizeDirectory(p);
return AppendFileName(p, file);
#else
#error ConfigFile not implemented for this platform, comment this line to get input string back
return file;
#endif//PLATFORM
}

String ConfigFile() {
return ConfigFile(GetExeTitle() + ".cfg");
}

GLOBAL_VAR(Vector<WString>, coreCmdLine__)

const Vector<String>& CommandLine()
{
Vector<String> *ptr;
INTERLOCKED {
static ArrayMap< byte, Vector<String> > charset_cmd;
byte cs = GetDefaultCharset();
int f = charset_cmd.Find(cs);
if(f >= 0)
ptr = &charset_cmd[f];
else {
ptr = &charset_cmd.Add(cs);
const Vector<WString>& src = coreCmdLine__();
for(int i = 0; i < src.GetCount(); i++)
ptr->Add(src[i].ToString());
}
}
return *ptr;
}

VectorMap<WString, WString>& EnvMap()
{
static VectorMap<WString, WString> x;
return x;
}

const VectorMap<String, String>& Environment()
{
VectorMap<String, String> *ptr;
INTERLOCKED {
static ArrayMap< byte, VectorMap<String, String> > charset_env;
byte cs = GetDefaultCharset();
int f = charset_env.Find(cs);
if(f >= 0)
ptr = &charset_env[f];
else {
ptr = &charset_env.Add(cs);
const VectorMap<WString, WString>& env_map = EnvMap();
for(int i = 0; i < env_map.GetCount(); i++)
ptr->Add(env_map.GetKey(i).ToString(), env_map[i].ToString());
}
}
return *ptr;
}

static int exitcode;
static bool sMainRunning;

void SetExitCode(int code) { exitcode = code; }
int GetExitCode() { return exitcode; }

bool IsMainRunning()
{
return sMainRunning;
}

void LoadLangFiles(const char *dir)
{
FindFile ff(AppendFileName(dir, "*.tr"));
while(ff) {
LoadLngFile(AppendFileName(dir, ff.GetName()));
ff.Next();
}
}

void CommonInit()
{
#ifdef PLATFORM_WIN32
LoadLangFiles(GetFileFolder(GetExeFilePath()));
#else
LoadLangFiles(GetHomeDirectory());
#endif

Vector<WString>& cmd = coreCmdLine__();
static WString exp_cmd = "--export-tr";
static WString brk_cmd = "--memory-breakpoint__";

for(int i = 0; i < cmd.GetCount();) {
if(cmd[i] == exp_cmd) {
{
i++;
int lang = 0;
int lang2 = 0;
byte charset = CHARSET_UTF8;
String fn = "all";
if(i < cmd.GetCount())
if(cmd[i].GetLength() == 4 || cmd[i].GetLength() == 5) {
lang = LNGFromText(cmd[i].ToString());
fn = cmd[i].ToString();
int c = cmd[i][4];
if(c >= '0' && c <= '8')
charset = c - '0' + CHARSET_WIN1250;
if(c >= 'A' && c <= 'J')
charset = c - 'A' + CHARSET_ISO8859_1;
}
fn << ".tr";
if(++i < cmd.GetCount() && (cmd[i].GetLength() == 4 || cmd[i].GetLength() == 5))
lang2 = LNGFromText(cmd[i].ToString());
#ifdef PLATFORM_WIN32
FileOut out(GetExeDirFile(fn));
#else
FileOut out(GetHomeDirFile(fn));
#endif
if(lang) {
if(lang2)
SaveLngFile(out, SetLNGCharset(lang, charset), SetLNGCharset(lang2, charset));
else
SaveLngFile(out, SetLNGCharset(lang, charset));
}
else {
Index<int> l = GetLngSet();
for(int i = 0; i < l.GetCount(); i++)
SaveLngFile(out, SetLNGCharset(l[i], charset));
}
}
exit(0);
}
#if defined(_DEBUG) && defined(UPP_HEAP)
if(cmd[i] == brk_cmd && i + 1 < cmd.GetCount()) {
MemoryBreakpoint(atoi(cmd[i + 1].ToString()));
cmd.Remove(i, 2);
}
else
i++;
#else
i++;
#endif
}
sMainRunning = true;
}

void Exit(int code)
{
SetExitCode(code);
throw ExitExc();
}

void AppExecute__(void (*app)())
{
try {
(*app)();
}
catch(Exc e) {
Panic(e);
}
catch(ExitExc) {}
}

#ifdef PLATFORM_POSIX

void s_ill_handler(int)
{
Panic("Illegal instruction!");
}

void s_segv_handler(int)
{
Panic("Invalid memory access!");
}

void s_fpe_handler(int)
{
Panic("Invalid arithmetic operation!");
}

void AppInit__(int argc, const char **argv, const char **envptr)
{
SetLanguage(LNG_ENGLISH);
sSetArgv0__(argv[0]);
for(const char *var; (var = *envptr) != 0; envptr++)
{
const char *b = var;
while(*var && *var != '=')
var++;
String varname(b, var);
if(*var == '=')
var++;
EnvMap().Add(varname.ToWString(), String(var).ToWString());
}
Vector<WString>& cmd = coreCmdLine__();
for(int i = 1; i < argc; i++)
cmd.Add(FromSystemCharset(argv[i]).ToWString());
CommonInit();
signal(SIGILL, s_ill_handler);
signal(SIGSEGV, s_segv_handler);
signal(SIGBUS, s_segv_handler);
signal(SIGFPE, s_fpe_handler);
}
#endif

#if defined(PLATFORM_WIN32)

void AppInitEnvironment__()
{
#ifndef PLATFORM_WINCE
wchar *env = GetEnvironmentStringsW();
for(wchar *ptr = env; *ptr; ptr++)
{
const wchar *b = ptr;
if(*ptr)
ptr++;
while(*ptr && *ptr != '=')
ptr++;
WString varname(b, ptr);
if(*ptr)
ptr++;
b = ptr;
while(*ptr)
ptr++;
EnvMap().GetAdd(ToUpper(varname)) = WString(b, ptr);
}
FreeEnvironmentStringsW(env);
#endif
CommonInit();
}

void AppInit__(int argc, const char **argv)
{
SetLanguage(LNG_ENGLISH);
Vector<WString>& cmd = coreCmdLine__();
for(int i = 1; i < argc; i++)
cmd.Add(argv[i]);
AppInitEnvironment__();
}
#endif

void AppExit__()
{
sMainRunning = false;
#ifdef PLATFORM_POSIX
MemoryIgnoreLeaksBegin(); // Qt leaks on app exit...
#endif
}

#if defined(PLATFORM_WIN32) && !defined(PLATFORM_WINCE)
static auxthread_t auxthread__ sShellExecuteOpen(void *str)
{
ShellExecuteW(NULL, L"open", (wchar *)str, NULL, L".", SW_SHOWDEFAULT);
free(str);
return 0;
}

void LaunchWebBrowser(const String& url)
{
WString wurl = ToSystemCharsetW(url);
if (int(ShellExecuteW(NULL, L"open", wurl, NULL, L".", SW_SHOWDEFAULT)) <= 32) {
int l = sizeof(wchar) * wurl.GetLength() + 1;
char *curl = (char *)malloc(l);
memcpy(curl, wurl, l);
StartAuxThread(sShellExecuteOpen, curl);
}
}
#endif

#ifdef PLATFORM_POSIX
void LaunchWebBrowser(const String& url)
{
const char * browser[] = {
"htmlview", "xdg-open", "x-www-browser", "firefox", "konqueror", "opera", "epiphany", "galeon", "netscape"
};
for(int i = 0; i < __countof(browser); i++)
if(system("which " + String(browser[i])) == 0) {
IGNORE_RESULT(
system(String(browser[i]) + " " + url + " &")
);
break;
}
}
#endif

String GetDataFile(const char *filename)
{
String s = GetEnv("UPP_MAIN__");
return s.GetCount() ? AppendFileName(s, filename) : GetExeDirFile(filename);
}

String GetComputerName()
{
char temp[256];
*temp = 0;
#if defined(PLATFORM_WIN32)
dword w = 255;
::GetComputerNameA(temp, &w);
#else
gethostname(temp, sizeof(temp));
#endif
return FromSystemCharset(temp);
}

String GetUserName()
{
char temp[256];
*temp = 0;
#if defined(PLATFORM_WIN32)
dword w = 255;
::GetUserNameA(temp, &w);
return FromSystemCharset(temp);
#else
return Nvl(GetEnv("USER"), "boot");
#endif
}

String GetDesktopManager()
{
#if defined(PLATFORM_WIN32) && !defined(PLATFORM_WINCE)
return "windows";
#endif
#ifdef PLATFORM_POSIX
if(GetEnv("GNOME_DESKTOP_SESSION_ID").GetCount())
return "gnome";
if(GetEnv("KDE_FULL_SESSION").GetCount() || GetEnv("KDEDIR").GetCount())
return "kde";
return GetEnv("DESKTOP_SESSION");
#endif
}

#if defined(PLATFORM_WIN32)

String GetShellFolder(int clsid)
{
wchar path[MAX_PATH];
if(SHGetFolderPathW(NULL, clsid, NULL, /*SHGFP_TYPE_CURRENT*/0, path) == S_OK)
return FromUnicodeBuffer(path);
return Null;
}

String GetDesktopFolder() { return GetShellFolder(CSIDL_DESKTOP); }
String GetProgramsFolder() { return GetShellFolder(CSIDL_PROGRAM_FILES); }
String GetAppDataFolder() { return GetShellFolder(CSIDL_APPDATA);}
String GetMusicFolder() { return GetShellFolder(CSIDL_MYMUSIC);}
String GetPicturesFolder() { return GetShellFolder(CSIDL_MYPICTURES);}
String GetVideoFolder() { return GetShellFolder(CSIDL_MYVIDEO);}
String GetDocumentsFolder() { return GetShellFolder(/*CSIDL_MYDOCUMENTS*/0x0005);}
String GetTemplatesFolder() { return GetShellFolder(CSIDL_TEMPLATES);}

#define MY_DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
static const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }

MY_DEFINE_KNOWN_FOLDER(MY_FOLDERID_Downloads, 0x374de290, 0x123f, 0x4565, 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b);

String GetDownloadFolder()
{
static HRESULT (STDAPICALLTYPE * SHGetKnownFolderPath)(const void *rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
ONCELOCK {
DllFn(SHGetKnownFolderPath, "shell32.dll", "SHGetKnownFolderPath");
}
if(SHGetKnownFolderPath) {
PWSTR path = NULL;
if(SHGetKnownFolderPath(&MY_FOLDERID_Downloads, 0, NULL, &path) == S_OK && path) {
String s = FromUnicodeBuffer(path, wstrlen(path));
CoTaskMemFree(path);
return s;
}
}
return Null;
};
#endif

#ifdef PLATFORM_POSIX

String GetPathXdg(String xdgConfigHome, String xdgConfigDirs)
{
String ret = "";
if (FileExists(ret = AppendFileName(xdgConfigHome, "user-dirs.dirs"))) ;
else if (FileExists(ret = AppendFileName(xdgConfigDirs, "user-dirs.defaults"))) ;
else if (FileExists(ret = AppendFileName(xdgConfigDirs, "user-dirs.dirs"))) ;
return ret;
}

String GetPathDataXdg(String fileConfig, const char *folder)
{
TextSettings settings;
settings.Load(fileConfig);
String v = settings.Get(folder);
if(*v == '\"')
v = v.Mid(1);
if(*v.Last() == '\"')
v.Trim(v.GetCount() - 1);
String r;
const char *s = v;
while(*s) {
if(*s == '$') {
CParser p(s + 1);
p.NoSkipSpaces();
p.Spaces();
if(p.IsId()) {
String id = p.ReadId();
r.Cat(GetEnv(id));
s = p.GetPtr();
}
else {
r.Cat('$');
s++;
}
}
else
r.Cat(*s++);
}
return r;
}

String GetShellFolder(const char *local, const char *users)
{
String xdgConfigHome = GetEnv("XDG_CONFIG_HOME");
if (xdgConfigHome == "") // By default
xdgConfigHome = AppendFileName(GetHomeDirectory(), ".config");
String xdgConfigDirs = GetEnv("XDG_CONFIG_DIRS");
if (xdgConfigDirs == "") // By default
xdgConfigDirs = "/etc/xdg";
String xdgFileConfigData = GetPathXdg(xdgConfigHome, xdgConfigDirs);
String ret = GetPathDataXdg(xdgFileConfigData, local);
if (ret == "" && *users != '\0')
return GetPathDataXdg(xdgFileConfigData, users);
else
return ret;
}

String GetDesktopFolder()
{
String ret = GetShellFolder("XDG_DESKTOP_DIR", "DESKTOP");
if (ret.IsEmpty())
return AppendFileName(GetHomeDirectory(), "Desktop");
else
return ret;
}

String GetProgramsFolder() { return String("/usr/bin"); }
String GetAppDataFolder() { return GetHomeDirectory(); }
String GetMusicFolder() { return GetShellFolder("XDG_MUSIC_DIR", "MUSIC"); }
String GetPicturesFolder() { return GetShellFolder("XDG_PICTURES_DIR", "PICTURES"); }
String GetVideoFolder() { return GetShellFolder("XDG_VIDEOS_DIR", "VIDEOS"); }
String GetDocumentsFolder() { return GetShellFolder("XDG_DOCUMENTS_DIR", "DOCUMENTS"); }
String GetTemplatesFolder() { return GetShellFolder("XDG_TEMPLATES_DIR", "XDG_TEMPLATES_DIR"); }
String GetDownloadFolder() { return GetShellFolder("XDG_DOWNLOAD_DIR", "XDG_DOWNLOAD_DIR"); }

#endif

END_UPP_NAMESPACE

Change log

r4982 by cxl on May 21, 2012   Diff
Core: Exit
Go to: 
Project members, sign in to write a code review

Older revisions

r4791 by cxl on Apr 16, 2012   Diff
Core: inet docs
r4761 by cxl on Apr 9, 2012   Diff
Core: TcpSocket, Thread::AtExit
r4759 by cxl on Apr 8, 2012   Diff
Core: Refactored Stream interface
(dword->int), fixed LauchWebBrowser
for certaion situations (thanks Koldo)
All revisions of this file

File info

Size: 13649 bytes, 614 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting