zfile1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (C) 1989, 2000, 2001 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zfile1.c,v 1.12 2004/07/14 15:59:51 giles Exp $ */
  14. /* Special file operators */
  15. #include "memory_.h"
  16. #include "string_.h"
  17. #include "ghost.h"
  18. #include "gp.h"
  19. #include "ierrors.h"
  20. #include "oper.h"
  21. #include "ialloc.h"
  22. #include "opdef.h"
  23. #include "opcheck.h"
  24. #include "store.h"
  25. /* <string> <string> <bool> .file_name_combine <string> true */
  26. /* <string> <string> <bool> .file_name_combine <string> <string> false */
  27. private int
  28. zfile_name_combine(i_ctx_t *i_ctx_p)
  29. {
  30. uint plen, flen, blen, blen0;
  31. const byte *prefix, *fname;
  32. byte *buffer;
  33. os_ptr op = osp;
  34. bool no_sibling;
  35. check_type(op[ 0], t_boolean);
  36. check_type(op[-1], t_string);
  37. check_type(op[-2], t_string);
  38. plen = r_size(op - 2);
  39. flen = r_size(op - 1);
  40. blen = blen0 = plen + flen + 2; /* Inserts separator and ending zero byte. */
  41. buffer = ialloc_string(blen, "zfile_name_combine");
  42. if (buffer == 0)
  43. return_error(e_VMerror);
  44. prefix = op[-2].value.const_bytes;
  45. fname = op[-1].value.const_bytes;
  46. no_sibling = op[0].value.boolval;
  47. if (gp_file_name_combine((const char *)prefix, plen,
  48. (const char *)fname, flen, no_sibling,
  49. (char *)buffer, &blen) != gp_combine_success) {
  50. make_bool(op, false);
  51. } else {
  52. buffer = iresize_string(buffer, blen0, blen, "zfile_name_combine");
  53. if (buffer == 0)
  54. return_error(e_VMerror);
  55. make_string(op - 2, a_all | icurrent_space, blen, buffer);
  56. make_bool(op - 1, true);
  57. pop(1);
  58. }
  59. return 0;
  60. }
  61. /* This is compiled conditionally to let PS library to know
  62. * whether it works with the new gp_combine_file_name.
  63. */
  64. /* <string> .file_name_is_absolute <bool> */
  65. private int
  66. zfile_name_is_absolute(i_ctx_t *i_ctx_p)
  67. { os_ptr op = osp;
  68. check_type(op[0], t_string);
  69. make_bool(op, (gp_file_name_root((const char *)op->value.const_bytes,
  70. r_size(op)) > 0));
  71. return 0;
  72. }
  73. private int
  74. push_string(i_ctx_t *i_ctx_p, const char *v)
  75. { os_ptr op = osp;
  76. int len = strlen(v);
  77. push(1);
  78. make_const_string(op, avm_foreign | a_readonly,
  79. len, (const byte *)v);
  80. return 0;
  81. }
  82. /* - .file_name_separator <string> */
  83. private int
  84. zfile_name_separator(i_ctx_t *i_ctx_p)
  85. { return push_string(i_ctx_p, gp_file_name_separator());
  86. }
  87. /* - .file_name_directory_separator <string> */
  88. private int
  89. zfile_name_directory_separator(i_ctx_t *i_ctx_p)
  90. { return push_string(i_ctx_p, gp_file_name_directory_separator());
  91. }
  92. /* - .file_name_current <string> */
  93. private int
  94. zfile_name_current(i_ctx_t *i_ctx_p)
  95. { return push_string(i_ctx_p, gp_file_name_current());
  96. }
  97. /* - .file_name_parent <string> */
  98. private int
  99. zfile_name_parent(i_ctx_t *i_ctx_p)
  100. { return push_string(i_ctx_p, gp_file_name_parent());
  101. }
  102. const op_def zfile1_op_defs[] =
  103. {
  104. {"0.file_name_combine", zfile_name_combine},
  105. {"0.file_name_is_absolute", zfile_name_is_absolute},
  106. {"0.file_name_separator", zfile_name_separator},
  107. {"0.file_name_directory_separator", zfile_name_directory_separator},
  108. {"0.file_name_current", zfile_name_current},
  109. {"0.file_name_parent", zfile_name_parent},
  110. op_def_end(0)
  111. };