gsfname.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (C) 1993, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gsfname.c,v 1.2 2000/09/19 19:00:28 lpd Exp $ */
  16. /* File name utilities */
  17. #include "memory_.h"
  18. #include "gserror.h"
  19. #include "gserrors.h"
  20. #include "gsmemory.h"
  21. #include "gstypes.h"
  22. #include "gsfname.h"
  23. #include "gxiodev.h"
  24. /* Parse a file name into device and individual name. */
  25. /* The device may be NULL, or the name may be NULL, but not both. */
  26. /* According to the Adobe documentation, %device and %device% */
  27. /* are equivalent; both return name==NULL. */
  28. int
  29. gs_parse_file_name(gs_parsed_file_name_t * pfn, const char *pname, uint len)
  30. {
  31. uint dlen;
  32. const char *pdelim;
  33. gx_io_device *iodev;
  34. if (len == 0)
  35. return_error(gs_error_undefinedfilename); /* null name not allowed */
  36. if (pname[0] != '%') { /* no device */
  37. pfn->memory = 0;
  38. pfn->iodev = NULL;
  39. pfn->fname = pname;
  40. pfn->len = len;
  41. return 0;
  42. }
  43. pdelim = memchr(pname + 1, '%', len - 1);
  44. if (pdelim == NULL) /* %device */
  45. dlen = len;
  46. else if (pdelim[1] == 0) { /* %device% */
  47. pdelim = NULL;
  48. dlen = len;
  49. } else {
  50. dlen = pdelim - pname;
  51. pdelim++, len--;
  52. }
  53. iodev = gs_findiodevice((const byte *)pname, dlen);
  54. if (iodev == 0)
  55. return_error(gs_error_undefinedfilename);
  56. pfn->memory = 0;
  57. pfn->iodev = iodev;
  58. pfn->fname = pdelim;
  59. pfn->len = len - dlen;
  60. return 0;
  61. }
  62. /* Parse a real (non-device) file name and convert to a C string. */
  63. int
  64. gs_parse_real_file_name(gs_parsed_file_name_t * pfn, const char *pname,
  65. uint len, gs_memory_t *mem, client_name_t cname)
  66. {
  67. int code = gs_parse_file_name(pfn, pname, len);
  68. if (code < 0)
  69. return code;
  70. if (pfn->len == 0)
  71. return_error(gs_error_invalidfileaccess); /* device only */
  72. return gs_terminate_file_name(pfn, mem, cname);
  73. }
  74. /* Convert a file name to a C string by adding a null terminator. */
  75. int
  76. gs_terminate_file_name(gs_parsed_file_name_t * pfn, gs_memory_t *mem,
  77. client_name_t cname)
  78. {
  79. uint len = pfn->len;
  80. char *fname;
  81. if (pfn->iodev == NULL) /* no device */
  82. pfn->iodev = iodev_default;
  83. if (pfn->memory)
  84. return 0; /* already copied */
  85. /* Copy the file name to a C string. */
  86. fname = (char *)gs_alloc_string(mem, len + 1, cname);
  87. if (fname == 0)
  88. return_error(gs_error_VMerror);
  89. memcpy(fname, pfn->fname, len);
  90. fname[len] = 0;
  91. pfn->memory = mem;
  92. pfn->fname = fname;
  93. pfn->len = len + 1; /* null terminator */
  94. return 0;
  95. }
  96. /* Free a file name that was copied to a C string. */
  97. void
  98. gs_free_file_name(gs_parsed_file_name_t * pfn, client_name_t cname)
  99. {
  100. if (pfn->fname != 0)
  101. gs_free_const_string(pfn->memory, (const byte *)pfn->fname, pfn->len,
  102. cname);
  103. }