summaryrefslogtreecommitdiff
path: root/gs/src/zfname.c
blob: 1611b382533c05e77e532b3cafa3ea64742a4197 (plain)
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
/* Copyright (C) 1993, 1999 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */


/* File name utilities */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "ialloc.h"
#include "stream.h"
#include "gxiodev.h"		/* must come after stream.h */
#include "fname.h"

/* Parse a file name into device and individual name. */
/* The device may be NULL, or the name may be NULL, but not both. */
/* According to the Adobe documentation, %device and %device% */
/* are equivalent; both return name==NULL. */
int
parse_file_name(const ref * op, parsed_file_name * pfn)
{
    const byte *pname;
    uint len, dlen;
    const byte *pdelim;
    gx_io_device *iodev;

    check_read_type(*op, t_string);
    len = r_size(op);
    pname = op->value.const_bytes;
    if (len == 0)
	return_error(e_undefinedfilename);
    if (pname[0] != '%') {	/* no device */
	pfn->iodev = NULL;
	pfn->fname = (const char *)pname;
	pfn->len = len;
	return 0;
    }
    pdelim = (const byte *)memchr(pname + 1, '%', len - 1);
    if (pdelim == NULL)		/* %device */
	dlen = len;
    else if (pdelim[1] == 0) {	/* %device% */
	pdelim = NULL;
	dlen = len;
    } else {
	dlen = pdelim - pname;
	pdelim++, len--;
    }
    iodev = gs_findiodevice(pname, dlen);
    if (iodev == 0)
	return_error(e_undefinedfilename);
    pfn->iodev = iodev;
    pfn->fname = (const char *)pdelim;
    pfn->len = len - dlen;
    return 0;
}

/* Parse a real (non-device) file name and convert to a C string. */
int
parse_real_file_name(const ref * op, parsed_file_name * pfn, client_name_t cname)
{
    int code = parse_file_name(op, pfn);

    if (code < 0)
	return code;
    if (pfn->len == 0)
	return_error(e_invalidfileaccess);	/* device only */
    return terminate_file_name(pfn, cname);
}

/* Convert a file name to a C string by adding a null terminator. */
int
terminate_file_name(parsed_file_name * pfn, client_name_t cname)
{
    uint len = pfn->len;
    ref fnref;
    const char *fname;

    if (pfn->iodev == NULL)	/* no device */
	pfn->iodev = iodev_default;
    fnref.value.const_bytes = (const byte *)pfn->fname;
    r_set_size(&fnref, len);
    fname = ref_to_string(&fnref, imemory, cname);
    if (fname == 0)
	return_error(e_VMerror);
    pfn->fname = fname;
    pfn->len = len + 1;		/* null terminator */
    return 0;
}

/* Free a file name that was copied to a C string. */
void
free_file_name(parsed_file_name * pfn, client_name_t cname)
{
    if (pfn->fname != 0)
	ifree_const_string((const byte *)pfn->fname, pfn->len, cname);
}