34 this->freebuffer = freeBuffer;
47 this->freebuffer = freeBuffer;
52 void mcs::Buffer::set(
void* extbuf,
unsigned int size,
59 this->freebuffer = freeBuffer;
85 throw MCS_FATAL(MSG_NOT_ENOUGH_SPACE, size, bufsize);
89 buf = (
char*) malloc(size);
92 else if (bufsize < size) {
93 buf = (
char*) realloc(buf, size);
121 if (wstart == (
unsigned int) -1) {
122 unsigned int tt = bufsize;
123 resize(bufsize + wlen);
125 memcpy(p, extbuf, wlen);
128 resize(wstart + wlen);
129 memcpy(buf + wstart, extbuf, wlen);
134 throw MCS_ERROR( MSG_NO_WINDOWS_SELECTED );
143 if (wstart == (
unsigned int) -1)
146 stream.read(buf + wstart, wlen);
151 while (! stream.eof()) {
152 stream.read(tmp, 2048);
153 int n = stream.gcount();
168 if (wstart == (
unsigned int) -1)
171 memcpy(extbuf, buf + wstart, wlen);
175 memcpy(extbuf, buf, bufsize);
185 if (wstart == (
unsigned int) -1)
188 stream.write(buf + wstart, wlen);
192 stream.write(buf, bufsize);
202 throw MCS_ERROR( MSG_INVALID_POSITION, pos );
242 char* lbuf = (
char*) llbuf;
243 unsigned short int count;
247 memcpy(&count, lbuf,
sizeof(
unsigned short int));
248 lbuf +=
sizeof(
unsigned short int);
250 for (i=0; i<
count; i++) {
262 for (
int i=0; i<from.
count(); i++)
272 for (
int i=0; i<from.
count(); i++)
283 unsigned int size = 0;
286 size +=
sizeof(
unsigned short int);
288 for (i=0; i<
count(); i++) {
289 size +=
array[i].objSize();
301 unsigned short int lcount =
count(), i;
305 abuf(
sizeof(lcount)) << &lcount;
306 for (i=0; i< lcount; i++) {
307 void* tmp =
array[i].getEntireBuffer(chunk);
350 void mcs::Record::clear()
361 return array.count();
405 Data mcs::Record::pop(
int x)
414 MCS_CRITICAL_SECTION_END_RETURN(d);
425 MCS_CRITICAL_SECTION_END_RETURN(d);
435 MCS_CRITICAL_SECTION_END_RETURN(d);
462 for (i=0; i<
count(); i++)
463 if (
array[i].name() == name) {
472 throw MCS_ERROR(MSG_MISSING_FIELD, name.csz);
487 for (ui=0; ui< (
unsigned int)
array.count(); ui++)
492 vector<string> vmap =
split(s);
494 for (ui=0; ui<vmap.size(); ui++)
510 for (
int i=0; i<
count(); i++)
511 operator[](i).emptyName();
518 for (
int i=0; i<
count(); i++)
519 operator[](i).setNull();
528 for (
int i=0; i<
count(); i++) {
538 string mcs::Record::asStringNames(
string sep)
542 for (
int i=0; i<
count(); i++) {
553 string mcs::Record::asStringTypes(
string sep)
557 for (
int i=0; i<
count(); i++) {
578 lmetarec(false), rs(false)
609 (*rec)[i].emptyName();
617 void mcs::RecordSet::clear()
639 Record* meta,
short int id)
675 throw MCS_ERROR( MSG_INSERT_NOT_ALLOWED );
685 throw MCS_ERROR( MSG_INSERT_NOT_ALLOWED );
694 if ((
lfetch) && (
rs.count() == 0)) {
724 if (
lpos != newpos) {
734 leof = !
fetch(newpos, ( newpos == (
lpos+1) ?
false :
true) );
745 throw MCS_ERROR(MSG_METHOD_MUST_BE_OVERLOADED,
"RecordSet::fetch");
753 bool mcs::RecordSet::know_nRows() {
return lknow_nrows; }
755 unsigned int mcs::RecordSet::nRows() {
775 unsigned int mcs::RecordSet::pos() {
return lpos; }
777 bool mcs::RecordSet::eof() {
return leof; }
779 bool mcs::RecordSet::alwaysSameStructure() {
return lusemetarec; }
782 Record& mcs::RecordSet::metarec()
791 Record& RecordSet::rec() {
793 throw MCS_ERROR( MSG_ALREADY_AT_END_OF_FILE );
811 throw MCS_ERROR( MSG_ALREADY_AT_END_OF_FILE );
820 throw MCS_ERROR( MSG_NOT_A_RANDOM_ACCESS_RECORDSET,
"setPrev()" );
823 throw MCS_ERROR( MSG_ALREADY_AT_BEGIN_OF_FILE );
832 throw MCS_ERROR( MSG_NOT_A_RANDOM_ACCESS_RECORDSET,
"setFirst()" );
841 throw MCS_ERROR( MSG_NOT_A_RANDOM_ACCESS_RECORDSET,
"setLast()" );
853 throw MCS_ERROR( MSG_NOT_A_RANDOM_ACCESS_RECORDSET,
"setPos()" );
868 throw MCS_ERROR(MSG_INVALID_POSITION, field);
873 if (! alwaysSameStructure())
874 throw MCS_ERROR( MSG_NO_SAME_META_INFO );
882 if (rec()[field].sval() == equalTo) {
897 throw MCS_ERROR(MSG_INVALID_POSITION, field);
902 if (! alwaysSameStructure())
903 throw MCS_ERROR( MSG_NO_SAME_META_INFO );
911 if (rec()[field].ival() == equalTo) {
924 void mcs::RecordSet::dump(
string fn) {
931 Record* mcs::RecordSet::prepRecToSend()
934 long int nrows = (
long int)
lnrows;
virtual bool fetch(unsigned int newpos, bool random)
The virtual method to fetch new records.
#define MCS_RS_INSERT
Flag for RecordSet::init().
bool lusemetarec
If all records share the same meta structure.
#define MCS_RS_ACCUM
Flag for RecordSet::init().
void free()
Free allocated buffer.
int posWhoseNameIs(string name, enum ThrowExceptions throwexc=THROW)
Return the index of the first Data objects whose name is "name".
bool setPrev()
Set the record pointer to the previous position in the set.
short int lid
General purpose identificator.
#define MCS_RS_USEMETAREC
Flag for RecordSet::init().
Serialize memory buffers or files into chunks.
void setFieldMap(string s="")
Creates a new field map from the given string.
unsigned char code
Flags used in the init() method.
A simple class to implement "critical sections".
Buffer & operator>>(void *extbuf)
Copy data into an external buffer.
string smap
String representation of the map.
unsigned int size()
Return size of the buffer.
string sval(bool addWhiteSpaces=false) const
Convert internal data to a string object.
unsigned int objSize()
Return how many bytes require the object to be serialized.
Buffer & operator<<(const void *extbuf)
Copy data from an external buffer.
void setTag(unsigned char tag)
Set a new value to internal tag.
bool setLast()
Set the record pointer to the last position.
bool setPos(unsigned int i)
Set the record pointer to the i-th position.
bool linsert
If true the insert mechanism will be used.
BufferFreeOnDestroy
Values to be used in Buffer class constructor.
char * operator[](unsigned int pos)
Use the Buffer class an an array of pointers to char.
void synchronize(bool setactive)
Enable or disable the synchronization feature.
bool lknow_nrows
If the number of records is known.
Buffer(enum BufferFreeOnDestroy freeBuffer=AUTO_FREE)
Constructor, buffer allocation is handled internally.
bool serialize_buffer(char *&from, unsigned int &size)
Prepare a buffer with all informations contained in the object.
unsigned int current
Internal position in the Dynamic_Array object.
string name()
Return the name of the object.
A dynamic array of Data objects.
char * buf
Pointer to temporary buffer containing the next chunk.
bool setFirst()
Set the record pointer to the first position.
bool laccum
If all records has been fetched during the init() call.
BASE pop(int pos=0)
Extracts an element from the array.
virtual ~RecordSet()
Destructor.
void push(BASE *d)
Push an existing element at the end of the array.
void setNull()
Set all Data object's name to null values.
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Data & operator[](string name)
char * from
Pointer to source buffer, if type = MCS_SERIAL_BUFFER.
bool leof
If the current position (lpos) is at the end of the set.
string Types2Str(Types type, bool isunsigned)
Return the name of the type given in "type" and "isunsigned".
string asString(string sep="\)
Return record content as a unique string.
bool internal_fetch(unsigned int newpos)
Fetch a new Record object in the set.
unsigned int size()
If knowSize() is true, return the size of the entire block of data.
Data field(string name)
Returns the Data object in the vector whose name is "name".
int nFields()
Returns number of fields.
void resize(unsigned int size)
Check size, and eventually enlarge allocated memory.
int type
Type of data to be sent.
virtual void hk_dump(string fn)
Virtual method to provide dump facility to a file.
void startFetch()
Must be called when record fetching is possible.
Record & operator=(Record &from)
Assignment operator, will copy all data objects.
vector< string > split(string s, string sep=" ")
Split a string into tokens.
void insert(Record *rec)
Insert a new record in the set.
#define MCS_CRITICAL_SECTION_END
End a critical section, there must be no "return" or "goto" instruction inside the section...
Main include file for all MCS based applications.
void setFieldMap(string s="")
Set up a field map for each record in the recordset.
bool setWhere(int i, string equalTo)
Set the record pointer to the position which satisfies a string condition.
RecordSet()
Build an empty record set.
void addField(Data *d)
Wrapper around Dynamic_Array.push.
#define MCS_RS_KNOW_NROWS
Flag for RecordSet::init().
void emptyName()
Set all Data object's name to an empty string.
Record(Record &from)
Copy constructor, will copy all data objects.
void init(unsigned char code, unsigned int nrows=0, Record *meta=NULL, short int id=0)
Initailize the Record set.
A general purpose data type.
#define MCS_RS_RANDOM
Flag for RecordSet::init().
Dynamic_Array< Record > rs
Internal Record set.
void clear()
Empty the array.
virtual Record * newRecord()
Creates a new Record object in the heap.
ThrowExceptions
Values to be used with throwexc parameters.
bool lrandom
If random access (setFirst, setLast, setPos) is allowed.
~Buffer()
Destructor, will free the buffer if "freeBuffer" is true.
string trim(string s)
Remove any leading or trailing blanks.
int count()
Wrapper around Dynamic_Array.count.
#define MCS_FATAL(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
bool setNext()
Set the record pointer to the next position.
#define MCS_CRITICAL_SECTION_BEGIN
Start a critical section, there must be no "return" or "goto" instruction inside the section...
Record lmetarec
Record of meta structure, to be used only if lusemetarec is true.
unsigned int lnrows
Number of records, to be used only if lknow_nrows is true.
Dynamic_Array< int > lmap
Array of mapped fields.
int count()
Returns number of elements in the array.
Buffer & operator()(unsigned int start, unsigned int len)
Select a "window" in the buffer.
Dynamic_Array< Data > array
Array of fields.
unsigned int objSize()
Return how many bytes require the object to be serialized.
unsigned int lpos
Current position in the recordset.
Namespace for MCS library.
bool lfetch
If true the fetch mechanism will be used.