Cクライアントの作業リストの例
#include <stdio.h>
#include <stdlib.h>
/* JSM header */
#include "x_jsm.h"
#define JSMHOST "LOCALHOST:4560"
#define MAX_BYTE_LENGTH 32767
#define WORKING_LIST_SIZE 100
#define PK1_LENGTH 10
#define PK2_LENGTH 7
/* Working list entry definition:
*
* <rdml:field name="TXT" type="A" length="30" />
* <rdml:field name="PK1" type="P" length="10" decimal="2" />
* <rdml:field name="PK2" type="P" length="7" decimal="0" />
*
* Bytes for packed field = ( length / 2 ) + 1
*/
struct Example3Line
{
char txt[30];
char pk1[(PK1_LENGTH / 2) + 1];
char pk2[(PK2_LENGTH / 2) + 1];
};
static char status[JSM_STATUS_SIZE+1];
static char response[JSM_RESPONSE_SIZE+1];
static char byteArray[MAX_BYTE_LENGTH];
static int byteArrayLength;
/* Simple wrapper functions for JSM API calls */
void jsmOpen(char *cmd);
void jsmCommand(char *cmd);
void jsmCommandX(char *cmd, void *target, int strucSize);
void jsmClose();
/* Main function */
int main(int argc, char **argv);
void example3Test();
/* Utility functions */
struct Example3Line makeEx3Line(char *txt, long pk1, long pk2);
void trim(char *str);
void printCharArray(char *chars, int size);
void printEx3Line(struct Example3Line line);
void getByteArray(int *byteSize, char *byteArray);
void longToPacked(unsigned char *packed, long num, int length);
long packedToLong(unsigned char *packed, int length);
void jsmOpen(char *cmd)
{
printf("\nJSMOPEN %s\n",cmd);
JSMOPEN(cmd,status,response);
trim(response);
printf("status = %s\nresponse = %s\n",status,response);
}
void jsmCommand(char *cmd)
{
printf("\n%s\n",cmd);
JSMCMD(cmd,status,response);
trim(response);
printf("status = %s\nresponse = %s\n",status,response);
}
void jsmCommandX(char *cmd, void *target, int strucSize)
{
printf("\n%s\n",cmd);
byteArrayLength = strucSize;
JSMCMDX(cmd,(char*)target,&byteArrayLength,status,response);
trim(response);
printf("status = %s\nresponse = %s\n",status,response);
}
void jsmClose()
{
printf("\nJSMCLOSE\n");
JSMCLOSE(status,response);
trim(response);
printf("status = %s\nresponse = %s\n",status,response);
}
int main(int argc, char **argv)
{
if (argc > 1)
{
jsmOpen(argv[1]);
}
else
{
jsmOpen(JSMHOST);
}
example3Test();
jsmClose();
return 0;
}
void example3Test()
{
char command[JSM_COMMAND_SIZE+1];
int byteSize;
int i;
struct Example3Line workingList[WORKING_LIST_SIZE];
struct Example3Line fields = makeEx3Line( "3GL client field",
-987654321,
180980 );
workingList[0] = makeEx3Line( "Text line 1", 12345, 678 );
workingList[1] = makeEx3Line( "Text line 2", -52300, -3918 );
jsmCommand("SERVICE_LOAD SERVICE(EXAMPLE3)");
sprintf ( command,
"SETFIELDS SERVICE_STRUCTURE(EXAMPLE3.LINE) SIZE(%d)",
sizeof(fields)
);
jsmCommandX(command,&fields,sizeof(fields));
getByteArray(&byteSize,(char*)&fields);
printf("\nFields\n");
printEx3Line(fields);
sprintf ( command,
"ADDENTRY SERVICE_STRUCTURE(EXAMPLE3.LINE) SIZE(%d) OCCURS(%d) COUNT(%d)",
sizeof(fields), WORKING_LIST_SIZE, 2
);
jsmCommandX(command,workingList,sizeof(workingList));
getByteArray(&byteSize, (char*) workingList);
for (i=0; i<(byteSize/sizeof(struct Example3Line)); i++)
{
printf("\nWorking list entry %d\n",i+1);
printEx3Line(workingList[i]);
}
}
struct Example3Line makeEx3Line(char *txt, long pk1, long pk2)
{
struct Example3Line line;
memset(line.txt,' ',30);
memcpy(line.txt,txt,strlen(txt));
longToPacked(line.pk1,pk1,PK1_LENGTH);
longToPacked(line.pk2,pk2,7);
return line;
}
void trim(char *str)
{
char *end = str;
while (*str)
if (*(str++) != ' ')
end = str;
*end = '\0';
}
void printCharArray(char *chars, int size)
{
int i;
putchar('"');
for (i=0; i<size; i++)
if (*chars)
putchar(*(chars++));
else
break;
putchar('"');
putchar('\n');
}
void printEx3Line(struct Example3Line line)
{
printf(" TXT = ");
printCharArray(line.txt,30);
printf(" PK1 = %.2f\n",packedToLong(line.pk1,PK1_LENGTH)/100.0);
printf(" PK2 = %d\n",packedToLong(line.pk2,PK2_LENGTH));
}
void getByteArray(int *byteSize, char *byteArray)
{
JSMBYTELNGTH(byteSize);
printf("byte array size = %d\n",*byteSize);
JSMBYTERECV(byteArray);
}
void longToPacked(unsigned char *packed, long num, int length)
{
int packedLen = (length / 2) + 1;
int i;
unsigned char *c = &packed[packedLen-1];
/* Sign */
if (num < 0)
{
*c = 0x0D;
num = -num;
}
else
{
*c = 0x0F;
}
/* High nibble of last byte */
*c |= (num % 10) << 4;
num /= 10;
for (c = &packed[packedLen-2]; c >= packed; c--)
{
*c = num % 10;
num /= 10;
*c |= (num % 10) << 4;
num /= 10;
}
}
long packedToLong(unsigned char *packed, int length)
{
int packedLen = (length / 2) + 1;
long num = 0;
unsigned char hi,lo;
int i;
for (i=0; i<packedLen; i++)
{
hi = (packed[i] & 0xF0) >> 4;
lo = (packed[i] & 0x0F);
if (i < (packedLen-1))
{
if (hi > 9 || lo > 9)
return 0;
num = (num * 100) + (hi * 10) + lo;
}
else
{
num = (num * 10) + hi;
if (lo == 0x0D)
num = -num;
else if (lo != 0x0F)
num = 0;
}
}
return num;
}