File: std_c.def

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (348 lines) | stat: -rw-r--r-- 7,859 bytes parent folder | download | duplicates (2)
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
## frame
subcode:@ basic_frame
    $include stdio
    $include stdlib
    $call @global
    $function main
        $call main

###############################################################
## Utility
subcode:@ assert(check)
    $if !($(check))
        $print "Assert Error: ($(check)) at line %d \n", __LINE__
        exit(-1)

subcode:@ die(@msg)
    $call warn, $(msg)
    exit(-1)

subcode:@ warn(@msg)
    $(set:print_to=stderr)
    $print $(msg)

subcode: atexit
    $function atexit_handler
        BLOCK
    atexit($(lambda))

subcode: on_signal(name)
    $function signal_$(name)
        BLOCK
    signal($(name), $(lambda))

#----
subcode: try
    $include setjmp
    $global jmp_buf tryenv
    $local n_exception_code
    n_exception_code=setjmp(tryenv)
    $if n_exception_code==0
        BLOCK

subcode: catch(exception)
    $elif n_exception_code==$(exception)
        BLOCK

subcode: throw(exception)
    longjmp(tryenv, $(exception))

#----
subcode: seq_block
    $dowhile 0
        BLOCK

#----
subcode: on_tn_err(fn)
    $if tn_err
        $print "Error: $(fn): tn_err=$tn_err"
        exit(1)

#---- useful ------------
subcode: swap(a, b)
    $get_type(type) $(a)
    $temp $(type) temp

    $(temp)=$(a)
    $(a)=$(b)
    $(b)=$(temp)

subcode: update_max(max, a)
    $if $(max)<$(a)
        $(max)=$(a)

subcode: update_min(min, a)
    $if $(min)>$(a)
        $(min)=$(a)

subcode: clamp(v, min, max)
    $if $(v)<$(min)
        $(v)=$(min)
    $elif $(v)>$(max)
        $(v)=$(max)

subcode: _autoload
    $plugin swap, min_set(update_min), max_set(update_max)
    # how about missing_set, over_set?
#----
subcode: get_2d_l(i, j, ldi)
    $(export:l=$(i)*$(ldi)+$(j))

subcode: free(v)
    free($(v))

subcode: autoflush
    setvbuf(stdout, NULL, _IONBF, 0)

## FILE IO
subcode: get_file_in_s(file)
    s = get_file_s($(file), NULL)

fncode: get_file_s(s_file, pn_size)
    $autolist skip
    &call2 try_open_r, s_file
        $my n, s
        $call get_size, n
        $allocate(n+1) s
        n=fread(s, 1, n, file_in)
        s[n]='\0'
        $if pn_size
            *pn_size=n
        return s
    fail:
        return NULL

# --------------------------------
subcode: open_a(file)
    $(set:mode=a)
    &call open_w, $(file)
        BLOCK

subcode: open_W(file)
    &call open_w, $(file)
        $(set:print_to=file_out)
        BLOCK

subcode: open_w(file)
    $include stdio
    $(if:mode!=a)
        $(set:mode=w)
    file_out=fopen($(file), "$(mode)b")
    $if file_out==NULL
        $call die, "Can't write %s", $(file)
    $else
        BLOCK
        fclose(file_out)
    # --
    subcode: write_var_n(name, n)
        t_n=fwrite(&$(name), $(n), 1, file_out)
    # --
    subcode: write_array(a, n, size)
        t_n = fwrite($(a), $(size), $(n), file_out)
    # --
    subcode: write_sig(name)
        t_n=fwrite("$(name)", 1, $(name:strlen), file_out)
    subcode: write_sig_n(quoted_sig, n)
        t_n=fwrite($(quoted_sig), 1, $(n), file_out)

subcode: open_r(file)
    $include stdio
    file_in=fopen($(file), "rb")
    $if file_in==NULL
        $call die, "Can't open %s", $(file)
    $else
        &call fread_frame
            BLOCK
        fclose(file_in)

subcode: try_open_r(file)
    # multi-callback: &call2 try_open_r, file
    $include stdio
    file_in=fopen($(file), "rb")
    $if file_in==NULL
        BLOCK2
    $else
        &call fread_frame
            BLOCK1
        fclose(file_in)

subcode: fread_frame
    BLOCK
    # --
    subcode: read_int(n)
        t_n = fread(&$(n), 4, 1, file_in)
    subcode: read_array(a, n, size)
        t_n = fread($(a), $(size), $(n), file_in)
    # --
    subcode: get_size(n)
        fseek(file_in, 0L, SEEK_END)
        $(n) = ftell(file_in)
        fseek(file_in, 0L, SEEK_SET)
    subcode: skip_r(n)
        t_n=fseek(file_in, $(n), SEEK_CUR)
    # --
    subcode: read_sig(name, len)
        t_n=fread($(name), 1, $(len), file_in)
        $(name)[$(len)]=0
    subcode: read_var(name)
        # 4-byte variable
        t_n=fread(&$(name), 4, 1, file_in)
    subcode: read_data(name, size)
        t_n=fread($(name), 1, $(size), file_in)
    # --
    subcode: gets
        $local char _gets_line[1024]
        $my s=_gets_line
        $while fgets(_gets_line, 1024, file_in)
            BLOCK

