Subclassing Example
Here is an example that defines a DirectoryTable class. This class manages files in a directory. Within this example, the member functions that are used to notify the Data Access library when certain events occur appear in bold type.
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <ilviews/dataccess/table.h>
 
class DirectoryTable : public IliTable {
public:
enum ColumnTags { FileName = 0 };
DirectoryTable(IlvDisplay* dpy, const char* directory)
: IliTable(dpy)
{
_rowsCount = 0;
_files = NULL;
_directory = dupString(directory);
insertColumn(FileName, “FileName”, IliStringType);
}
~DirectoryTable() {
tidy();
delete [] _directory;
}
void clearRows() {
tidy();
allRowsDeleted();
tableChanged();
}
IlvBoolean select() {
readDir();
}
IlvBoolean isSelectDone() const {
return _files != NULL;
}
IlvInt getRowsCount() const {
return _rowsCount;
}
IlvBoolean getValue(IlInt rowno, IlvInt colno,
IliValue& value) const {
if (rowno >= 0 && rowno < _rowsCount && colno == 0) {
value = _files[rowno];
return IlTrue;
}
return IlFalse;
}
IlvBoolean updateRow(IlInt rowno,
IliTableBuffer* buf) {
const IliValue& value = buf->at(FileName);
if (rowno >= 0 && rowno < _rowsCount
&& !value.isNull() && value.getType() == IliStringType) {
const char* newname = value.asString();
char oldpath[_POSIX_MAX_PATH];
char newpath[_POSIX_MAX_PATH];
sprintf(oldpath, “%s/%s”, _directory, _files[rowno]);
sprintf(newpath, “%s/%s”, _directory, newname);
if (rename(oldpath, newpath) == 0) {
delete _files[rowno];
_files[rowno] = dupString(newname);
rowChanged(rowno);
return IlTrue;
} else {
 
IliErrorMessage msg;
msg.setApplicationError(strerror(errno));
raiseError(msg);
}
}
return IlFalse;
}
IlvBoolean insertRow(IlInt rowno,
IliTableBuffer* buf) {
IliErrorMessage msg;
msg.setApplicationError(“Insertion not supported in “
“DirectoryTables”);
raiseError(msg);
return IlFalse;
}
IlvBoolean deleteRow (IlInt rowno) {
IliErrorMessage msg;
msg.setApplicationError(“Deletion not supported in “
“DirectoryTables”);
raiseError(msg);
return IlFalse;
}
 
private:
IlInt _rowsCount;
char* _directory;
char** _files;
 
char* dupString(const char* str) const {
char* d = new char[strlen(str) + 1];
strcpy(d, str);
return d;
}
void tidy() {
for (IlInt i = 0; i < _rowsCount; ++i)
delete [] _files[i];
delete [] _files;
_files = NULL;
_rowsCount = 0;
}
IlBoolean readDir() {
DIR *dir = opendir(_directory);
if (dir != NULL) {
tidy();
struct dirent* entry;
while ((entry = readdir(dir)) != NULL)
_rowsCount++;
_files = new char*[_rowsCount];
rewinddir(dir);
_rowsCount = 0;
while ((entry = readdir(dir)) != NULL)
_files[_rowsCount++] = dupString(entry->d_name);
closedir(dir);
tableChanged();
return IlTrue;
}
IliErrorMessage msg;
msg.setApplicationError(strerror(errno));
raiseError(msg);
IlFalse;
}
};
Note: This example applies only to systems where the opendir, readdir, and closedir functions are defined. For other systems, you may have to make some changes in order to call the appropriate functions.
Published date: 05/24/2022
Last modified date: 02/24/2022