summary refs log tree commit diff stats
path: root/lib/pure/os.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/os.nim')
-rw-r--r--lib/pure/os.nim68
1 files changed, 20 insertions, 48 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 433a79c5e..233967753 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -184,7 +184,7 @@ proc OSErrorMsg*(): string {.rtl, extern: "nos$1".} =
         if FormatMessageW(0x00000100 or 0x00001000 or 0x00000200,
                           nil, err, 0, addr(msgbuf), 0, nil) != 0'i32:
           result = $msgbuf
-          if msgbuf != nil: LocalFree(msgbuf)
+          if msgbuf != nil: LocalFree(cast[pointer](msgbuf))
       else:
         var msgbuf: cstring
         if FormatMessageA(0x00000100 or 0x00001000 or 0x00000200,
@@ -254,20 +254,14 @@ proc UnixToNativePath*(path: string): string {.
 
 when defined(windows):
   template wrapUnary(varname, winApiProc, arg: expr) {.immediate.} =
-    var tmp = allocWideCString(arg)
-    var varname = winApiProc(tmp)
-    dealloc tmp
+    var varname = winApiProc(newWideCString(arg))
 
   template wrapBinary(varname, winApiProc, arg, arg2: expr) {.immediate.} =
-    var tmp2 = allocWideCString(arg)
-    var varname = winApiProc(tmp2, arg2)
-    dealloc tmp2
+    var varname = winApiProc(newWideCString(arg), arg2)
 
   when useWinUnicode:
     proc FindFirstFile(a: string, b: var TWIN32_FIND_DATA): THandle =
-      var aa = allocWideCString(a)
-      result = FindFirstFileW(aa, b)
-      dealloc aa
+      result = FindFirstFileW(newWideCString(a), b)
     template FindNextFile(a, b: expr): expr = FindNextFileW(a, b)
     template getCommandLine(): expr = getCommandLineW()
 
@@ -363,11 +357,10 @@ proc getCurrentDir*(): string {.rtl, extern: "nos$1", tags: [].} =
   const bufsize = 512 # should be enough
   when defined(windows):
     when useWinUnicode:
-      var res = cast[wideCString](alloc0(bufsize+1))
+      var res = newWideCString("", bufsize)
       var L = GetCurrentDirectoryW(bufsize, res)
-      result = res$L
-      dealloc res
       if L == 0'i32: OSError()
+      result = res$L
     else:
       result = newString(bufsize)
       var L = GetCurrentDirectoryA(bufsize, result)
@@ -385,10 +378,7 @@ proc setCurrentDir*(newDir: string) {.inline, tags: [].} =
   ## `newDir` cannot been set.
   when defined(Windows):
     when useWinUnicode:
-      var x = allocWideCString(newDir)
-      let res = SetCurrentDirectoryW(x)
-      dealloc x
-      if res == 0'i32: OSError()
+      if SetCurrentDirectoryW(newWideCString(newDir)) == 0'i32: OSError()
     else:
       if SetCurrentDirectoryA(newDir) == 0'i32: OSError()
   else:
@@ -579,15 +569,11 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
     const bufsize = 3072'i32
     when useWinUnicode:
       var unused: widecstring
-      var res = cast[widecstring](alloc(bufsize*2+2))
-      var f = allocWideCString(filename)
-      var L = GetFullPathNameW(f, bufsize, res, unused)
-      dealloc f
+      var res = newWideCString("", bufsize div 2)
+      var L = GetFullPathNameW(newWideCString(filename), bufsize, res, unused)
       if L <= 0'i32 or L >= bufsize: 
-        dealloc res
         OSError()
       result = res$L
-      dealloc res
     else:
       var unused: cstring
       result = newString(bufsize)
@@ -673,8 +659,8 @@ proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1",
     var success = true
 
     when useWinUnicode:
-      var p1 = allocWideCString(path1)
-      var p2 = allocWideCString(path2)
+      var p1 = newWideCString(path1)
+      var p2 = newWideCString(path2)
       template OpenHandle(path: expr): expr =
         CreateFileW(path, 0'i32, FILE_SHARE_DELETE or FILE_SHARE_READ or
           FILE_SHARE_WRITE, nil, OPEN_EXISTING,
@@ -705,9 +691,6 @@ proc sameFile*(path1, path2: string): bool {.rtl, extern: "nos$1",
 
     discard CloseHandle(f1)
     discard CloseHandle(f2)
-    when useWinUnicode:
-      dealloc p1
-      dealloc p2
 
     if not success: OSError()
   else:
@@ -754,12 +737,9 @@ proc copyFile*(source, dest: string) {.rtl, extern: "nos$1",
   ## `EOS` is raised.
   when defined(Windows):
     when useWinUnicode:
-      var s = allocWideCString(source)
-      var d = allocWideCString(dest)
-      let res = CopyFileW(s, d, 0'i32)
-      dealloc s
-      dealloc d
-      if res == 0'i32: OSError()
+      let s = newWideCString(source)
+      let d = newWideCString(dest)
+      if CopyFileW(s, d, 0'i32) == 0'i32: OSError()
     else:
       if CopyFileA(source, dest, 0'i32) == 0'i32: OSError()
   else:
@@ -794,11 +774,8 @@ when not defined(ENOENT):
   var ENOENT {.importc, header: "<errno.h>".}: cint
 
 proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} =
-  ## Removes the `file`.
-  ##
-  ## If this fails, `EOS` is raised. This does not fail if the file never
-  ## existed in the first place. Despite the name of this proc you can also
-  ## call it on directories, but they will only be removed if they are empty.
+  ## Removes the `file`. If this fails, `EOS` is raised. This does not fail
+  ## if the file never existed in the first place.
   if cremove(file) != 0'i32 and errno != ENOENT: OSError()
 
 proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", 
@@ -934,12 +911,9 @@ proc putEnv*(key, val: string) {.tags: [FWriteEnv].} =
       OSError()
   else:
     when useWinUnicode:
-      var k = allocWideCString(key)
-      var v = allocWideCString(val)
-      let res = SetEnvironmentVariableW(k, v)
-      dealloc k
-      dealloc v
-      if res == 0'i32: OSError()
+      var k = newWideCString(key)
+      var v = newWideCString(val)
+      if SetEnvironmentVariableW(k, v) == 0'i32: OSError()
     else:
       if SetEnvironmentVariableA(key, val) == 0'i32: OSError()
 
@@ -1082,9 +1056,7 @@ proc removeDir*(dir: string) {.rtl, extern: "nos$1", tags: [
   ## in `dir` (recursively).
   ##
   ## If this fails, `EOS` is raised. This does not fail if the directory never
-  ## existed in the first place. If you need non recursive directory removal
-  ## which fails when the directory contains files use the `removeFile` proc
-  ## instead.
+  ## existed in the first place.
   for kind, path in walkDir(dir): 
     case kind
     of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path)
class='alt'>
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