subcode: open_rs(file)
    &call open_r, $(file)
        &call gets
            BLOCK

subcode: fread_s_frame
    &call fread_frame
        &call gets
            BLOCK

# ---- sscanf ------------------------------------- 
subcode: sscanf(s, @plist)
    $(unset:fmt, vars)
    $(for:p in $(plist))
        # init var
        $(if:p~tf)
            $local $(p)=0.0
        $(elif:p~tn)
            $local $(p)=0
        $(elif:p~tb)
            $local $(p)=0
        $(elif:p~ts)
            $local $(p)[100]
        # $(vars)
        $(if:p~t?s)
            $(set-1:vars.=,$(p))
        $(else)
            $(set-1:vars.=,&$(p))
        # $(fmt)
        $(if:p~[tp]?f)
            $(if:use_double)
                $(set-1:fmt.= %lf)
            $(else)
                $(set-1:fmt.= %f)
        $(elif:p~[tp]?[inb])
            $(set-1:fmt.= %i)
        $(elif:p~t?s)
            $(set-1:fmt.= %99s)

    $(assert:vars and fmt)
    sscanf($(s), "$(fmt)" $(vars));

# ---- time -------------------------------------------
subcode: tick_loop
    $include time
    $call tick_reset
    $while 1
        BLOCK

    subcode: on_tick(sec)
        $if time(NULL)-time_tick_set>=$(sec)
            BLOCK

    subcode: every_tick(sec)
        $if time(NULL)-time_tick_set>=$(sec)
            $call tick_reset
            BLOCK

    subcode: tick_reset
        time_tick_set=time(NULL)

subcode: print_time(type)
    $include time
    time_now=time(NULL)
    $my struct tm * p_now
    p_now=localtime(&time_now)
    $(if:type=default)
        printf("%d/%d %02d:%02d ", p_now->tm_mon+1, p_now->tm_mday, p_now->tm_hour, p_now->tm_min)
    $(else)
        printf("%d/%d %02d:%02d ", p_now->tm_mon+1, p_now->tm_mday, p_now->tm_hour, p_now->tm_min)
# --
subcode: start_clock
    $include time
    $global clock_t clock_start
    clock_start = clock()

subcode: report_clock(msg)
    tf_clock = (double)clock()/CLOCKS_PER_SEC
    $print "$(msg): %f seconds", (double)(clock()-clock_start)/CLOCKS_PER_SEC
# --
subcode: add_clock(name)
    $global clock_t clock_$(name)=0.0
    $my clock_t clock_temp
    clock_temp = clock()
    BLOCK
    clock_$(name) += clock() - clock_temp
    # $dump clock_$(name)

subcode: report_clocks(@namelist)
    $(for:i in $(namelist))
        $print "    clock_$(i): %f sec", (double)clock_$(i)/CLOCKS_PER_SEC

subcode: clock_it(name)
    $call start_clock
    BLOCK
    $print "  $(name): %f sec", (double)(clock()-clock_start)/CLOCKS_PER_SEC

# ---- sort -------------------------------------
subcode: qsort(list, size, type)
    $function qsort_cmp(const void * a, const void * b)
        $return_type int
        $(set:a=(*($(type)*)a))
        $(set:b=(*($(type)*)b))
        BLOCK
    qsort($(list), $(size), sizeof($(type)), $(lambda))

    # -----
    subcode: numcmp(a, b)
        $if $(a)<$(b)
            return -1
        $elif $(a)>$(b)
            return 1
        $else
            return 0
        

subcode: numcmp(na, nb)
    $(allow_recurse:10)
    $if $(na)<$(nb)
        return -1
    $elif $(na)>$(nb)
        return 1
    $else
        BLOCK

#---- scanf ------------------------------------ 
fncode: get_s_int(s, int * p_n)
    $autolist skip
    $return_type char *
    n=0
    $while *s>='0' && *s<='9'
        n*=10
        n+=(*s-'0')
        s++
    *p_n=n
    $while *s==' '
        s++
    return s

#---- template -----
subcode: global_init
    DUMP_STUB global_init
    $call @main

subcode: fn_(@line)
    $(set:name=$(line:0:word))
    DUMP_STUB $(name)_pre
    $in_function $(line)
        $call fn_$(name)