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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* $Id: memfile.c,v 1.5 2002/02/01 15:47:00 jan Exp $
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: jan@swi.psy.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2002, University of Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#include <stdlib.h>
#include "error.h"
#define streq(s,q) (strcmp((s), (q)) == 0)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Memory-files
make_memory_file(-Handle)
free_memory_file(+Handle)
open_memory_file(+Handle, +Mode, -Stream)
size_memory_file(+Handle, -Size)
memory_file_to_codes(+Handle, -Codes)
memory_file_to_atom(+Handle, -Atom)
atom_to_memory_file(+Atom, -Handle)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static functor_t FUNCTOR_memory_file1;
#define MEMFILE_MAGIC 0x5624a6b3L
typedef struct
{ long magic; /* MEMFILE_MAGIC */
char *data; /* data of the file */
int size; /* length of the file */
IOSTREAM *stream; /* Stream hanging onto it */
atom_t atom; /* Created from atom */
} memfile;
static int
unify_memfile(term_t handle, memfile *f)
{ return PL_unify_term(handle,
PL_FUNCTOR, FUNCTOR_memory_file1,
PL_POINTER, f);
}
static int
get_memfile(term_t handle, memfile **f)
{ if ( PL_is_functor(handle, FUNCTOR_memory_file1) )
{ term_t a = PL_new_term_ref();
void *ptr;
PL_get_arg(1, handle, a);
if ( PL_get_pointer(a, &ptr) )
{ memfile *m = ptr;
if ( m->magic == MEMFILE_MAGIC )
{ *f = ptr;
return TRUE;
}
return pl_error(NULL, 0, NULL, ERR_EXISTENCE,
"memory_file", handle);
}
}
return pl_error(NULL, 0, NULL, ERR_ARGTYPE, 1,
handle, "memory_file");
}
static foreign_t
new_memory_file(term_t handle)
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return FALSE;
m->magic = MEMFILE_MAGIC;
m->data = 0;
m->size = 0;
return unify_memfile(handle, m);
}
static foreign_t
free_memory_file(term_t handle)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ if ( m->stream )
Sclose(m->stream);
if ( m->atom )
PL_unregister_atom(m->atom);
else if ( m->data )
free(m->data);
m->magic = 0;
free(m);
return TRUE;
}
return FALSE;
}
static void
closehook(void *closure)
{ memfile *m = closure;
m->stream = NULL;
}
static foreign_t
alreadyOpen(term_t handle, const char *op)
{ return pl_error(NULL, 0, "already open",
ERR_PERMISSION, handle, op, "memory_file");
}
static foreign_t
open_memory_file(term_t handle, term_t mode, term_t stream)
{ memfile *m;
char *x;
char *s;
IOSTREAM *fd;
if ( !get_memfile(handle, &m) )
return FALSE;
if ( m->stream )
return alreadyOpen(handle, "open");
if ( !PL_get_atom_chars(mode, &s) )
return pl_error("open_memory_file", 3, NULL, ERR_ARGTYPE, 2,
mode, "io_mode");
if ( streq(s, "write") )
{ x = "w";
if ( m->atom )
return pl_error("open_memory_file", 3, NULL, ERR_PERMISSION,
handle, "write", "memory_file");
} else if ( streq(s, "read") )
x = "r";
else
return pl_error("open_memory_file", 3, NULL, ERR_DOMAIN,
mode, "io_mode");
if ( !(fd = Sopenmem(&m->data, &m->size, x)) )
return pl_error("open_memory_file", 3, NULL, ERR_ERRNO,
"memory_file", "create");
fd->close_hook = closehook;
fd->closure = m;
m->stream = fd;
return PL_unify_stream(stream, fd);
}
static foreign_t
size_memory_file(term_t handle, term_t size)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ if ( m->stream && !m->atom )
return alreadyOpen(handle, "size");
if ( m->data )
{ return PL_unify_integer(size, m->size);
} else
return PL_unify_integer(size, 0);
}
return FALSE;
}
static foreign_t
atom_to_memory_file(term_t atom, term_t handle)
{ char *s;
unsigned int len;
if ( !PL_get_atom_nchars(atom, &len, &s) )
return pl_error(NULL, 0, NULL, ERR_ARGTYPE, 1,
atom, "atom");
else
{ memfile *m = calloc(1, sizeof(*m));
if ( !m )
return FALSE;
PL_get_atom(atom, &m->atom);
PL_register_atom(m->atom);
m->magic = MEMFILE_MAGIC;
m->data = s;
m->size = len;
return unify_memfile(handle, m);
}
}
static foreign_t
memory_file_to_atom(term_t handle, term_t atom)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ if ( m->stream )
return alreadyOpen(handle, "to_atom");
if ( m->data )
{ return PL_unify_atom_nchars(atom, m->size, m->data);
} else
return PL_unify_atom_nchars(atom, 0, "");
}
return FALSE;
}
static foreign_t
memory_file_to_codes(term_t handle, term_t codes)
{ memfile *m;
if ( get_memfile(handle, &m) )
{ if ( m->stream )
return alreadyOpen(handle, "to_codes");
if ( m->data )
{ return PL_unify_list_ncodes(codes, m->size, m->data);
} else
return PL_unify_list_ncodes(codes, 0, "");
}
return FALSE;
}
install_t
install_memfile()
{ if ( PL_query(PL_QUERY_VERSION) <= 40006 )
{ PL_warning("Requires SWI-Prolog version 4.0.7 or later");
return;
}
FUNCTOR_memory_file1 = PL_new_functor(PL_new_atom("$memory_file"), 1);
PL_register_foreign("new_memory_file", 1, new_memory_file, 0);
PL_register_foreign("free_memory_file", 1, free_memory_file, 0);
PL_register_foreign("size_memory_file", 2, size_memory_file, 0);
PL_register_foreign("open_memory_file", 3, open_memory_file, 0);
PL_register_foreign("atom_to_memory_file", 2, atom_to_memory_file, 0);
PL_register_foreign("memory_file_to_atom", 2, memory_file_to_atom, 0);
PL_register_foreign("memory_file_to_codes", 2, memory_file_to_codes, 0);
}
|