// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <stdlib.h>
#include <string.h>

#include "Version.h"

typedef char *charptr;

static char *store = NULL;

Version *Version::instance = NULL;

Version::Version()
{
   noVersions = 0;
   module = new charptr[MAX_VERSIONS];
   version = new charptr[MAX_VERSIONS];
   date = new charptr[MAX_VERSIONS];
}

Version *Version::getInstance()
{
   if(instance == NULL)
   {
      instance = new Version();
   }
   return instance;
}

void Version::add(char *m, char *v, char *d)
{
   module[noVersions] = m;
   version[noVersions] = v;
   date[noVersions] = d;
   noVersions++;
   return;
}

char *VersionDefine::getVersion(char *m)
{
   if(store == NULL)
   {
      store = new char[MAX_INFO_LEN];
   }
   Version *ver = Version::getInstance();
   strcpy(store, "");
   for(int i = 0; i < ver->getNoVersions(); i++)
   {
      if(strcmp(m, ver->getModule(i)) == 0)
      {
         strcat(store, ver->getVersion(i));
         strcat(store, " ");
         strcat(store, ver->getDate(i));
      }
   }
   return store;
}

char *VersionDefine::getAllVersions()
{
   if(store == NULL)
   {
      store = new char[MAX_INFO_LEN];
   }
   Version *ver = Version::getInstance();
   strcpy(store, "");
   for(int i = 0; i < ver->getNoVersions(); i++)
   {
      strcat(store, ver->getModule(i));
      strcat(store, " ");
      strcat(store, ver->getVersion(i));
      strcat(store, " ");
      strcat(store, ver->getDate(i));
      strcat(store, "\n");
   }
   return store;
}

