urbisendbin.cc

00001 
00004 #include "urbi/uclient.hh"
00005 #include <sys/types.h>
00006 #include "libport/sys/stat.h"
00007 #include "libport/windows.hh"
00008 
00009 int main(int argc, char * argv[])
00010 {
00011   if (argc < 4 || argc >5)
00012     {
00013       printf("usage: %s robotname variablename filename ['BIN headers']\n"
00014              "\t send the content of a file to the URBI server, to be saved in a variable\n"
00015              "\t example: %s myrobot sounds.hello ~/sounds/hello.wav WAV\n",argv[0], argv[0]);
00016       return -1;
00017     }
00018 
00019   char * headers= "";
00020   if (argc==5)
00021     headers=argv[4];
00022 
00023   urbi::UClient uc(argv[1]);
00024   if (uc.error())
00025     exit(2);
00026 
00027   FILE *f;
00028   if (STREQ(argv[3],"-"))
00029     f = stdin;
00030   else
00031     f = fopen(argv[3],"r");
00032   if (!f)
00033     {
00034       printf("error opening file\n");
00035       exit(3);
00036     }
00037 
00038   char * buffer;
00039   int pos;
00040   //read the whole file in memory
00041   if (f!=stdin)
00042   {
00043     struct stat st;
00044     stat(argv[3],&st);
00045     buffer = static_cast<char *> (malloc (st.st_size));
00046     if (!buffer)
00047     {
00048       printf("not enough memory\n");
00049       return -2;
00050     }
00051 
00052     pos=0;
00053     while (true)
00054     {
00055       int r = fread(buffer + pos, 1, st.st_size-pos, f);
00056       if (r<=0)
00057         break;
00058       pos +=r;
00059     }
00060   }
00061 
00062   else
00063   {
00064     int sz=10000;
00065     pos = 0;
00066     buffer = static_cast<char *> (malloc (sz));
00067     while (true)
00068     {
00069       if (sz-pos < 500)
00070       {
00071         sz += 10000;
00072         buffer = static_cast<char *> (realloc (buffer,sz));
00073         if (!buffer)
00074         {
00075           printf("not enough memory\n");
00076           return -2;
00077         }
00078         int l = fread(buffer + pos, 1, sz-pos, f);
00079         if (l<=0)
00080           break;
00081         pos +=l;
00082       }
00083     }
00084   }
00085 
00086   uc.sendBin(buffer, pos, "%s = BIN %d %s;", argv[2], pos, headers);
00087   sleep(1);
00088 }

Generated on Tue Apr 10 17:45:45 2007 for URBISDK by  doxygen 1.5.1