- Adapted indenation to new agreed upon system
- Fixed bad status in poldi zug driver
This commit is contained in:
182
arrobj.c
182
arrobj.c
@ -36,8 +36,9 @@ typedef struct WrtObjContext {
|
||||
char filename[PATH_MAX];
|
||||
} WrtObjContext;
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int WrtObjOpen(WrtObjContext *ctx, char *fileName) {
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int WrtObjOpen(WrtObjContext * ctx, char *fileName)
|
||||
{
|
||||
|
||||
int iret;
|
||||
char *slashpos;
|
||||
@ -49,17 +50,20 @@ int WrtObjOpen(WrtObjContext *ctx, char *fileName) {
|
||||
} else {
|
||||
ctx->pos = slashpos - fileName + 1;
|
||||
}
|
||||
iret = snprintf(ctx->filename, sizeof(ctx->filename), "%.*s.%s"
|
||||
, ctx->pos, fileName, fileName + ctx->pos);
|
||||
iret =
|
||||
snprintf(ctx->filename, sizeof(ctx->filename), "%.*s.%s", ctx->pos,
|
||||
fileName, fileName + ctx->pos);
|
||||
if (iret < 0 || iret >= sizeof(ctx->filename)) {
|
||||
return 0;
|
||||
}
|
||||
remove(ctx->filename); /* remove already existing temporary file */
|
||||
ctx->file = fopen(ctx->filename,"w");
|
||||
}
|
||||
remove(ctx->filename); /* remove already existing temporary file */
|
||||
ctx->file = fopen(ctx->filename, "w");
|
||||
return ctx->file != NULL;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void WrtObj(WrtObjContext *ctx, char *objectName) {
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void WrtObj(WrtObjContext * ctx, char *objectName)
|
||||
{
|
||||
CommandList *pCom = NULL;
|
||||
Dummy *pDum = NULL;
|
||||
|
||||
@ -70,26 +74,30 @@ void WrtObj(WrtObjContext *ctx, char *objectName) {
|
||||
}
|
||||
pDum = pCom->pData;
|
||||
if (pDum != NULL) {
|
||||
pDum->pDescriptor->SaveStatus(pCom->pData,pCom->pName,ctx->file);
|
||||
pDum->pDescriptor->SaveStatus(pCom->pData, pCom->pName, ctx->file);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void WrtObjClose(WrtObjContext *ctx) {
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void WrtObjClose(WrtObjContext * ctx)
|
||||
{
|
||||
char finalName[PATH_MAX];
|
||||
|
||||
if (ctx) {
|
||||
fclose(ctx->file);
|
||||
snprintf(finalName, sizeof finalName, "%.*s%s"
|
||||
, ctx->pos, ctx->filename, ctx->filename + ctx->pos + 1);
|
||||
snprintf(finalName, sizeof finalName, "%.*s%s", ctx->pos,
|
||||
ctx->filename, ctx->filename + ctx->pos + 1);
|
||||
rename(ctx->filename, finalName);
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int ArrayExists(void *object, void *arg, int argc, char *argv[]) {
|
||||
static int ArrayExists(void *object, void *arg, int argc, char *argv[])
|
||||
{
|
||||
ArrayObj *arr = ParCast(&arrayObjClass, object);
|
||||
ArrayItem *item;
|
||||
|
||||
|
||||
assert(arr);
|
||||
if (argc == 1) {
|
||||
for (item = arr->items; item != NULL; item = item->next) {
|
||||
@ -102,8 +110,10 @@ static int ArrayExists(void *object, void *arg, int argc, char *argv[]) {
|
||||
ParPrintf(object, eValue, "0");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int ArrayItems(void *object, void *arg, int argc, char *argv[]) {
|
||||
static int ArrayItems(void *object, void *arg, int argc, char *argv[])
|
||||
{
|
||||
ArrayObj *arr = ParCast(&arrayObjClass, object);
|
||||
ArrayItem *item;
|
||||
int l;
|
||||
@ -112,35 +122,43 @@ static int ArrayItems(void *object, void *arg, int argc, char *argv[]) {
|
||||
assert(arr);
|
||||
l = 1;
|
||||
for (item = arr->items; item != NULL; item = item->next) {
|
||||
l += strlen(item->name); l++;
|
||||
l += strlen(item->name);
|
||||
l++;
|
||||
}
|
||||
result = calloc(l, 1);
|
||||
if (result) {
|
||||
p = result;
|
||||
for (item = arr->items; item != NULL; item = item->next) {
|
||||
strcpy(p, item->name); p+=strlen(item->name);
|
||||
*p = ' '; p++;
|
||||
strcpy(p, item->name);
|
||||
p += strlen(item->name);
|
||||
*p = ' ';
|
||||
p++;
|
||||
}
|
||||
if (p > result) p--;
|
||||
*p='\0';
|
||||
if (p > result)
|
||||
p--;
|
||||
*p = '\0';
|
||||
ParPrintf(object, eValue, result);
|
||||
free(result);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int ArrayMakeItem(void *object, void *delete, int argc, char *argv[]) {
|
||||
static int ArrayMakeItem(void *object, void *delete, int argc,
|
||||
char *argv[])
|
||||
{
|
||||
ArrayObj *arr = ParCast(&arrayObjClass, object);
|
||||
ArrayItem *item, **last;
|
||||
int iarg;
|
||||
|
||||
|
||||
assert(arr);
|
||||
if (argc < 1) goto Usage;
|
||||
if (argc < 1)
|
||||
goto Usage;
|
||||
last = &arr->items;
|
||||
for (item = arr->items; item != NULL; item = item->next) {
|
||||
if (strcasecmp(argv[0], item->name) == 0) {
|
||||
if (strcasecmp(argv[0], item->name) == 0) {
|
||||
if (delete) {
|
||||
*last = item->next; /* remove item from list */
|
||||
*last = item->next; /* remove item from list */
|
||||
item->next = arr->freeItems;
|
||||
arr->freeItems = item;
|
||||
return 1;
|
||||
@ -150,7 +168,8 @@ static int ArrayMakeItem(void *object, void *delete, int argc, char *argv[]) {
|
||||
last = &item->next;
|
||||
}
|
||||
if (delete) {
|
||||
ParPrintf(object, eError, "ERROR: %s.%s not found", arr->p.name, argv[0]);
|
||||
ParPrintf(object, eError, "ERROR: %s.%s not found", arr->p.name,
|
||||
argv[0]);
|
||||
return 0;
|
||||
}
|
||||
if (item == NULL) {
|
||||
@ -158,13 +177,15 @@ static int ArrayMakeItem(void *object, void *delete, int argc, char *argv[]) {
|
||||
item = arr->freeItems;
|
||||
arr->freeItems = item->next;
|
||||
if (item->name) {
|
||||
free(item->name); item->name = NULL;
|
||||
free(item->name);
|
||||
item->name = NULL;
|
||||
}
|
||||
item->next = NULL;
|
||||
*last = item;
|
||||
} else {
|
||||
item = calloc(1, sizeof(ArrayItem));
|
||||
if (item == NULL) return 0;
|
||||
if (item == NULL)
|
||||
return 0;
|
||||
*last = item;
|
||||
item->next = NULL;
|
||||
item->unit = NULL;
|
||||
@ -172,19 +193,21 @@ static int ArrayMakeItem(void *object, void *delete, int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
if (item->unit) {
|
||||
free(item->unit); item->unit=NULL;
|
||||
free(item->unit);
|
||||
item->unit = NULL;
|
||||
}
|
||||
if (item->value) {
|
||||
free(item->value); item->value=NULL;
|
||||
free(item->value);
|
||||
item->value = NULL;
|
||||
}
|
||||
if (item->name != NULL) {
|
||||
if (0 != strcmp(item->name, argv[0])) {
|
||||
free(item->name);
|
||||
item->name = strdup(argv[0]);
|
||||
}
|
||||
} else {
|
||||
item->name = strdup(argv[0]);
|
||||
}
|
||||
if (0 != strcmp(item->name, argv[0])) {
|
||||
free(item->name);
|
||||
item->name = strdup(argv[0]);
|
||||
}
|
||||
} else {
|
||||
item->name = strdup(argv[0]);
|
||||
}
|
||||
iarg = 1;
|
||||
if (iarg < argc) {
|
||||
if (argv[iarg] && argv[iarg][0]) {
|
||||
@ -196,22 +219,27 @@ static int ArrayMakeItem(void *object, void *delete, int argc, char *argv[]) {
|
||||
item->unit = argv[iarg];
|
||||
}
|
||||
iarg++;
|
||||
if (iarg < argc) goto Usage;
|
||||
if (iarg < argc)
|
||||
goto Usage;
|
||||
}
|
||||
}
|
||||
if (item->unit) item->unit = strdup(item->unit);
|
||||
if (item->value) item->value = strdup(item->value);
|
||||
if (item->unit)
|
||||
item->unit = strdup(item->unit);
|
||||
if (item->value)
|
||||
item->value = strdup(item->value);
|
||||
ParInitPar(object, item->name);
|
||||
SCparChange(arr->p.conn);
|
||||
|
||||
return 1;
|
||||
Usage:
|
||||
ParPrintf(object, eError, "Usage: %s makeitem <name> [value] [unit]"
|
||||
, arr->p.name);
|
||||
return 0;
|
||||
Usage:
|
||||
ParPrintf(object, eError, "Usage: %s makeitem <name> [value] [unit]",
|
||||
arr->p.name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void ArrayObjParDef(void *object) {
|
||||
static void ArrayObjParDef(void *object)
|
||||
{
|
||||
ArrayObj *arr = ParCast(&arrayObjClass, object);
|
||||
FILE *saveFile;
|
||||
ArrayItem *item, *next;
|
||||
@ -233,12 +261,15 @@ static void ArrayObjParDef(void *object) {
|
||||
} else {
|
||||
u = "";
|
||||
}
|
||||
fprintf(saveFile, " %s makeitem %s \"%s\" \"%s\"\n", arr->p.name, item->name, item->value, u);
|
||||
fprintf(saveFile, " %s makeitem %s \"%s\" \"%s\"\n", arr->p.name,
|
||||
item->name, item->value, u);
|
||||
if (saveObjects) {
|
||||
WrtObj(&context, item->name);
|
||||
}
|
||||
}
|
||||
ParName(item->name); ParSave(2); ParAccess(usUser);
|
||||
ParName(item->name);
|
||||
ParSave(2);
|
||||
ParAccess(usUser);
|
||||
if (item->unit) {
|
||||
ParTail(item->unit);
|
||||
} else {
|
||||
@ -252,34 +283,53 @@ static void ArrayObjParDef(void *object) {
|
||||
doNotNest = 0;
|
||||
WrtObjClose(&context);
|
||||
}
|
||||
ParName("makeitem"); ParAccess(usUser); ParCmd(ArrayMakeItem, NULL);
|
||||
ParName("deleteitem"); ParAccess(usUser); ParCmd(ArrayMakeItem, "del");
|
||||
ParName("exists"); ParAccess(usSpy); ParCmd(ArrayExists, NULL);
|
||||
ParName("items"); ParAccess(usSpy); ParCmd(ArrayItems, NULL);
|
||||
ParName("saveFile"); ParAccess(usUser); ParStr(&arr->saveFile, "");
|
||||
ParName("makeitem");
|
||||
ParAccess(usUser);
|
||||
ParCmd(ArrayMakeItem, NULL);
|
||||
ParName("deleteitem");
|
||||
ParAccess(usUser);
|
||||
ParCmd(ArrayMakeItem, "del");
|
||||
ParName("exists");
|
||||
ParAccess(usSpy);
|
||||
ParCmd(ArrayExists, NULL);
|
||||
ParName("items");
|
||||
ParAccess(usSpy);
|
||||
ParCmd(ArrayItems, NULL);
|
||||
ParName("saveFile");
|
||||
ParAccess(usUser);
|
||||
ParStr(&arr->saveFile, "");
|
||||
ParStdDef();
|
||||
if (ParActionIs(PAR_KILL)) {
|
||||
for (item = arr->items; item != NULL; item = next) {
|
||||
next = item->next;
|
||||
if (item->name) free(item->name);
|
||||
if (item->unit) free(item->unit);
|
||||
if (item->value) free(item->value);
|
||||
if (item->name)
|
||||
free(item->name);
|
||||
if (item->unit)
|
||||
free(item->unit);
|
||||
if (item->value)
|
||||
free(item->value);
|
||||
free(item);
|
||||
}
|
||||
for (item = arr->freeItems; item != NULL; item = next) {
|
||||
next = item->next;
|
||||
if (item->name) free(item->name);
|
||||
if (item->unit) free(item->unit);
|
||||
if (item->value) free(item->value);
|
||||
if (item->name)
|
||||
free(item->name);
|
||||
if (item->unit)
|
||||
free(item->unit);
|
||||
if (item->value)
|
||||
free(item->value);
|
||||
free(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int ArrayObjInit(SConnection *con, int argc, char *argv[], int dynamic) {
|
||||
static int ArrayObjInit(SConnection * con, int argc, char *argv[],
|
||||
int dynamic)
|
||||
{
|
||||
ArrayObj *arr = NULL;
|
||||
char *creationCmd = NULL;
|
||||
|
||||
|
||||
if (dynamic) {
|
||||
creationCmd = Arg2Tcl(argc, argv, NULL, 0);
|
||||
}
|
||||
@ -288,8 +338,10 @@ static int ArrayObjInit(SConnection *con, int argc, char *argv[], int dynamic) {
|
||||
arr->items = NULL;
|
||||
return arr != NULL;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
void ArrayObjStartup(void) {
|
||||
void ArrayObjStartup(void)
|
||||
{
|
||||
ParMakeClass(&arrayObjClass, NULL);
|
||||
MakeDriver("array", ArrayObjInit, 0, "String Array Object");
|
||||
MakeDriver("array", ArrayObjInit, 0, "String Array Object");
|
||||
}
|
||||
|
Reference in New Issue
Block a user