The Gnome Chemistry Utils 0.13.3
testbabelserver.c
00001 /* 
00002  * Gnome Chemisty Utils
00003  * tests/testbabelserver.c 
00004  *
00005  * Copyright (C) 2010-2011 Jean Bréfort <jean.brefort@normalesup.org>
00006  *
00007  * This program is free software; you can redistribute it and/or 
00008  * modify it under the terms of the GNU General Public License as 
00009  * published by the Free Software Foundation; either version 2 of the
00010  * License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00020  * USA
00021  */
00022 
00023 #include "config.h"
00024 #include <glib.h>
00025 #include <netinet/in.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <sys/stat.h>
00029 #include <sys/un.h>
00030 #include <unistd.h>
00031 
00032 int main ()
00033 {
00034         struct stat statbuf;
00035         char inbuf[256], *start = NULL;
00036         int index, cur, length;
00037         char *usr = getenv ("USER");
00038         char *path = malloc (strlen ("/tmp/babelsocket-") + strlen (usr) + 1);
00039         strcpy (path, "/tmp/babelsocket-");
00040         strcat (path, usr);
00041         if (strlen (path) >= 107) { //WARNING: don't know if this is portable
00042                 puts ("path too long");
00043                 return -1;
00044         }
00045         if (stat (path, &statbuf)) {
00046                 char *args[] = {LIBEXECDIR"/babelserver", NULL};
00047                 GError *error = NULL;
00048                 g_spawn_async (NULL, (char **) args, NULL, 0, NULL, NULL, NULL, &error);
00049                 if (error) {
00050                         g_error_free (error);
00051                         error = NULL;
00052                         free (path);
00053                         return -1;
00054                 }
00055                 while (stat (path, &statbuf));
00056         }
00057         int babelsocket = socket (AF_UNIX, SOCK_STREAM, 0);
00058         if (babelsocket == -1) {
00059                 perror ("Could not create the socket");
00060                 free (path);
00061                 return -1;
00062         }
00063         struct sockaddr_un adr_serv;
00064         adr_serv.sun_family = AF_UNIX;
00065         strcpy (adr_serv.sun_path, path);
00066         free (path);
00067         if (connect (babelsocket, (const struct sockaddr*) &adr_serv, sizeof (struct sockaddr_un)) == -1) {
00068                 perror ("Connexion failed");
00069                 return -1;
00070         }
00071         char const *buf = "-i xyz -o inchi ";
00072         write (babelsocket, buf, strlen (buf));
00073         buf = "5\n\nC       0       0       0\nH       0       1.093   0\nH       1.030490282     -0.364333333    0\nH       -0.515245141    -0.364333333    0.892430763\nH       -0.515245141    -0.364333333    -0.892430763";
00074         char *size = g_strdup_printf ("-l %u -D", strlen (buf));
00075         write (babelsocket, size, strlen (size));
00076         write (babelsocket, buf, strlen (buf));
00077         while (1) {
00078                 if ((cur = read (babelsocket, inbuf + index, 255 - index))) {
00079                         index += cur;
00080                         inbuf[index] = 0;
00081                         if (start == NULL) {
00082                                 if ((start = strchr (inbuf, ' '))) {
00083                                         length = strtol (inbuf, NULL, 10);
00084                                         start++;
00085                                 }
00086                         }
00087                         if (index - (start - inbuf) == length) {
00088                                 printf ("answer is: %s\n", start);
00089                                 break;
00090                         }
00091                 }
00092         }
00093         close (babelsocket);
00094         return 0;
00095 }