-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_os.cpp
More file actions
174 lines (151 loc) · 2.94 KB
/
wrap_os.cpp
File metadata and controls
174 lines (151 loc) · 2.94 KB
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
//OS specific stuff
#ifdef __linux__
#include <time.h>
#include <sys/termios.h>
#include <pthread.h>
#include <stdio.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
/*
#ifdef _WIN32
#endif
#ifdef __linux__
#endif
*/
#import "doot.hpp"
#import "list.hpp"
#import "string.hpp"
using doot::list;
using doot::str;
using doot::i64;
#ifndef DOOT_NO_MAIN
extern int dootmain(list<str>& args);
#endif
// DOOT_NO_MAIN for main't-ed linked libs
#ifndef DOOT_NO_MAIN
#import "thread.hpp"
#ifdef _WIN32
int main(int argc, char* argv[], char* envp[]){
#endif
#ifdef __linux__
int main(int argc, char** argv){
#endif
doot::warp::init();
#ifdef DEBUG
#ifdef _WIN32
//this is for a VS setup
//assume windows users wont launch from term even when debug build
doot::create_console();
freopen("stdout.txt", "w", stdout);
freopen("stderr.txt", "w", stderr);
#endif
#ifdef __linux__
//gdb doesnt like stdio being rerouted, this is easier
freopen("stdout", "w", stdout);
freopen("stderr", "w", stderr);
#endif
::doot::run_tests();
#endif
list<str> args;
ra(i,argc)
args+= str(argv[i]);
re dootmain(args);
}
#endif
#import "time.hpp"
namespace doot{
void create_console(){
#ifdef _WIN32
//FreeConsole();
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
#ifdef __linux__
//todo what even
#endif
}
nsec current_time(){
#ifdef _WIN32
LARGE_INTEGER f;
QueryPerformanceFrequency(&f);
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return t.QuadPart*1'000'000'000ll/f.QuadPart;
#endif
#ifdef __linux__
timespec t;
clock_gettime(CLOCK_MONOTONIC,&t);
return t.tv_sec*1'000'000'000ll + t.tv_nsec;
#endif
}
void namethread(cstr s){
#ifdef _WIN32
//fixme link Kernel32.lib
#error
SetThreadDescription(GetCurrentThread(), s);
#endif
#ifdef __linux__
pthread_setname_np(pthread_self(), s);
#endif
}
}
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "file.hpp"
#include "hmap.hpp"
namespace doot{
bool file_lock(str& name){
FILE* file= fopen(name, "rw");
#ifdef _WIN32
#error
#endif
#ifdef __linux__
bool r= !!ftrylockfile(file);
#endif
fclose(file);
retr;
}
void file_unlock(str& name){
FILE* file= fopen(name, "rw");
#ifdef _WIN32
#error
#endif
#ifdef __linux__
funlockfile(file);
#endif
fclose(file);
}
struct fchgcall{
#ifdef _WIN32
#endif
#ifdef __linux__
#endif
void (*call)(void*);
void* obj;
void invoke(){
call(obj);
}
};
hmap<str, fchgcall> fchgmap;
void fchg_(str fnam){
may_if(ato call, fchgmap[fnam])
call.invoke();
else
err("fook fnam findn't");//future self will HATE this //future self: lmoa
}
bool file_change_listen(str fname, void (*callback)(void*), void* callbackarg){
#ifdef _WIN32
#endif
#ifdef __linux__
#endif
fchgmap.add<fchgcall>(fname, {callback,callbackarg});
return false;
}
}