V
VB
Hi,
here File.cpp and File.h:
File.cpp:
----------------------
#pragma warning (disable: 4786)
#include <cstring>
#include <cstdlib>
#include <cassert>
#include "File.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
File::File(const char* filename, bool ld)
{
fn_in = new char[strlen(filename)+1];
std::strcpy(fn_in,filename);
load = ld;
maxcnt = isetsz = 0;
curr = reccnt = 0;
// bufsid = -1;
}
File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}
bool File::read(ITEMSET& s)
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(!std::fseek(file, pos, SEEK_SET))
error(E_FREAD,fn_in); /* reset file position */
return false; /* return no file read */
}
if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else
get_iset(); /* otherwhise read from file */
curr++; /* and increment the counter */
s = cis; /* return s */
return true;
}
ITEMSET* File::read()
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(std::fseek(file, pos, SEEK_SET) != 0)
error(E_FREAD,fn_in); /* reset file position */
return NULL; /* return no file read */
}
if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else {
get_iset(); /* otherwhise read from file */
cis.prepare(); /* sort and remove duplicates */
}
curr++; /* and increment the counter */
return &cis; /* return s */
}
int File::reorg(long thresh)
{
SymTab tmap; /* temporary symbol table */
int i, size;
ITEM* item;
std::vector<ITEM*> sorted = nimap.bvec; /* bucket vector */
std::sort(sorted.begin(),sorted.end(),frqcmp()); /* order by
frequency */
for (i = 0, size = sorted.size();
(i < size) && (sorted->frq >= thresh); i++)
{
item = (ITEM*)tmap.insert(nimap.ids[sorted->id], sizeof(ITEM));
if (!item)
return E_NOMEM; /* add the new item to the map, */
item->frq = sorted->frq;
}
nimap = tmap;
return 0;
}
int File::reset()
{ /* resets to the beginning */
if (load) /* if to work on memory */
curr = 0; /* set the current pointer to 0 */
else {
if(std::fseek(file, pos, SEEK_SET) != 0) /* otherwise reset the file
pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
return 0; /* return OK */
}
ITEMSET* File::read(long i)
{ /* read i-th record */
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(fseek(file, locpos, SEEK_SET) != 0) /* reset the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
else cis = isets;
return &cis; /* return s */
}
else return NULL; /* return no file read */
}
bool File::load_iset(int i, ITEMSET &trans)
{
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(std::fseek(file, locpos, SEEK_SET) != 0) /* reset the file
pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
} else
trans = isets;
return true; /* return s */
} else
return false; /* return no file read */
trans=isets;
}
char* File::readTrans(int trans)
{
int l = 0;
int i;
for (i = 0; i < isets[trans].size(); i++) {
l += std::strlen(lookup(isets[trans]->id)) + 1;
}
char* result = new char[l + 1];
strcpy(result,"");
for (i = 0; i < isets[trans].size(); i++) {
std::strcat(result,lookup(isets[trans]->id));
std::strcat(result," ");
}
//fn_in = new char[strlen(filename)+1];
//strcpy(fn_in,filename);
return result;
}
----------------------
Here file.h:
------------------------------------
#ifndef FILE_HPP_INCLUDED
#define FILE_HPP_INCLUDED
#include <vector>
#include "resources.h"
#include "Ctfscan.h"
#include "SymTab.h"
#define BUFSIZE 256 /* size of read buffer */
class File
{
public:
char* readTrans(int trans);
ITEMSET* read(long i);
int reset();
bool loaded() const;
int items();
hash_map<int, int> classcount;
hash_map<std::string, int, hashString, eqstr> classes; /*
Transaction-class
mapping */
ITEMSET* read();
SymTab& symbols();
void insert_iset(ITEMSET is){isets.push_back(is);};
bool load_iset(int i, ITEMSET &trans);
const char* lookup(int id) const;
const char* looktr(int id) const;
int reorg(long thresh);
int size() const;
bool read(ITEMSET& s); /* read an itemset from buffer */
File(const char* fname, /* file name */
bool load = true /* flag for loading item sets */
);
virtual ~File();
protected:
FILE* file; /* file pointer */
SymTab nimap; /* name/identifier map */
SymTab trmap; /* Transaction map */
private:
virtual void read_blk() = 0; /* caches the dataset into memory */
virtual int get_item () = 0; /* --- read an item */
virtual int get_iset () = 0; /* --- read an item set */
// void prepare (); /* --- sort set and remove duplicates */
protected:
std::vector<ITEMSET> isets; /* item set vector */
int isetsz; /* number of itemsets */
ITEMSET cis; /* current item set */
int reccnt; /* number of records read */
bool load; /* flag for file-buffering */
int maxcnt; /* maximal number of items per set */
int curr; /* current record read */
int pos; /* starting reading position in file */
char* fn_in; /* file name */
};
inline int File::items()
{
return nimap.size();
}
inline bool File::loaded()
const{
return load;
}
inline int File::size() const{ /* get the number of itemset */
return isetsz; /* currently loaded */
}
inline const char* File::lookup(int id) /* get the name of a given
identifier*/
const{
return nimap.lookup(id);
}
inline const char* File::looktr(int id) /* get the name of a given
transaction*/
const{
return trmap.lookup(id);
}
inline SymTab& File::symbols() /* gets the symbol table */
{
return nimap;
}
#endif
--------------------------------------
I have a problem at runtime in
File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}
Segmentation fault when std::fclose(file) is executed, I don't
understand why...
Here some details:
----------
(gdb) #0 0x00000000 in ?? ()
(gdb) #1 0x400e710d in fclose@@GLIBC_2.1 () from /lib/libc.so.6
(gdb) #2 0x0804b86a in File::~File (this=0x8072c18, __in_chrg=0) at
.../File.cpp:26
(gdb) #3 0x0805bca2 in AsciiFile::~AsciiFile (this=0x8072c18,
__in_chrg=3)
(gdb) at ../AsciiFile.h:19
----------------------------------------
I remember to you that it is a Linux porting from a Win32 version, no
problems there.
Thanks to everybody,
V.B.
Italy
here File.cpp and File.h:
File.cpp:
----------------------
#pragma warning (disable: 4786)
#include <cstring>
#include <cstdlib>
#include <cassert>
#include "File.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
File::File(const char* filename, bool ld)
{
fn_in = new char[strlen(filename)+1];
std::strcpy(fn_in,filename);
load = ld;
maxcnt = isetsz = 0;
curr = reccnt = 0;
// bufsid = -1;
}
File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}
bool File::read(ITEMSET& s)
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(!std::fseek(file, pos, SEEK_SET))
error(E_FREAD,fn_in); /* reset file position */
return false; /* return no file read */
}
if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else
get_iset(); /* otherwhise read from file */
curr++; /* and increment the counter */
s = cis; /* return s */
return true;
}
ITEMSET* File::read()
{
if (curr == isetsz) { /* if all records read */
curr = 0; /* reset position of current pointer */
if (!load) /* if to work on input file */
if(std::fseek(file, pos, SEEK_SET) != 0)
error(E_FREAD,fn_in); /* reset file position */
return NULL; /* return no file read */
}
if (load) /* if to work on memory */
cis = isets[curr]; /* get current itemset */
else {
get_iset(); /* otherwhise read from file */
cis.prepare(); /* sort and remove duplicates */
}
curr++; /* and increment the counter */
return &cis; /* return s */
}
int File::reorg(long thresh)
{
SymTab tmap; /* temporary symbol table */
int i, size;
ITEM* item;
std::vector<ITEM*> sorted = nimap.bvec; /* bucket vector */
std::sort(sorted.begin(),sorted.end(),frqcmp()); /* order by
frequency */
for (i = 0, size = sorted.size();
(i < size) && (sorted->frq >= thresh); i++)
{
item = (ITEM*)tmap.insert(nimap.ids[sorted->id], sizeof(ITEM));
if (!item)
return E_NOMEM; /* add the new item to the map, */
item->frq = sorted->frq;
}
nimap = tmap;
return 0;
}
int File::reset()
{ /* resets to the beginning */
if (load) /* if to work on memory */
curr = 0; /* set the current pointer to 0 */
else {
if(std::fseek(file, pos, SEEK_SET) != 0) /* otherwise reset the file
pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
return 0; /* return OK */
}
ITEMSET* File::read(long i)
{ /* read i-th record */
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(fseek(file, locpos, SEEK_SET) != 0) /* reset the file pointer
*/
error(E_FREAD,fn_in); /* checking for I/O errors */
}
else cis = isets;
return &cis; /* return s */
}
else return NULL; /* return no file read */
}
bool File::load_iset(int i, ITEMSET &trans)
{
if (i < isetsz) { /* if current record exist */
if (!load){ /* if to work on input file */
int locpos = ftell(file); /* get the local record position */
if(std::fseek(file, pos, SEEK_SET) != 0) /* set the file pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
long cnt = 0; /* get the local record counter */
while(read()&&(++pos<i)); /* read records in sequence until ith */
if(std::fseek(file, locpos, SEEK_SET) != 0) /* reset the file
pointer */
error(E_FREAD,fn_in); /* checking for I/O errors */
} else
trans = isets;
return true; /* return s */
} else
return false; /* return no file read */
trans=isets;
}
char* File::readTrans(int trans)
{
int l = 0;
int i;
for (i = 0; i < isets[trans].size(); i++) {
l += std::strlen(lookup(isets[trans]->id)) + 1;
}
char* result = new char[l + 1];
strcpy(result,"");
for (i = 0; i < isets[trans].size(); i++) {
std::strcat(result,lookup(isets[trans]->id));
std::strcat(result," ");
}
//fn_in = new char[strlen(filename)+1];
//strcpy(fn_in,filename);
return result;
}
----------------------
Here file.h:
------------------------------------
#ifndef FILE_HPP_INCLUDED
#define FILE_HPP_INCLUDED
#include <vector>
#include "resources.h"
#include "Ctfscan.h"
#include "SymTab.h"
#define BUFSIZE 256 /* size of read buffer */
class File
{
public:
char* readTrans(int trans);
ITEMSET* read(long i);
int reset();
bool loaded() const;
int items();
hash_map<int, int> classcount;
hash_map<std::string, int, hashString, eqstr> classes; /*
Transaction-class
mapping */
ITEMSET* read();
SymTab& symbols();
void insert_iset(ITEMSET is){isets.push_back(is);};
bool load_iset(int i, ITEMSET &trans);
const char* lookup(int id) const;
const char* looktr(int id) const;
int reorg(long thresh);
int size() const;
bool read(ITEMSET& s); /* read an itemset from buffer */
File(const char* fname, /* file name */
bool load = true /* flag for loading item sets */
);
virtual ~File();
protected:
FILE* file; /* file pointer */
SymTab nimap; /* name/identifier map */
SymTab trmap; /* Transaction map */
private:
virtual void read_blk() = 0; /* caches the dataset into memory */
virtual int get_item () = 0; /* --- read an item */
virtual int get_iset () = 0; /* --- read an item set */
// void prepare (); /* --- sort set and remove duplicates */
protected:
std::vector<ITEMSET> isets; /* item set vector */
int isetsz; /* number of itemsets */
ITEMSET cis; /* current item set */
int reccnt; /* number of records read */
bool load; /* flag for file-buffering */
int maxcnt; /* maximal number of items per set */
int curr; /* current record read */
int pos; /* starting reading position in file */
char* fn_in; /* file name */
};
inline int File::items()
{
return nimap.size();
}
inline bool File::loaded()
const{
return load;
}
inline int File::size() const{ /* get the number of itemset */
return isetsz; /* currently loaded */
}
inline const char* File::lookup(int id) /* get the name of a given
identifier*/
const{
return nimap.lookup(id);
}
inline const char* File::looktr(int id) /* get the name of a given
transaction*/
const{
return trmap.lookup(id);
}
inline SymTab& File::symbols() /* gets the symbol table */
{
return nimap;
}
#endif
--------------------------------------
I have a problem at runtime in
File::~File()
{
if (file)
std::fclose(file);
if (fn_in)
delete[] fn_in;
}
Segmentation fault when std::fclose(file) is executed, I don't
understand why...
Here some details:
----------
(gdb) #0 0x00000000 in ?? ()
(gdb) #1 0x400e710d in fclose@@GLIBC_2.1 () from /lib/libc.so.6
(gdb) #2 0x0804b86a in File::~File (this=0x8072c18, __in_chrg=0) at
.../File.cpp:26
(gdb) #3 0x0805bca2 in AsciiFile::~AsciiFile (this=0x8072c18,
__in_chrg=3)
(gdb) at ../AsciiFile.h:19
----------------------------------------
I remember to you that it is a Linux porting from a Win32 version, no
problems there.
Thanks to everybody,
V.B.
Italy