Node:The changebase Source Code, Previous:Reference, Up:Top



The changebase Source Code

This appendix contains the source code for the changebase program. The changebase program is used by Padre Software to alter html and RealMedia ram files just before they are transferred by WebPublish. Specifically, the changebase program will change the local base directory that is specified in the html and ram files to the base directory that it should be on the remote server.

The function parse_command_line() is called to read in the command line arguments that were passed to the program by WebPublish. The command line arguments contain the account information for the website that is being published.

The function change_html() will alter the base address in an html file to the address that it should be on the server. For example, a local base address of <BASE href="file:/mnt/linux/websites/weedguy/">, will be changed to <BASE href="http://weedguy.padresoftware.com/">.

The function change_ram() will alter the base address in a RealMedia ram file to the address that it should be on the server. For example, a local base address of file:/mnt/linux/websites/weedguy/, will be changed to http://weedguy.padresoftware.com/.

Finally, the main program function will use the information that was included on the command line to determine which website is being published and which base address is to be used when changing a base address.

// Sample HTML and RAM alteration program for WebPublish 0.0.0

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>             // 'getopt_long' function.

#define OK                      0
#define ERROR                   1
#define NOCHANGES               2
#define CHANGES                 3

char*           account         = (char*)0;
char*           website         = (char*)0;
char*           server          = (char*)0;
char*           user            = (char*)0;
char*           directory       = (char*)0;
char*           extension       = (char*)0;

static char shortoptions[]      = "a:w:s:u:d:e:";
static struct option            longoptions[] =
   {
      { "account",     true,   0,              'a'             },
      { "website",     true,   0,              'w'             },
      { "server",      true,   0,              's'             },
      { "user",        true,   0,              'u'             },
      { "directory",   true,   0,              'd'             },
      { "extension",   true,   0,              'e'             },

      { 0,             0,      0,               0              }
   };

int store_parse_option(char*& item, char *optarg)
   {
      int               result          = OK;

      if (item != (char*)0)
         {
            cerr << "Item already allocated.\n";
            result      = ERROR;
         }
      else
         {
            item        = (char*)malloc(strlen(optarg) + 1);
            if (item == (char*)0)
               {
                  cerr << "Could not allocate memory.\n";
                  result        = ERROR;
               }
            else
               {
                  strcpy(item, optarg);
               }
         }
      return(result);
   }

// Parse the command line options.

int parse_command_line(int argc, char** argv)
   {
      int               result          = OK;
      int               option;                 // Switch value.
      int               flagindex       = 0;    // Cmd line flag index.

      while ((option = getopt_long(argc, argv, shortoptions, longoptions,
         &flagindex)) != EOF && result == OK)
         {
            switch(option)
               {
                  case 'a':             // -a or --account
                     result     = store_parse_option(
                                  account, optarg);
                     break;
                  case 'w':             // -w or --website
                     result     = store_parse_option(
                                  website, optarg);
                     break;
                  case 's':             // -s or --server
                     result     = store_parse_option(
                                  server, optarg);
                     break;
                  case 'u':             // -u or --user
                     result     = store_parse_option(
                                  user, optarg);
                     break;
                  case 'd':             // -d or --directory
                     result     = store_parse_option(
                                  directory, optarg);
                     break;
                  case 'e':             // -e or --extension
                     result     = store_parse_option(
                                  extension, optarg);
                     break;
                  default:              // Unsupported
                     result     = ERROR;
                     cerr << "Unknown command line option.\n";
                     break;
               }
         }
      return(result);
   }

int change_html(const char* base)
   {
      int               result          = NOCHANGES;
      char*             pointer;
      char              buffer[1024];

      while (fgets(buffer, 1024, stdin) != 0)
         {
            if ((pointer = strstr(buffer, "\n", buffer, base);
               {
                  result        = CHANGES;
               }
            else if ((pointer = strstr(buffer, "\n", buffer, base);
               {
                  result        = CHANGES;
               }
            else
               {
                  if (fputs(buffer, stdout) == 0)
                     {
                        cerr << "Could not write to stdout.\n";
                        result	= ERROR;
                        break;
                     }
               }
         }
      return(result);
   }

int change_ram(const char* base)
   {
      int               result          = NOCHANGES;
      char              buffer[1024];
      char              newram[1024];

      if (fgets(buffer, 1024, stdin) == (char*)0)
         {
            /* Could not read from file.			*/

            cerr << "Could not read from stdin.\n";
            result      = ERROR;
         }
      else
         {
            strcat(newram, base);
            strcat(newram, buffer + strlen(website) + 6);
            if (fputs(newram, stdout) == 0)
               {
                  /* Could not write to stdout.			*/

                  cerr << "Could not write to stdout.\n";
                  result        = ERROR;
               }
            else
               {
                  result        = CHANGES;
               }
         }
      return(result);
   }

int main(int argc, char** argv)
   {
      int               result          = NOCHANGES;
      char              base[1024];

      if (parse_command_line(argc, argv) == ERROR)
         {
            cerr << "Error parsing shell program command line.\n";

            result      = ERROR;
         }
      else if (strcmp(account, "padre") == 0)
         {
            strcpy(base, "http://padresoftware.com/");
         }
      else if (strcmp(account, "tripod") == 0)
         {
            strcpy(base, "http://weedguy20.tripod.com/");
         }
      else if (strcmp(account, "weedguy") == 0)
         {
            strcpy(base, "http://weedguy.padresoftware.com/");
         }
      else if (strcmp(account, "president") == 0)
         {
            strcpy(base, "http://padresi.tripod.com/");
         }
      else if (strcmp(account, "personal") == 0)
         {
            strcpy(base, "http://charles.padresoftware.com/");
         }
      else if (strcmp(account, "journal") == 0)
         {
            strcpy(base, "http://videojournal.tripod.com/");
         }
      else
         {
            cerr << "Unknown account!\n";
            result      = ERROR;
         }
      if (result != ERROR)
         {
            if (strcmp(extension, "html") == 0)
               {
                  result        = change_html(base);
               }
            else if (strcmp(extension, "ram") == 0)
               {
                  result        = change_ram(base);
               }
            else
               {
                  cerr << "Unknown extension!\n";
                  result        = ERROR;
               }
         }
      return(result);
   }