Skip to content

Commit

Permalink
dmd.backend.filespec: Remove unused functions (#16825)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis Korpel <dennis@sarc.nl>
  • Loading branch information
dkorpel and Dennis Korpel authored Sep 3, 2024
1 parent cbbc96f commit 6a79518
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 274 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/cgobj.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import dmd.backend.dlist;
import dmd.backend.dvarstats;
import dmd.backend.dvec;
import dmd.backend.el;
import dmd.backend.filespec : filespecdotext, filespecgetroot, filespecname;
import dmd.backend.filespec : filespecgetroot, filespecname;
import dmd.backend.mem;
import dmd.backend.global;
import dmd.backend.obj;
Expand Down
273 changes: 0 additions & 273 deletions compiler/src/dmd/backend/filespec.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,215 +15,19 @@ import dmd.backend.mem;
nothrow:
@safe:

/*********************************
* String compare of filenames.
*/

version (Windows)
{
extern (C)
{
int stricmp(const(char)*, const(char)*) pure nothrow @nogc;
int memicmp(const(void)*, const(void)*, size_t) pure nothrow @nogc;
}

alias filespeccmp = stricmp;
alias filespecmemcmp = memicmp;

enum DIRCHAR = '\\';

bool ispathdelim(char c) { return c == DIRCHAR || c == ':' || c == '/'; }
}
else
{
import core.stdc.string : strcmp, memcmp;
alias filespeccmp = strcmp;
alias filespecmemcmp = memcmp;

enum DIRCHAR = '/';

bool ispathdelim(char c) { return c == DIRCHAR; }
}

/****************************
* Combine path and filename to form a filespec.
* Input:
* path Path, with or without trailing /
* (can be NULL)
* filename Cannot be NULL
* Returns:
* filespec mem_malloc'd file specification
* NULL Out of memory
*/
@trusted
char *filespecaddpath(const(char)* path, const(char)* filename)
{
char* filespec;
size_t pathlen;

if (!path || (pathlen = strlen(path)) == 0)
filespec = mem_strdup(filename);
else
{
filespec = cast(char*) mem_malloc(pathlen + 1 + strlen(filename) + 1);
if (filespec)
{
strcpy(filespec,path);
version (Windows)
{
if (!ispathdelim(filespec[pathlen - 1]))
strcat(filespec,"\\");
}
else
{
if (!ispathdelim(filespec[pathlen - 1]))
strcat(filespec,"/");
}
strcat(filespec,filename);
}
}
return filespec;
}

/******************************* filespecrootpath **************************
* Purpose: To expand a relative path into an absolute path.
*
* Side Effects: mem_frees input string.
*
* Returns: mem_malloced string with absolute path.
* NULL if some failure.
*/

version (Windows)
extern (C) char* getcwd(char*, size_t);
else
{
import core.sys.posix.unistd: getcwd;
}

@trusted
char *filespecrootpath(char* filespec)
{
char *cwd;
char *cwd_t;
char *p;
char *p2;

if (!filespec)
return filespec;
version (Windows)
{
// if already absolute (with \ or drive:) ...
if (*filespec == DIRCHAR || (isalpha(*filespec) && *(filespec+1) == ':'))
return filespec; // ... return input string
}
else
{
if (*filespec == DIRCHAR) // already absolute ...
return filespec; // ... return input string
}

// get current working directory path
version (Windows)
{
char[132] cwd_d = void;
if (getcwd(cwd_d.ptr, cwd_d.length))
cwd_t = cwd_d.ptr;
else
cwd_t = null;
}
else
{
cwd_t = cast(char *)getcwd(null, 256);
}

if (cwd_t == null)
{
mem_free(filespec);
return null; // error - path too long (more than 256 chars !)
}
cwd = mem_strdup(cwd_t); // convert cwd to mem package
version (Windows)
{
}
else
{
free(cwd_t);
}
p = filespec;
while (p != null)
{
p2 = cast(char*)strchr(p, DIRCHAR);
if (p2 != null)
{
*p2 = '\0';
if (strcmp(p, "..") == 0) // move up cwd
// remove last directory from cwd
*(cast(char *)strrchr(cwd, DIRCHAR)) = '\0';
else if (strcmp(p, ".") != 0) // not current directory
{
cwd_t = cwd;
const cwdlen = strlen(cwd_t) + 1 + strlen(p) + 1;
cwd = cast(char *)mem_calloc(cwdlen);
snprintf(cwd, cwdlen, "%s%c%s", cwd_t, DIRCHAR, p); // add relative directory
mem_free(cwd_t);
}
// else if ".", then ignore - it means current directory
*p2 = DIRCHAR;
p2++;
}
else if (strcmp(p,"..") == 0) // move up cwd
{
// remove last directory from cwd
*(cast(char *)strrchr(cwd, DIRCHAR)) = '\0';
}
else if (strcmp(p,".") != 0) // no more subdirectories ...
{ // ... save remaining string
cwd_t = cwd;
const cwdlen = strlen(cwd_t) + 1 + strlen(p) + 1;
cwd = cast(char *)mem_calloc(cwdlen);
snprintf(cwd, cwdlen, "%s%c%s", cwd_t, DIRCHAR, p); // add relative directory
mem_free(cwd_t);
}
p = p2;
}
mem_free(filespec);

return cwd;
}

/*****************************
* Add extension onto filespec, if one isn't already there.
* Input:
* filespec Cannot be NULL
* ext Extension (without the .)
* Returns:
* mem_malloc'ed string (NULL if error)
*/
@trusted
char *filespecdefaultext(const(char)* filespec, const(char)* ext)
{
char *p;

const(char)* pext = filespecdotext(filespec);
if (*pext == '.') /* if already got an extension */
{
p = mem_strdup(filespec);
}
else
{
const n = pext - filespec;
p = cast(char *) mem_malloc(n + 1 + strlen(ext) + 1);
if (p)
{
memcpy(p,filespec,n);
p[n] = '.';
strcpy(&p[n + 1],ext);
}
}
return p;
}

/**********************
* Return string that is the dot and extension.
* The string returned is NOT mem_malloc'ed.
Expand Down Expand Up @@ -252,43 +56,6 @@ char *filespecdotext(const(char)* filespec)
return cast(char*)p;
}

/*****************************
* Force extension onto filespec.
* Input:
* filespec String that may or may not contain an extension
* ext Extension that doesn't contain a .
* Returns:
* mem_malloc'ed string (NULL if error)
* NULL if filespec is NULL
* If ext is NULL, return mem_strdup(filespec)
*/
@trusted
char *filespecforceext(const(char)* filespec, const(char)* ext)
{
char* p;

if (ext && *ext == '.')
ext++;
if ((p = cast(char *)filespec) != null)
{
const(char)* pext = filespecdotext(filespec);
if (ext)
{
size_t n = pext - filespec;
p = cast(char*) mem_malloc(n + 1 + strlen(ext) + 1);
if (p)
{
memcpy(p, filespec, n);
p[n] = '.';
strcpy(&p[n + 1],ext);
}
}
else
p = mem_strdup(filespec);
}
return p;
}

/***********************
* Get root name of file name.
* That is, return a mem_strdup()'d version of the filename without
Expand Down Expand Up @@ -325,43 +92,3 @@ char *filespecname(const(char)* filespec)
{ }
return cast(char *)p;
}


/*****************************
* Convert filespec into a backup filename appropriate for the
* operating system. For instance, under MS-DOS path\filename.ext will
* be converted to path\filename.bak.
* Input:
* filespec String that may or may not contain an extension
* Returns:
* mem_malloc'ed string (NULL if error)
* NULL if filespec is NULL
*/

@trusted
char *filespecbackup(const(char)* filespec)
{
version (Windows)
{
return filespecforceext(filespec,"BAK");
}
else
{
char* p;
char* f;

// Prepend .B to file name, if it isn't already there
if (!filespec)
return cast(char *)filespec;
p = filespecname(filespec);
if (p[0] == '.' && p[1] == 'B')
return mem_strdup(filespec);
f = cast(char *) mem_malloc(strlen(filespec) + 2 + 1);
if (f)
{ strcpy(f,filespec);
strcpy(&f[p - filespec],".B");
strcat(f,p);
}
return f;
}
}

0 comments on commit 6a79518

Please sign in to comment.