xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/d/dmd/utils.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg 
2*627f7eb2Smrg /* Compiler implementation of the D programming language
3*627f7eb2Smrg  * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
4*627f7eb2Smrg  * written by Walter Bright
5*627f7eb2Smrg  * http://www.digitalmars.com
6*627f7eb2Smrg  * Distributed under the Boost Software License, Version 1.0.
7*627f7eb2Smrg  * http://www.boost.org/LICENSE_1_0.txt
8*627f7eb2Smrg  */
9*627f7eb2Smrg 
10*627f7eb2Smrg #include "root/dsystem.h"
11*627f7eb2Smrg #include "mars.h"
12*627f7eb2Smrg #include "globals.h"
13*627f7eb2Smrg #include "root/file.h"
14*627f7eb2Smrg #include "root/filename.h"
15*627f7eb2Smrg #include "root/outbuffer.h"
16*627f7eb2Smrg #include "root/rmem.h"
17*627f7eb2Smrg 
18*627f7eb2Smrg /**
19*627f7eb2Smrg  * Normalize path by turning forward slashes into backslashes
20*627f7eb2Smrg  *
21*627f7eb2Smrg  * Params:
22*627f7eb2Smrg  *   src = Source path, using unix-style ('/') path separators
23*627f7eb2Smrg  *
24*627f7eb2Smrg  * Returns:
25*627f7eb2Smrg  *   A newly-allocated string with '/' turned into backslashes
26*627f7eb2Smrg  */
toWinPath(const char * src)27*627f7eb2Smrg const char * toWinPath(const char *src)
28*627f7eb2Smrg {
29*627f7eb2Smrg     if (src == NULL)
30*627f7eb2Smrg         return NULL;
31*627f7eb2Smrg 
32*627f7eb2Smrg     char *result = mem.xstrdup(src);
33*627f7eb2Smrg     char *p = result;
34*627f7eb2Smrg     while (*p != '\0')
35*627f7eb2Smrg     {
36*627f7eb2Smrg         if (*p == '/')
37*627f7eb2Smrg             *p = '\\';
38*627f7eb2Smrg         p++;
39*627f7eb2Smrg     }
40*627f7eb2Smrg     return result;
41*627f7eb2Smrg }
42*627f7eb2Smrg 
43*627f7eb2Smrg /**
44*627f7eb2Smrg  * Reads a file, terminate the program on error
45*627f7eb2Smrg  *
46*627f7eb2Smrg  * Params:
47*627f7eb2Smrg  *   loc = The line number information from where the call originates
48*627f7eb2Smrg  *   f = a `ddmd.root.file.File` handle to read
49*627f7eb2Smrg  */
readFile(Loc loc,File * f)50*627f7eb2Smrg void readFile(Loc loc, File *f)
51*627f7eb2Smrg {
52*627f7eb2Smrg     if (f->read())
53*627f7eb2Smrg     {
54*627f7eb2Smrg         error(loc, "Error reading file '%s'", f->name->toChars());
55*627f7eb2Smrg         fatal();
56*627f7eb2Smrg     }
57*627f7eb2Smrg }
58*627f7eb2Smrg 
59*627f7eb2Smrg /**
60*627f7eb2Smrg  * Writes a file, terminate the program on error
61*627f7eb2Smrg  *
62*627f7eb2Smrg  * Params:
63*627f7eb2Smrg  *   loc = The line number information from where the call originates
64*627f7eb2Smrg  *   f = a `ddmd.root.file.File` handle to write
65*627f7eb2Smrg  */
writeFile(Loc loc,File * f)66*627f7eb2Smrg void writeFile(Loc loc, File *f)
67*627f7eb2Smrg {
68*627f7eb2Smrg     if (f->write())
69*627f7eb2Smrg     {
70*627f7eb2Smrg         error(loc, "Error writing file '%s'", f->name->toChars());
71*627f7eb2Smrg         fatal();
72*627f7eb2Smrg     }
73*627f7eb2Smrg }
74*627f7eb2Smrg 
75*627f7eb2Smrg /**
76*627f7eb2Smrg  * Ensure the root path (the path minus the name) of the provided path
77*627f7eb2Smrg  * exists, and terminate the process if it doesn't.
78*627f7eb2Smrg  *
79*627f7eb2Smrg  * Params:
80*627f7eb2Smrg  *   loc = The line number information from where the call originates
81*627f7eb2Smrg  *   name = a path to check (the name is stripped)
82*627f7eb2Smrg  */
ensurePathToNameExists(Loc loc,const char * name)83*627f7eb2Smrg void ensurePathToNameExists(Loc loc, const char *name)
84*627f7eb2Smrg {
85*627f7eb2Smrg     const char *pt = FileName::path(name);
86*627f7eb2Smrg     if (*pt)
87*627f7eb2Smrg     {
88*627f7eb2Smrg         if (FileName::ensurePathExists(pt))
89*627f7eb2Smrg         {
90*627f7eb2Smrg             error(loc, "cannot create directory %s", pt);
91*627f7eb2Smrg             fatal();
92*627f7eb2Smrg         }
93*627f7eb2Smrg     }
94*627f7eb2Smrg     FileName::free(pt);
95*627f7eb2Smrg }
96*627f7eb2Smrg 
97*627f7eb2Smrg /**
98*627f7eb2Smrg  * Takes a path, and escapes '(', ')' and backslashes
99*627f7eb2Smrg  *
100*627f7eb2Smrg  * Params:
101*627f7eb2Smrg  *   buf = Buffer to write the escaped path to
102*627f7eb2Smrg  *   fname = Path to escape
103*627f7eb2Smrg  */
escapePath(OutBuffer * buf,const char * fname)104*627f7eb2Smrg void escapePath(OutBuffer *buf, const char *fname)
105*627f7eb2Smrg {
106*627f7eb2Smrg     while (1)
107*627f7eb2Smrg     {
108*627f7eb2Smrg         switch (*fname)
109*627f7eb2Smrg         {
110*627f7eb2Smrg             case 0:
111*627f7eb2Smrg                 return;
112*627f7eb2Smrg             case '(':
113*627f7eb2Smrg             case ')':
114*627f7eb2Smrg             case '\\':
115*627f7eb2Smrg                 buf->writeByte('\\');
116*627f7eb2Smrg                 /* fall through */
117*627f7eb2Smrg             default:
118*627f7eb2Smrg                 buf->writeByte(*fname);
119*627f7eb2Smrg                 break;
120*627f7eb2Smrg         }
121*627f7eb2Smrg         fname++;
122*627f7eb2Smrg     }
123*627f7eb2Smrg }
124