Click here to Skip to main content
15,881,027 members
Articles / Desktop Programming / Win32

How To Add Simple Web-enabled 2D/3D Dashboards to your Natively Built Application

Rate me:
Please Sign up or sign in to vote.
4.92/5 (26 votes)
29 Dec 2010CPOL6 min read 58K   2.7K   65  
The webonization of gnuplot
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#if defined (WIN32) || defined (WIN64)
#include <direct.h>
#include <windows.h>
#endif
#include <string.h>
#include <sys/stat.h>

char *
search_env (char *pszenv,
            char *pszfilename, char *pszbuff, size_t cbbuff)
{
  char *psz = 0;
  char *tok = 0;
  char *pszpath = 0;

  if (!pszenv || !pszbuff)
    return (char *) 0;

  pszbuff[0] = 0;

  pszpath = getenv (pszenv);
  if (pszpath)
    {
      psz = strdup (pszpath);
      if (!psz)
        return (char *) 0;
      tok = strtok (psz, ":");
      while (tok)
        {
          struct stat stf;
          pszbuff[0] = 0;
          strcat (pszbuff, tok);
          strcat (pszbuff, "/");
          strcat (pszbuff, pszfilename);
          if (stat (pszbuff, &stf) == 0)
            {
              free (psz);
              return pszbuff;
            }
          tok = strtok (psz, ":");
        }
      free (psz);
    }

  return (char *) 0;
}



char *
search_path (char *pszfilename,
             char *pszbuff, size_t cbbuff)
{
  char *pszfilepart = 0;
  char *pszpath = 0;
  struct stat stf;

  if (!pszfilename || !pszbuff)
    return (char *) 0;

  pszbuff[0] = 0;

#if defined (WIN32) || defined (WIN64)
  if (SearchPathA (0, pszfilename,
                   0, cbbuff, pszbuff, pszfilepart) != 0)
    return pszbuff;
#else
  strcat (pszbuff, "./");
  strcat (pszbuff, pszfilename);
  if (stat (pszfilename, &stf) == 0)
    return pszbuff;

  if (search_env ("PATH", pszfilename, pszbuff, cbbuff))
    return (pszbuff);

  if (search_env
      ("LD_LIBRARY_PATH", pszfilename, pszbuff, cbbuff))
    return (pszbuff);

#endif
  return (char *) 0;
}

void
syntax (char *pszprog)
{
  fprintf (stderr, "syntax error:\n");
  fprintf (stderr,
           "%s [-d launch_directory] -e application [-arg arg0 -arg arg1 arg2 ... argN]\n",
           pszprog);
  exit (1);
}

#if defined (WIN32) || defined (WIN64)
void
spawn_proc (char *pszcommand_line)
{

  STARTUPINFOA startInfo;
  PROCESS_INFORMATION pInfo;

  memset (&startInfo, 0, sizeof (STARTUPINFO));
  memset (&pInfo, 0, sizeof (PROCESS_INFORMATION));
  startInfo.cb = sizeof (STARTUPINFO);
  startInfo.lpReserved = 0;
  startInfo.lpDesktop = 0;
  startInfo.lpTitle = 0;
  startInfo.dwFlags = 0;
  startInfo.cbReserved2 = 0;
  startInfo.lpReserved2 = 0;
  startInfo.wShowWindow = SW_SHOW;

  CreateProcessA (0, pszcommand_line, 0, 0, FALSE,
                  CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
                  0, 0, &startInfo, &pInfo);
}
#else
void
spawn_proc (char *pszcommand_line)
{
  putenv ("PATH=./:/bin:/usr/sbin:/usr/local/bin:/etc");
  system (pszcommand_line);
}
#endif

int
main (int argc, char *argv[])
{
  char szcommand[2048];
  char szargs[1024];
  int i = 0;
  char *pszexe = 0;

  szcommand[0] = 0;
  szargs[0] = 0;

  for (i = 1; i < argc; i++)
    {
      if ((argv[i][0] == '-' ||
           argv[i][0] == '/') && argv[i][2] == 0)
        {
          switch (argv[i][1])
            {
            case 'd':          /* change directory */
              _chdir (argv[i + 1]);
              i++;
              break;
            case 'e':          /* executable */
              pszexe = argv[i + 1];
              i++;
              break;
            default:
              strcat (szargs, " ");
              strcat (szargs, argv[i]);
              strcat (szargs, " ");
              strcat (szargs, argv[i + 1]);
              i++;
              break;
            }
        }
      else
        {
          strcat (szargs, " ");
          strcat (szargs, argv[i]);
        }
    }

  if (!pszexe)
    syntax (argv[0]);

  if (sizeof (szcommand) <
      strlen (pszexe) + strlen (szargs) + 1)
    {
      fprintf (stderr,
               "error: internal command buffer too small!\n");
      exit (1);
    }
  strcat (szcommand, pszexe);
  strcat (szcommand, " ");
  strcat (szcommand, szargs);

  spawn_proc (szcommand);

  exit (0);                     /* success */

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Currently I am a Software Architect for Compuware specializing in software security
Languages: C/C++, FORTRAN, COBOL, Java
Libraries: OpenGL, MFC, X11 (X-Windows), WIN32

Platform Experience: AIX, HP-UX, SunOS, Open VMS, AS400, OSF, AIX, SGI, Linux, Windows CE, Windows

I have a strong background in 3D graphics programming, TCP/IP development, threading, cross platform development, encryption and secured communications, and system level programming.

In the early years before OpenGL made its way to the PC world, I authored one of the first high performance double buffered 3D graphics engines that supported both DOS and Windows providing real time 3D graphics for Engineering Technology Associate’s Finite Element Model Builder, FEMB. You can see their products at www.eta.com.

I also hold a US patent http://www.patentstorm.us/patents/6894690.html

Comments and Discussions