41 #include <sys/types.h>
63 #define MAX_STREAMS_OPEN 200
67 #define DEBUG_DELETE if (0)
68 #define DEBUG_STREAM_LEN if (0)
69 #define DEBUG_ASSERT if (0)
73 #define STREAM_TMPDIR "STREAM_DIR"
76 #define BASE_NAME "STREAM"
78 #define STREAM_BUFFER_SIZE (1 << 18)
188 const char *
name()
const;
238 cerr <<
"ERROR: setvbuf failed (stream " <<
path
239 <<
") with: " << strerror(errno) << endl;
248 logical_bos = logical_eos = -1;
269 if (path_name ==
NULL) {
283 cerr <<
"ERROR: setvbuf failed (stream " <<
path
284 <<
") with: " << strerror(errno) << endl;
300 logical_bos = logical_eos = -1;
327 if (substream_level) {
328 if ((sub_begin >= (logical_eos - logical_bos)) ||
329 (sub_end >= (logical_eos - logical_bos))) {
335 off_t len = stream_len();
336 if (sub_begin > len || sub_end > len) {
348 if (substream_level) {
368 *sub_stream = substr;
383 off_t posn_save, st_size;
386 if (posn_save == -1) {
387 perror(
"ERROR: AMI_STREAM::stream_len(): ftell(fp) failed ");
395 perror(
"ERROR: AMI_STREAM::stream_len(): ftell[SEEK_END] failed ");
400 G_fseek(fp, posn_save, SEEK_SET);
406 return (st_size /
sizeof(T));
409 if (stat(
path, &statbuf) == -1) {
410 perror(
"AMI_STREAM::stream_len(): fstat failed ");
417 path, (
long long int)statbuf.st_size,
sizeof(T));
419 return (statbuf.st_size /
sizeof(T));
429 *stream_name =
new char[strlen(
path) + 1];
450 if (substream_level) {
451 if (offset > (
unsigned)(logical_eos - logical_bos)) {
453 cerr <<
"ERROR: AMI_STREAM::seek bos=" << logical_bos
454 <<
", eos=" << logical_eos <<
", offset " << offset
455 <<
" out of range.\n";
461 seek_offset = (logical_bos + offset) *
sizeof(T);
466 seek_offset = offset *
sizeof(T);
469 G_fseek(fp, seek_offset, SEEK_SET);
481 switch (usage_type) {
510 if (unlink(
path) == -1) {
511 cerr <<
"ERROR: AMI_STREAM: failed to unlink " <<
path << endl;
512 perror(
"cannot unlink: ");
531 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
535 if (fread((
char *)(&read_tmp),
sizeof(T), 1, fp) < 1) {
541 cerr <<
"ERROR: file=" <<
path <<
":";
542 perror(
"cannot read!");
560 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
565 nobj = fread((
void *)data,
sizeof(T), len, fp);
575 cerr <<
"ERROR: file=" <<
path <<
":";
576 perror(
"cannot read!");
593 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
597 if (fwrite((
char *)(&elt),
sizeof(T), 1, fp) < 1) {
598 cerr <<
"ERROR: AMI_STREAM::write_item failed.\n";
602 perror(
"AMI_STREAM::write_item: ");
619 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
623 nobj = fwrite(data,
sizeof(T), len, fp);
625 cerr <<
"ERROR: AMI_STREAM::write_array failed.\n";
629 perror(
"AMI_STREAM::write_array: ");
654 static char desc[BUFSIZ + 256];
655 snprintf(desc,
sizeof(desc),
"[AMI_STREAM %s %ld]",
path,
#define STREAM_BUFFER_SIZE
@ AMI_APPEND_WRITE_STREAM
const char * ami_str_error[]
int ami_single_temp_name(const std::string &base, char *tmp_path)
@ AMI_ERROR_END_OF_STREAM
@ AMI_ERROR_PERMISSION_DENIED
@ AMI_ERROR_NO_MAIN_MEMORY_OPERATION
@ AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS
@ AMI_ERROR_OBJECT_INITIALIZATION
@ AMI_ERROR_INSUFFICIENT_MAIN_MEMORY
@ AMI_ERROR_ENV_UNDEFINED
FILE * open_stream(int fd, AMI_stream_type st)
static AMI_err main_memory_usage(size_t *usage, MM_stream_usage usage_type=MM_STREAM_USAGE_OVERHEAD)
AMI_err write_item(const T &elt)
AMI_err write_array(const T *data, off_t len)
AMI_err read_array(T *data, off_t len, off_t *lenp=NULL)
AMI_err new_substream(AMI_stream_type st, off_t sub_begin, off_t sub_end, AMI_STREAM< T > **sub_stream)
AMI_err seek(off_t offset)
AMI_err read_item(T **elt)
void persist(persistence p)
const char * name() const
AMI_stream_type access_mode
static unsigned int get_block_length()
unsigned int substream_level
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
off_t G_ftell(FILE *)
Get the current file position of the stream.
#define assert(condition)
@ MM_STREAM_USAGE_OVERHEAD
@ MM_STREAM_USAGE_MAXIMUM
@ MM_STREAM_USAGE_CURRENT