Tips

4D Open APIs - Part X:4Dデータ(sample code)の変換

日付2001/09/21
ID01-736
バージョン6.7.x specific
プラットフォームWindows and Mac

4D Open:4Dデータの変換

4D Open librariesを備えたデータの扱いはデータ転換を要求するかもしれません。

Converting a string into a 4D Integer:
data.typ = s_Int16;
StringToNum(kAlphaData, &tempNum );
data.u.i = LoWord(tempNum);
Check(_4D_AddToBuffer(hConnect, hBuffer, 4, &data),"_4D_AddToBuffer\0");

ストリングを4D実数に変換すること:
data.typ = s_Number;
MonReal = atof(kAlphaData1);
data.u.r.r = MonReal;
Check(_4D_AddToBuffer(hConnect, hBuffer, 3, &data),"_4D_AddToBuffer\0");

stringを4D Longintに変換する:
data.typ = s_Int32;
StringToNum(kAlphaData1, &tempNum );
data.u.l = tempNum;
Check(_4D_AddToBuffer(hConnect, hBuffer, 5, &data),"_4D_AddToBuffer\0");

これはこれらのデータを変換するコード例です:
#if __INTEL__ /* <-- defined by CodeWarrior (x86 project) */
/* #pragma once */
/* #include "ansi_prefix.win32.h" */
/* #include */
/* Use pre-compiled header instead */
#elif WIN32 /* <-- defined by Visual C++ */
#include
#endif
#define Openlib 1
#include "Open4D.h"
#include
#include
#include
/*#ifdef __NWERKS__
#ifdef __INTEL__
#define WINVER 0x0400
#define MACVER 0x0400
#endif
#else
#define WINVER 0x0400
#define __INTEL__
#endif*/
const short KNC = NC_TCPIP;
#if def __INTEL__
const unsigned char KStationName[32]="Intel machine\0";
else
const unsigned char KStationName[32]="Motorola machine\0";
#endif
const unsigned char KUserName[32]="APIUser\0";
const unsigned char KUserPass[32]="APIPass\0";
const unsigned char KUserTask[32]="APITask\0";
const unsigned char kAlphaData[256] = "012345\0";
NetworkComponentHandle hNetworkComponent;
NetworkLocationPtr pNetworkLocation;
ConnectHandle hConnect;
IDRec logRec;
unsigned char szName[256];
int choix;
void initialisation(void);
int2 StartWSConnection(void);
void Check(int2 myErr, char *description);
void CloseWSConnection(void);
void AddRecords(void);
int2 StartWSConnection()
{
int2 myErr;
myErr =_4D_Select4DServer(szName, &pNetworkLocation, KNC, hNetworkComponent);
if(!myErr)
{
_4D_Open4DConnection(&logRec, pNetworkLocation, &hConnect,KNC, hNetworkComponent);
}
return(myErr);
}
void CloseWSConnection()
{
_4D_Close4DConnection(hConnect);
}
void initialisation(void)
{
memcpy(&logRec.StationName[1],KStationName, strlen((char *)KStationName));
logRec.StationName[0]=strlen((char *)KStationName);
memcpy(&logRec.UserName[1],KUserName, strlen((char *)KUserName));
logRec.UserName[0]=strlen((char *)KUserName);
memcpy(&logRec.UserPass[1],KUserPass, strlen((char *)KUserPass));
logRec.UserPass[0]=strlen((char *)KUserPass);
memcpy(&logRec.TaskName[1],KUserTask, strlen((char *)KUserTask));
logRec.TaskName[0]=strlen((char *)KUserTask);
}
void Check(int2 myErr, char *description)
{
char err[256];
if(myErr)
{
sprintf(err, "%s: returns %d\n", description , myErr);
printf(err);
getchar();
}
}
void AddRecords(void)
{
BufferHandle hBuffer;
Handle4D hText, hPict, hBlob;
char *pText, *pPict, *pBlob;
DataRec data;
long tempNum;
float MonReal;
short fNb = 1, nbRec=1;
hBuffer = _4D_CreateBuffer(hConnect, fNb);
data.typ = s_Number;
MonReal = atof(kAlphaData1);
data.u.r.r = MonReal;
Check(_4D_AddToBuffer(hConnect, hBuffer, 3, &data),"_4D_AddToBuffer\0");
data.typ = s_Int16;
StringToNum(kAlphaData1, &tempNum );
data.u.i = LoWord(tempNum);
Check(_4D_AddToBuffer(hConnect, hBuffer, 4, &data),"_4D_AddToBuffer\0");
data.typ = s_Int32;
StringToNum(kAlphaData1, &tempNum );
data.u.l = tempNum;
Check(_4D_AddToBuffer(hConnect, hBuffer, 5, &data),"_4D_AddToBuffer\0");
Check(_4D_NewRecord(hConnect, hBuffer),"_4D_NewRecord\0");
Free4DHandle(hText);
Free4DHandle(hPict);
Free4DHandle(hBlob);
Free4DHandle((Handle4D) hBuffer);
}
void main(void)
{
initialisation();
_4D_Init4DWS();
printf("THE VERSION NUMBER IS: %ld\n", (_4D_GetVersionNumber()>>16));
printf("THE 4D NETWORK COMPONENT ID IS %ld\n",_4D_NB_NetworkComponent());
printf("\n");
hNetworkComponent= _4D_InitNetworkComponent(KNC);
if(hNetworkComponent)
{
if(!StartWSConnection())
{
AddRecords();
}
_4D_DeInitNetworkComponent(KNC,hNetworkComponent);
}
_4D_DeInit4DWS();
}