43 #include <sys/types.h>
65 #define MAX_STREAMS_OPEN 200
69 #define DEBUG_DELETE if(0)
70 #define DEBUG_STREAM_LEN if(0)
71 #define DEBUG_ASSERT if(0)
75 #define STREAM_TMPDIR "STREAM_DIR"
78 #define BASE_NAME "STREAM"
80 #define STREAM_BUFFER_SIZE (1<<18)
194 const char*
name()
const;
249 cerr <<
"ERROR: setvbuf failed (stream " <<
path <<
") with: "
250 << strerror(errno) << endl;
259 logical_bos = logical_eos = -1;
281 if(path_name ==
NULL) {
286 strcpy(
path, path_name);
294 cerr <<
"ERROR: setvbuf failed (stream " <<
path <<
") with: "
295 << strerror(errno) << endl;
310 logical_bos = logical_eos = -1;
340 if (substream_level) {
341 if( (sub_begin >= (logical_eos - logical_bos)) ||
342 (sub_end >= (logical_eos - logical_bos)) ) {
347 off_t len = stream_len();
348 if (sub_begin > len || sub_end > len) {
360 if (substream_level) {
379 *sub_stream = substr;
395 off_t posn_save, st_size;
398 if(posn_save == -1) {
399 perror(
"ERROR: AMI_STREAM::stream_len(): ftell(fp) failed ");
407 perror(
"ERROR: AMI_STREAM::stream_len(): ftell[SEEK_END] failed ");
412 G_fseek(fp, posn_save, SEEK_SET);
416 path, st_size,
sizeof(T));
418 return (st_size /
sizeof(T));
421 if (stat(
path, &statbuf) == -1) {
422 perror(
"AMI_STREAM::stream_len(): fstat failed ");
429 path, (
long long int)statbuf.st_size,
sizeof(T));
431 return (statbuf.st_size /
sizeof(T));
442 *stream_name =
new char [strlen(
path) + 1];
443 strcpy(*stream_name,
path);
464 if (substream_level) {
465 if (offset > (
unsigned) (logical_eos - logical_bos)) {
467 cerr <<
"ERROR: AMI_STREAM::seek bos=" << logical_bos <<
", eos="
468 << logical_eos <<
", offset " << offset <<
" out of range.\n";
473 seek_offset = (logical_bos + offset) *
sizeof(T);
479 seek_offset = offset *
sizeof(T);
482 G_fseek(fp, seek_offset, SEEK_SET);
496 switch (usage_type) {
526 if (unlink(
path) == -1) {
527 cerr <<
"ERROR: AMI_STREAM: failed to unlink " <<
path << endl;
528 perror(
"cannot unlink: ");
548 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
552 if (fread((
char *) (&read_tmp),
sizeof(T), 1, fp) < 1) {
557 cerr <<
"ERROR: file=" <<
path <<
":";
558 perror(
"cannot read!");
578 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
583 nobj = fread((
void*)data,
sizeof(T), len, fp);
587 if(lenp) *lenp = nobj;
591 cerr <<
"ERROR: file=" <<
path <<
":";
592 perror(
"cannot read!");
596 if(lenp) *lenp = nobj;
610 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
614 if (fwrite((
char*)(&elt),
sizeof(T), 1,fp) < 1) {
615 cerr <<
"ERROR: AMI_STREAM::write_item failed.\n";
619 perror(
"AMI_STREAM::write_item: ");
636 if ((logical_eos >= 0) &&
G_ftell(fp) >=
sizeof(T) * logical_eos) {
640 nobj = fwrite(data,
sizeof(T), len, fp);
642 cerr <<
"ERROR: AMI_STREAM::write_array failed.\n";
646 perror(
"AMI_STREAM::write_array: ");
672 static char desc[BUFSIZ + 256];
673 sprintf(desc,
"[AMI_STREAM %s %ld]",
path, (
long)stream_len());
#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