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) 185 AMI_err write_item(
const T &elt);
186 AMI_err read_array(T *data, off_t len, off_t *lenp=
NULL);
187 AMI_err write_array(
const T *data, off_t len);
190 off_t stream_len(
void);
194 const char*
name()
const;
200 static AMI_err main_memory_usage(
size_t *usage,
249 cerr <<
"ERROR: setvbuf failed (stream " <<
path <<
") with: " 250 << strerror(errno) << endl;
281 if(path_name ==
NULL) {
286 strcpy(
path, path_name);
294 cerr <<
"ERROR: setvbuf failed (stream " <<
path <<
") with: " 295 << strerror(errno) << endl;
347 off_t len = stream_len();
348 if (sub_begin > len || sub_end > len) {
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 ");
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);
467 cerr <<
"ERROR: AMI_STREAM::seek bos=" <<
logical_bos <<
", eos=" 468 <<
logical_eos <<
", offset " << offset <<
" out of range.\n";
479 seek_offset = offset *
sizeof(T);
496 switch (usage_type) {
526 if (unlink(path) == -1) {
527 cerr <<
"ERROR: AMI_STREAM: failed to unlink " << path << endl;
528 perror(
"cannot unlink: ");
552 if (fread((
char *) (&read_tmp),
sizeof(T), 1,
fp) < 1) {
557 cerr <<
"ERROR: file=" <<
path <<
":";
558 perror(
"cannot read!");
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;
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: ");
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];
673 sprintf(desc,
"[AMI_STREAM %s %ld]",
path, (
long)stream_len());
684 #endif // _AMI_STREAM_H
AMI_stream_type access_mode
AMI_err read_item(T **elt)
static AMI_err main_memory_usage(size_t *usage, MM_stream_usage usage_type=MM_STREAM_USAGE_OVERHEAD)
#define STREAM_BUFFER_SIZE
static unsigned int get_block_length()
AMI_err read_array(T *data, off_t len, off_t *lenp=NULL)
AMI_err write_item(const T &elt)
AMI_err new_substream(AMI_stream_type st, off_t sub_begin, off_t sub_end, AMI_STREAM< T > **sub_stream)
unsigned int substream_level
AMI_err seek(off_t offset)
#define assert(condition)
AMI_err write_array(const T *data, off_t len)
FILE * open_stream(int fd, AMI_stream_type st)
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.
void persist(persistence p)
int ami_single_temp_name(const std::string &base, char *tmp_path)
const char * name() const
const char * ami_str_error[]