Added json style output.
r1172 | ffr | 2006-10-25 08:58:02 +1000 (Wed, 25 Oct 2006) | 2 lines
This commit is contained in:
committed by
Douglas Clowes
parent
2ab57e1eb2
commit
37508f2b87
245
sicshipadaba.c
245
sicshipadaba.c
@@ -19,6 +19,13 @@
|
||||
static pHdb root = NULL;
|
||||
static int scriptUpdate = -1;
|
||||
static hdbUpdateTask taskData;
|
||||
enum formatStyle {plain, cli, json};
|
||||
char *formatName[] = {"plain", "cli", "json"};
|
||||
static enum formatStyle currFmtStyle=plain;
|
||||
|
||||
|
||||
pDynString formatClientValue(char *name, hdbValue hVal, int children);
|
||||
pDynString formatJSONValue(char *name, hdbValue hVal, int children);
|
||||
/*=============== common callback functions used for SICS ===========================*/
|
||||
static int SICSCheckPermissionCallback(void *userData, void *callData, pHdb node,
|
||||
hdbValue v){
|
||||
@@ -137,7 +144,17 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||
|
||||
cbInfo = (HdbCBInfo *)userData;
|
||||
pPath = GetHipadabaPath(node);
|
||||
printedData = formatValue(v);
|
||||
switch (currFmtStyle) {
|
||||
case cli:
|
||||
printedData = formatClientValue(pPath,v,0);
|
||||
break;
|
||||
case json:
|
||||
printedData = formatJSONValue(pPath,v,0);
|
||||
break;
|
||||
case plain:
|
||||
default:
|
||||
printedData = formatValue(v);
|
||||
}
|
||||
result = CreateDynString(128,128);
|
||||
if(pPath == NULL || printedData == NULL || result == NULL){
|
||||
SCWriteInContext(cbInfo->pCon,"ERROR: out of memory formatting data" ,
|
||||
@@ -148,9 +165,22 @@ static int SICSNotifyCallback(void *userData, void *callData, pHdb node,
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
DynStringCopy(result,pPath);
|
||||
DynStringConcat(result," = ");
|
||||
DynStringConcat(result,GetCharArray(printedData));
|
||||
switch (currFmtStyle) {
|
||||
case cli:
|
||||
DynStringConcat(result,GetCharArray(printedData));
|
||||
break;
|
||||
case json:
|
||||
DynStringConcat(result,"{");
|
||||
DynStringConcat(result,GetCharArray(printedData));
|
||||
DynStringConcat(result,"}");
|
||||
break;
|
||||
case plain:
|
||||
default:
|
||||
DynStringCopy(result,pPath);
|
||||
DynStringConcat(result," = ");
|
||||
DynStringConcat(result,GetCharArray(printedData));
|
||||
}
|
||||
|
||||
SCWriteInContext(cbInfo->pCon,GetCharArray(result),
|
||||
eEvent,cbInfo->context);
|
||||
free(pPath);
|
||||
@@ -997,6 +1027,125 @@ static int convertHdbType(char *text){
|
||||
static char *hdbTypeToText(int type){
|
||||
return hdbTypes[type+1];
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
pDynString formatJSONValue(char *name, hdbValue hVal, int children) {
|
||||
pDynString result = NULL;
|
||||
int i,length;
|
||||
char number[50];
|
||||
|
||||
result = CreateDynString(128,128);
|
||||
if(result == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DynStringConcat(result,"\"");
|
||||
DynStringConcat(result,name);
|
||||
DynStringConcat(result,"\"");
|
||||
if (hVal.dataType != HIPNONE)
|
||||
DynStringConcat(result,":");
|
||||
switch(hVal.dataType){
|
||||
case HIPNONE:
|
||||
break;
|
||||
case HIPINT:
|
||||
snprintf(number,50,"%ld",hVal.v.intValue);
|
||||
DynStringConcat(result,number);
|
||||
break;
|
||||
case HIPFLOAT:
|
||||
snprintf(number,50,"%lf",hVal.v.doubleValue);
|
||||
DynStringConcat(result,number);
|
||||
break;
|
||||
case HIPTEXT:
|
||||
DynStringConcat(result,hVal.v.text);
|
||||
break;
|
||||
case HIPINTAR:
|
||||
case HIPINTVARAR:
|
||||
for(i = 0; i < length; i++){
|
||||
snprintf(number,50,"%ld",hVal.v.intArray[i]);
|
||||
DynStringConcat(result,number);
|
||||
if(i > length -1){
|
||||
DynStringConcat(result,",");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HIPFLOATAR:
|
||||
case HIPFLOATVARAR:
|
||||
DynStringConcat(result,"[");
|
||||
for(i = 0; i < length; i++){
|
||||
snprintf(number,50,"%lf",hVal.v.floatArray[i]);
|
||||
DynStringConcat(result,number);
|
||||
if(i > length -1){
|
||||
DynStringConcat(result,",");
|
||||
}
|
||||
DynStringConcat(result,"]");
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
pDynString formatClientValue(char *name, hdbValue hVal, int children) {
|
||||
pDynString result = NULL;
|
||||
int i,length;
|
||||
char number[50];
|
||||
|
||||
result = CreateDynString(128,128);
|
||||
if(result == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DynStringConcat(result,name);
|
||||
DynStringConcat(result,",");
|
||||
DynStringConcat(result,hdbTypeToText(hVal.dataType));
|
||||
DynStringConcat(result,",");
|
||||
snprintf(number,50,"%d",children);
|
||||
DynStringConcat(result,number);
|
||||
DynStringConcat(result,",");
|
||||
if(hVal.dataType >= 3){
|
||||
length = hVal.arrayLength;
|
||||
} else {
|
||||
length = 1;
|
||||
}
|
||||
snprintf(number,50,"%d",length);
|
||||
DynStringConcat(result,number);
|
||||
DynStringConcat(result,",");
|
||||
switch(hVal.dataType){
|
||||
case HIPNONE:
|
||||
break;
|
||||
case HIPINT:
|
||||
snprintf(number,50,"%ld",hVal.v.intValue);
|
||||
DynStringConcat(result,number);
|
||||
break;
|
||||
case HIPFLOAT:
|
||||
snprintf(number,50,"%lf",hVal.v.doubleValue);
|
||||
DynStringConcat(result,number);
|
||||
break;
|
||||
case HIPTEXT:
|
||||
DynStringConcat(result,hVal.v.text);
|
||||
break;
|
||||
case HIPINTAR:
|
||||
case HIPINTVARAR:
|
||||
for(i = 0; i < length; i++){
|
||||
snprintf(number,50,"%ld",hVal.v.intArray[i]);
|
||||
DynStringConcat(result,number);
|
||||
if(i > length -1){
|
||||
DynStringConcat(result,",");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HIPFLOATAR:
|
||||
case HIPFLOATVARAR:
|
||||
for(i = 0; i < length; i++){
|
||||
snprintf(number,50,"%lf",hVal.v.floatArray[i]);
|
||||
DynStringConcat(result,number);
|
||||
if(i > length -1){
|
||||
DynStringConcat(result,",");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
DynStringConcat(result,"\n");
|
||||
return result;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int MakeHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
@@ -1273,13 +1422,36 @@ static int GetHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
}
|
||||
memset(&newValue,0,sizeof(hdbValue));
|
||||
GetHipadabaPar(targetNode, &newValue, pCon);
|
||||
parData = formatValue(newValue);
|
||||
|
||||
switch (currFmtStyle) {
|
||||
case cli:
|
||||
parData = formatClientValue(oriPath,newValue,0);
|
||||
break;
|
||||
case json:
|
||||
parData = formatJSONValue(oriPath,newValue,0);
|
||||
break;
|
||||
case plain:
|
||||
default:
|
||||
parData = formatValue(newValue);
|
||||
}
|
||||
|
||||
if(parData == NULL){
|
||||
SCWrite(pCon,"ERROR: out of memory formatting data",eError);
|
||||
return 0;
|
||||
}
|
||||
DynStringInsert(parData," =",0);
|
||||
DynStringInsert(parData,oriPath,0);
|
||||
|
||||
switch (currFmtStyle) {
|
||||
case cli:
|
||||
break;
|
||||
case json:
|
||||
DynStringInsert(parData,"{", 0);
|
||||
DynStringConcat(parData,"}");
|
||||
break;
|
||||
case plain:
|
||||
default:
|
||||
DynStringInsert(parData," =",0);
|
||||
DynStringInsert(parData,oriPath,0);
|
||||
}
|
||||
SCWrite(pCon,GetCharArray(parData),eValue);
|
||||
DeleteDynString(parData);
|
||||
ReleaseHdbValue(&newValue);
|
||||
@@ -1412,6 +1584,35 @@ static pDynString formatClientList(pHdb node){
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static pDynString formatJSONList(pHdb node){
|
||||
pHdb current;
|
||||
pDynString result = NULL;
|
||||
int length;
|
||||
int i;
|
||||
char number[50];
|
||||
|
||||
result = CreateDynString(128,128);
|
||||
if(result == NULL){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
current = node->child;
|
||||
while(current != NULL){
|
||||
DynStringConcat(result, GetCharArray(formatJSONValue(current->name,current->value,0)));
|
||||
if (current->next != NULL)
|
||||
DynStringConcat(result, ",");
|
||||
current = current->next;
|
||||
}
|
||||
if (node->child->value.dataType == HIPNONE) {
|
||||
DynStringInsert(result,"[", 0);
|
||||
DynStringConcat(result,"]");
|
||||
} else {
|
||||
DynStringInsert(result,"{", 0);
|
||||
DynStringConcat(result,"}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
pHdb node = NULL;
|
||||
@@ -1437,12 +1638,24 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
}
|
||||
|
||||
strtolower(argv[1]);
|
||||
if(strcmp(argv[1],"-val") == 0){
|
||||
if (argc == 3) {
|
||||
if(strcmp(argv[1],"-val") == 0){
|
||||
listData = formatListWithVal(node);
|
||||
} else if(strcmp(argv[1],"-cli") == 0){
|
||||
} else if(strcmp(argv[1],"-cli") == 0 || currFmtStyle == cli){
|
||||
listData = formatClientList(node);
|
||||
}
|
||||
} else {
|
||||
listData = formatPlainList(node);
|
||||
switch (currFmtStyle) {
|
||||
case cli:
|
||||
listData = formatClientList(node);
|
||||
break;
|
||||
case json:
|
||||
listData = formatJSONList(node);
|
||||
break;
|
||||
case plain:
|
||||
default:
|
||||
listData = formatPlainList(node);
|
||||
}
|
||||
}
|
||||
if(listData == NULL){
|
||||
SCWrite(pCon,"ERROR: failed to format list",
|
||||
@@ -1454,6 +1667,17 @@ static int ListHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int setFormatStyle(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]) {
|
||||
if(strcmp(argv[1],formatName[json]) == 0){
|
||||
currFmtStyle = json;
|
||||
} else if(strcmp(argv[1],formatName[cli]) == 0){
|
||||
currFmtStyle = cli;
|
||||
} else if(strcmp(argv[1],formatName[plain]) == 0){
|
||||
currFmtStyle = plain;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int AutoNotifyHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
pHdb node = NULL;
|
||||
@@ -1535,6 +1759,7 @@ int InstallSICSHipadaba(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
AddCommand(pSics,"hlist", ListHdbNode, NULL, NULL);
|
||||
AddCommand(pSics,"hnotify", AutoNotifyHdbNode, NULL, NULL);
|
||||
AddCommand(pSics,"hdelcb", RemoveHdbCallback, NULL, NULL);
|
||||
AddCommand(pSics,"hstyle", setFormatStyle, NULL, NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user