2000-02-19 13:36:48 +01:00
|
|
|
/*
|
|
|
|
* tools.c: Various tools
|
|
|
|
*
|
2000-04-24 09:46:05 +02:00
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
2000-02-19 13:36:48 +01:00
|
|
|
* how to reach the author.
|
|
|
|
*
|
2001-02-11 14:53:44 +01:00
|
|
|
* $Id: tools.c 1.30 2001/02/11 14:44:22 kls Exp $
|
2000-02-19 13:36:48 +01:00
|
|
|
*/
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
#define _GNU_SOURCE
|
2000-02-19 13:36:48 +01:00
|
|
|
#include "tools.h"
|
2000-07-23 15:01:31 +02:00
|
|
|
#include <ctype.h>
|
2000-04-15 17:38:11 +02:00
|
|
|
#include <dirent.h>
|
2000-03-11 11:22:37 +01:00
|
|
|
#include <errno.h>
|
2000-09-17 08:23:46 +02:00
|
|
|
#if defined(DEBUG_OSD)
|
|
|
|
#include <ncurses.h>
|
|
|
|
#endif
|
2000-02-19 13:36:48 +01:00
|
|
|
#include <sys/time.h>
|
2000-04-15 17:38:11 +02:00
|
|
|
#include <unistd.h>
|
2000-02-19 13:36:48 +01:00
|
|
|
|
2000-03-11 11:22:37 +01:00
|
|
|
#define MaxBuffer 1000
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
int SysLogLevel = 3;
|
|
|
|
|
|
|
|
void writechar(int filedes, char c)
|
|
|
|
{
|
|
|
|
write(filedes, &c, sizeof(c));
|
|
|
|
}
|
|
|
|
|
2000-03-11 11:22:37 +01:00
|
|
|
char *readline(FILE *f)
|
|
|
|
{
|
|
|
|
static char buffer[MaxBuffer];
|
|
|
|
if (fgets(buffer, sizeof(buffer), f) > 0) {
|
|
|
|
int l = strlen(buffer) - 1;
|
|
|
|
if (l >= 0 && buffer[l] == '\n')
|
|
|
|
buffer[l] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-09-09 14:57:43 +02:00
|
|
|
char *strn0cpy(char *dest, const char *src, size_t n)
|
|
|
|
{
|
|
|
|
char *s = dest;
|
|
|
|
for ( ; --n && (*dest = *src) != 0; dest++, src++) ;
|
|
|
|
*dest = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-07-16 15:02:33 +02:00
|
|
|
char *strreplace(char *s, char c1, char c2)
|
|
|
|
{
|
|
|
|
char *p = s;
|
|
|
|
|
2001-02-11 11:29:22 +01:00
|
|
|
while (p && *p) {
|
2000-07-16 15:02:33 +02:00
|
|
|
if (*p == c1)
|
|
|
|
*p = c2;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
char *skipspace(const char *s)
|
2000-07-23 15:01:31 +02:00
|
|
|
{
|
|
|
|
while (*s && isspace(*s))
|
|
|
|
s++;
|
2000-10-29 13:17:22 +01:00
|
|
|
return (char *)s;
|
|
|
|
}
|
|
|
|
|
2000-11-11 16:38:41 +01:00
|
|
|
char *stripspace(char *s)
|
|
|
|
{
|
|
|
|
if (s && *s) {
|
|
|
|
for (char *p = s + strlen(s) - 1; p >= s; p--) {
|
|
|
|
if (!isspace(*p))
|
|
|
|
break;
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
bool isempty(const char *s)
|
|
|
|
{
|
|
|
|
return !(s && *skipspace(s));
|
2000-07-23 15:01:31 +02:00
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
int time_ms(void)
|
|
|
|
{
|
2000-04-15 17:38:11 +02:00
|
|
|
static time_t t0 = 0;
|
2000-02-19 13:36:48 +01:00
|
|
|
struct timeval t;
|
2000-04-15 17:38:11 +02:00
|
|
|
if (gettimeofday(&t, NULL) == 0) {
|
|
|
|
if (t0 == 0)
|
|
|
|
t0 = t.tv_sec; // this avoids an overflow (we only work with deltas)
|
|
|
|
return (t.tv_sec - t0) * 1000 + t.tv_usec / 1000;
|
|
|
|
}
|
2000-02-19 13:36:48 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
void delay_ms(int ms)
|
|
|
|
{
|
|
|
|
int t0 = time_ms();
|
|
|
|
while (time_ms() - t0 < ms)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2000-07-23 15:01:31 +02:00
|
|
|
bool isnumber(const char *s)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if (!isdigit(*s))
|
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2000-09-15 15:09:15 +02:00
|
|
|
const char *AddDirectory(const char *DirName, const char *FileName)
|
|
|
|
{
|
|
|
|
static char *buf = NULL;
|
|
|
|
delete buf;
|
|
|
|
asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2001-01-13 12:17:30 +01:00
|
|
|
#define DFCMD "df -m '%s'"
|
2000-07-29 15:21:42 +02:00
|
|
|
|
|
|
|
uint FreeDiskSpaceMB(const char *Directory)
|
|
|
|
{
|
|
|
|
//TODO Find a simpler way to determine the amount of free disk space!
|
|
|
|
uint Free = 0;
|
|
|
|
char *cmd = NULL;
|
|
|
|
asprintf(&cmd, DFCMD, Directory);
|
|
|
|
FILE *p = popen(cmd, "r");
|
|
|
|
if (p) {
|
|
|
|
char *s;
|
|
|
|
while ((s = readline(p)) != NULL) {
|
|
|
|
if (*s == '/') {
|
|
|
|
uint available;
|
|
|
|
sscanf(s, "%*s %*d %*d %u", &available);
|
|
|
|
Free = available;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog(LOG_ERR, "ERROR: can't open pipe for cmd '%s'", cmd);
|
|
|
|
delete cmd;
|
|
|
|
return Free;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirectoryOk(const char *DirName, bool LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
{
|
|
|
|
struct stat ds;
|
|
|
|
if (stat(DirName, &ds) == 0) {
|
|
|
|
if (S_ISDIR(ds.st_mode)) {
|
|
|
|
if (access(DirName, R_OK | W_OK | X_OK) == 0)
|
|
|
|
return true;
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
esyslog(LOG_ERR, "ERROR: can't access %s", DirName);
|
|
|
|
}
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
esyslog(LOG_ERR, "ERROR: %s is not a directory", DirName);
|
|
|
|
}
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
bool MakeDirs(const char *FileName, bool IsDirectory)
|
2000-03-11 11:22:37 +01:00
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
char *s = strdup(FileName);
|
|
|
|
char *p = s;
|
|
|
|
if (*p == '/')
|
|
|
|
p++;
|
2000-04-15 17:38:11 +02:00
|
|
|
while ((p = strchr(p, '/')) != NULL || IsDirectory) {
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
2000-03-11 11:22:37 +01:00
|
|
|
struct stat fs;
|
|
|
|
if (stat(s, &fs) != 0 || !S_ISDIR(fs.st_mode)) {
|
2000-07-29 15:21:42 +02:00
|
|
|
dsyslog(LOG_INFO, "creating directory %s", s);
|
2000-03-11 11:22:37 +01:00
|
|
|
if (mkdir(s, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) {
|
2000-12-08 16:23:32 +01:00
|
|
|
LOG_ERROR_STR(s);
|
2000-03-11 11:22:37 +01:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-04-15 17:38:11 +02:00
|
|
|
if (p)
|
|
|
|
*p++ = '/';
|
|
|
|
else
|
|
|
|
break;
|
2000-03-11 11:22:37 +01:00
|
|
|
}
|
|
|
|
delete s;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-07-29 15:21:42 +02:00
|
|
|
bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
|
2000-04-15 17:38:11 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(FileName, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
DIR *d = opendir(FileName);
|
|
|
|
if (d) {
|
|
|
|
struct dirent *e;
|
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
|
|
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s/%s", FileName, e->d_name);
|
2000-07-29 15:21:42 +02:00
|
|
|
if (FollowSymlinks) {
|
|
|
|
int size = strlen(buffer) * 2; // should be large enough
|
|
|
|
char *l = new char[size];
|
|
|
|
int n = readlink(buffer, l, size);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EINVAL)
|
|
|
|
LOG_ERROR_STR(buffer);
|
|
|
|
}
|
|
|
|
else if (n < size) {
|
|
|
|
l[n] = 0;
|
|
|
|
dsyslog(LOG_INFO, "removing %s", l);
|
|
|
|
if (remove(l) < 0)
|
|
|
|
LOG_ERROR_STR(l);
|
|
|
|
}
|
|
|
|
else
|
2000-07-29 19:03:09 +02:00
|
|
|
esyslog(LOG_ERR, "ERROR: symlink name length (%d) exceeded anticipated buffer size (%d)", n, size);
|
2000-07-29 15:21:42 +02:00
|
|
|
delete l;
|
|
|
|
}
|
|
|
|
dsyslog(LOG_INFO, "removing %s", buffer);
|
2000-04-15 17:38:11 +02:00
|
|
|
if (remove(buffer) < 0)
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(buffer);
|
2000-04-15 17:38:11 +02:00
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
else {
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(FileName);
|
2000-04-15 17:38:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2000-07-29 15:21:42 +02:00
|
|
|
dsyslog(LOG_INFO, "removing %s", FileName);
|
2001-02-04 12:36:32 +01:00
|
|
|
if (remove(FileName) < 0) {
|
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
return false;
|
|
|
|
}
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
2000-12-28 12:57:16 +01:00
|
|
|
else if (errno != ENOENT) {
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(FileName);
|
2000-12-28 12:57:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
|
|
|
|
2001-02-11 14:53:44 +01:00
|
|
|
bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
|
|
|
|
{
|
|
|
|
DIR *d = opendir(DirName);
|
|
|
|
if (d) {
|
|
|
|
bool empty = true;
|
|
|
|
struct dirent *e;
|
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
|
|
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s/%s", DirName, e->d_name);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(buffer, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (!RemoveEmptyDirectories(buffer, true))
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR_STR(buffer);
|
|
|
|
delete buffer;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
if (RemoveThis && empty) {
|
|
|
|
dsyslog(LOG_INFO, "removing %s", DirName);
|
|
|
|
if (remove(DirName) < 0) {
|
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-01-13 15:36:31 +01:00
|
|
|
char *ReadLink(const char *FileName)
|
|
|
|
{
|
|
|
|
char RealName[_POSIX_PATH_MAX];
|
|
|
|
const char *TargetName = NULL;
|
|
|
|
int n = readlink(FileName, RealName, sizeof(RealName) - 1);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno == ENOENT || errno == EINVAL) // file doesn't exist or is not a symlink
|
|
|
|
TargetName = FileName;
|
|
|
|
else { // some other error occurred
|
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (n < int(sizeof(RealName))) { // got it!
|
|
|
|
RealName[n] = 0;
|
|
|
|
TargetName = RealName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog(LOG_ERR, "ERROR: symlink's target name too long: %s", FileName);
|
|
|
|
return TargetName ? strdup(TargetName) : NULL;
|
|
|
|
}
|
|
|
|
|
2000-09-17 08:23:46 +02:00
|
|
|
// --- cFile -----------------------------------------------------------------
|
|
|
|
|
|
|
|
bool cFile::files[FD_SETSIZE] = { false };
|
|
|
|
int cFile::maxFiles = 0;
|
|
|
|
|
|
|
|
cFile::cFile(void)
|
|
|
|
{
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cFile::~cFile()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Open(const char *FileName, int Flags, mode_t Mode)
|
|
|
|
{
|
|
|
|
if (!IsOpen())
|
|
|
|
return Open(open(FileName, Flags, Mode));
|
|
|
|
esyslog(LOG_ERR, "ERROR: attempt to re-open %s", FileName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Open(int FileDes)
|
|
|
|
{
|
|
|
|
if (FileDes >= 0) {
|
|
|
|
if (!IsOpen()) {
|
|
|
|
f = FileDes;
|
|
|
|
if (f >= 0) {
|
|
|
|
if (f < FD_SETSIZE) {
|
|
|
|
if (f >= maxFiles)
|
|
|
|
maxFiles = f + 1;
|
|
|
|
if (!files[f])
|
|
|
|
files[f] = true;
|
|
|
|
else
|
|
|
|
esyslog(LOG_ERR, "ERROR: file descriptor %d already in files[]", f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog(LOG_ERR, "ERROR: file descriptor %d is larger than FD_SETSIZE (%d)", f, FD_SETSIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog(LOG_ERR, "ERROR: attempt to re-open file descriptor %d", FileDes);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cFile::Close(void)
|
|
|
|
{
|
|
|
|
if (f >= 0) {
|
|
|
|
close(f);
|
|
|
|
files[f] = false;
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cFile::ReadString(char *Buffer, int Size)
|
|
|
|
{
|
|
|
|
int rbytes = 0;
|
|
|
|
bool wait = true;
|
|
|
|
|
|
|
|
while (Ready(wait)) {
|
2000-09-19 17:55:09 +02:00
|
|
|
int n = read(f, Buffer + rbytes, 1);
|
2000-09-17 08:23:46 +02:00
|
|
|
if (n == 0)
|
|
|
|
break; // EOF
|
|
|
|
if (n < 0) {
|
|
|
|
LOG_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rbytes += n;
|
2000-09-19 17:55:09 +02:00
|
|
|
if (rbytes == Size || Buffer[rbytes - 1] == '\n')
|
2000-09-17 08:23:46 +02:00
|
|
|
break;
|
|
|
|
wait = false;
|
|
|
|
}
|
|
|
|
return rbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Ready(bool Wait)
|
|
|
|
{
|
|
|
|
return f >= 0 && AnyFileReady(f, Wait ? 1000 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::AnyFileReady(int FileDes, int TimeoutMs)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_OSD
|
|
|
|
refresh();
|
|
|
|
#endif
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
for (int i = 0; i < maxFiles; i++) {
|
|
|
|
if (files[i])
|
|
|
|
FD_SET(i, &set);
|
|
|
|
}
|
|
|
|
if (0 <= FileDes && FileDes < FD_SETSIZE && !files[FileDes])
|
|
|
|
FD_SET(FileDes, &set); // in case we come in with an arbitrary descriptor
|
2000-09-17 15:36:33 +02:00
|
|
|
if (TimeoutMs == 0)
|
|
|
|
TimeoutMs = 10; // load gets too heavy with 0
|
2000-09-17 08:23:46 +02:00
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = TimeoutMs / 1000;
|
|
|
|
timeout.tv_usec = (TimeoutMs % 1000) * 1000;
|
|
|
|
return select(FD_SETSIZE, &set, NULL, NULL, &timeout) > 0 && (FileDes < 0 || FD_ISSET(FileDes, &set));
|
|
|
|
}
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
bool cFile::FileReady(int FileDes, int TimeoutMs)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_OSD
|
|
|
|
refresh();
|
|
|
|
#endif
|
|
|
|
fd_set set;
|
|
|
|
struct timeval timeout;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(FileDes, &set);
|
|
|
|
if (TimeoutMs < 100)
|
|
|
|
TimeoutMs = 100;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = TimeoutMs * 1000;
|
|
|
|
return select(FD_SETSIZE, &set, NULL, NULL, &timeout) > 0 && FD_ISSET(FileDes, &set);
|
|
|
|
}
|
|
|
|
|
2001-01-13 15:36:31 +01:00
|
|
|
// --- cSafeFile -------------------------------------------------------------
|
|
|
|
|
|
|
|
cSafeFile::cSafeFile(const char *FileName)
|
|
|
|
{
|
|
|
|
f = NULL;
|
|
|
|
fileName = ReadLink(FileName);
|
|
|
|
tempName = fileName ? new char[strlen(fileName) + 5] : NULL;
|
|
|
|
if (tempName)
|
|
|
|
strcat(strcpy(tempName, fileName), ".$$$");
|
|
|
|
}
|
|
|
|
|
|
|
|
cSafeFile::~cSafeFile()
|
|
|
|
{
|
|
|
|
if (f)
|
|
|
|
fclose(f);
|
|
|
|
unlink(tempName);
|
|
|
|
delete fileName;
|
|
|
|
delete tempName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSafeFile::Open(void)
|
|
|
|
{
|
|
|
|
if (!f && fileName && tempName) {
|
|
|
|
f = fopen(tempName, "w");
|
|
|
|
if (!f)
|
|
|
|
LOG_ERROR_STR(tempName);
|
|
|
|
}
|
|
|
|
return f != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSafeFile::Close(void)
|
|
|
|
{
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
rename(tempName, fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
// --- cListObject -----------------------------------------------------------
|
|
|
|
|
|
|
|
cListObject::cListObject(void)
|
|
|
|
{
|
|
|
|
prev = next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cListObject::~cListObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListObject::Append(cListObject *Object)
|
|
|
|
{
|
|
|
|
next = Object;
|
|
|
|
Object->prev = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListObject::Unlink(void)
|
|
|
|
{
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
2000-03-11 11:22:37 +01:00
|
|
|
next = prev = NULL;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int cListObject::Index(void)
|
|
|
|
{
|
|
|
|
cListObject *p = prev;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
i++;
|
|
|
|
p = p->prev;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cListBase -------------------------------------------------------------
|
|
|
|
|
|
|
|
cListBase::cListBase(void)
|
|
|
|
{
|
|
|
|
objects = lastObject = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cListBase::~cListBase()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
while (objects) {
|
|
|
|
cListObject *object = objects->Next();
|
|
|
|
delete objects;
|
|
|
|
objects = object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Add(cListObject *Object)
|
|
|
|
{
|
|
|
|
if (lastObject)
|
|
|
|
lastObject->Append(Object);
|
|
|
|
else
|
|
|
|
objects = Object;
|
|
|
|
lastObject = Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Del(cListObject *Object)
|
|
|
|
{
|
|
|
|
if (Object == objects)
|
|
|
|
objects = Object->Next();
|
|
|
|
if (Object == lastObject)
|
|
|
|
lastObject = Object->Prev();
|
|
|
|
Object->Unlink();
|
|
|
|
delete Object;
|
|
|
|
}
|
|
|
|
|
2000-03-11 11:22:37 +01:00
|
|
|
void cListBase::Move(int From, int To)
|
|
|
|
{
|
|
|
|
Move(Get(From), Get(To));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Move(cListObject *From, cListObject *To)
|
|
|
|
{
|
|
|
|
if (From && To) {
|
|
|
|
if (From->Index() < To->Index())
|
|
|
|
To = To->Next();
|
|
|
|
if (From == objects)
|
|
|
|
objects = From->Next();
|
|
|
|
if (From == lastObject)
|
|
|
|
lastObject = From->Prev();
|
|
|
|
From->Unlink();
|
|
|
|
if (To) {
|
|
|
|
if (To->Prev())
|
|
|
|
To->Prev()->Append(From);
|
|
|
|
From->Append(To);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lastObject->Append(From);
|
|
|
|
if (!From->Prev())
|
|
|
|
objects = From;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
void cListBase::Clear(void)
|
|
|
|
{
|
|
|
|
while (objects) {
|
|
|
|
cListObject *object = objects->Next();
|
|
|
|
delete objects;
|
|
|
|
objects = object;
|
|
|
|
}
|
|
|
|
objects = lastObject = NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
cListObject *cListBase::Get(int Index) const
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
2000-09-09 14:57:43 +02:00
|
|
|
if (Index < 0)
|
|
|
|
return NULL;
|
2000-02-19 13:36:48 +01:00
|
|
|
cListObject *object = objects;
|
|
|
|
while (object && Index-- > 0)
|
|
|
|
object = object->Next();
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
int cListBase::Count(void) const
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
cListObject *object = objects;
|
|
|
|
|
|
|
|
while (object) {
|
|
|
|
n++;
|
|
|
|
object = object->Next();
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|