Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages | Examples

Encode.cpp

This is an example of how to use the DataCodec class. The program creates an rdaData object and writes its binary representation to the file specified by the first command line argument.
See also:
Decode.cpp
#include <iostream>
#include <rda/DataCodec.h>
#include <rda/RDAService.h>

#ifdef CMW_WIN
   // Windows
   #include <windows.h>
#else
   // Unix
   #include <unistd.h>
   #include <sys/stat.h>
   #include <fcntl.h>
#endif

using namespace std;

int main(int argc, char** argv)
{
   //
   // Get the file name from the command line
   //
   const char* file = 0;
   if (argc > 1) file = argv[1];
   else
   {
      cout << "File name not specified\n";
      return 0;
   }
   rdaRDAService::init();

   //
   // Create a Data object and fill it with some values
   //
   rdaData data;
   data.insert("0",(double)0);
   data.insert("1",(float)1);
   data.insert("2",(short)2);
   data.insert("3",(long)3);
   data.insert("4",(longlong)4);
   data.insert("5",(signed char)5);
   data.insert("6","6");
   //
   // Encode the Data object...
   //
   unsigned long size = 0;
   char* buf = rdaDataCodec::encode(data, size);
   //
   // and write its binary representation to the file.
   //
   cout << "==> writing data to file: " << file << endl;
   int fd = open(file, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
   if (fd == -1)
   {
      perror("open() failed");
      return 0;
   }
   int ret = write(fd, buf, size);
   if (ret == -1) perror("write() failed");
   else cout << size << " bytes written\n";
   
   delete buf;
   close(fd);

   return 0;
}

RDA-2.3 documentation - 27 Jun 2007 - N.Trofimov