/*
   b_fname.cpp   Generated by the Ballista(tm) Project data object compiler
   Copyright (C) 1998-2001  Carnegie Mellon University

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   File generated Tuesday, May 22 at 04:52 PM EDT

TITLE
   b_fname.cpp
*/

//--------------------------------------------------------------------

#include <errno.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <stream.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "b_fname.h"

//--------------------------------------------------------------------



#define TESTDIR "testdir/"
#define TESTFILE "testdir/testfile_fname"
#define TESTSUBDIR "testdir/testsubdir_fname"

#define BMAX_STR 1028
static FILE* filePtr = NULL;
static char *fnameString = NULL;
static char *cp_fnameStr = NULL;
//--------------------------------------------------------------------

CLASSNAME::CLASSNAME()
{
  //DIAL DECLARATIONS HERE

  //generated

   strcpy(_b_fnameTYPENAME,CLASS_STRING);
strcpy(_b_fnameR_USER,"b_fname_R_USER");
strcpy(_b_fnameW_USER,"b_fname_W_USER");
strcpy(_b_fnameX_USER,"b_fname_X_USER");
strcpy(_b_fnameRWX_USER,"b_fname_RWX_USER");
strcpy(_b_fnameR_GRP,"b_fname_R_GRP");
strcpy(_b_fnameNONE,"b_fname_NONE");
strcpy(_b_fnameNOT_EXIST_FILE,"b_fname_NOT_EXIST_FILE");
strcpy(_b_fnameNOT_EXIST_DIR,"b_fname_NOT_EXIST_DIR");
strcpy(_b_fnameOPEN_WRITE,"b_fname_OPEN_WRITE");
strcpy(_b_fnameOPEN_READ,"b_fname_OPEN_READ");
strcpy(_b_fnameCLOSED,"b_fname_CLOSED");
strcpy(_b_fnameLINK,"b_fname_LINK");
strcpy(_b_fnameEMPTY,"b_fname_EMPTY");
strcpy(_b_fnameNON_EMPTY,"b_fname_NON_EMPTY");
strcpy(_b_fnameLOCAL,"b_fname_LOCAL");
strcpy(_b_fnameTMP,"b_fname_TMP");
strcpy(_b_fnameEMBEDDED_SPACE,"b_fname_EMBEDDED_SPACE");
strcpy(_b_fnameLONG,"b_fname_LONG");
strcpy(_b_fnameSPECIAL_CHAR,"b_fname_SPECIAL_CHAR");
strcpy(_b_fnameDIR,"b_fname_DIR");
}

//--------------------------------------------------------------------
b_param *b_fname::b_fnameR_USER()
{
   return &_b_fnameR_USER;
}

b_param *b_fname::b_fnameW_USER()
{
   return &_b_fnameW_USER;
}

b_param *b_fname::b_fnameX_USER()
{
   return &_b_fnameX_USER;
}

b_param *b_fname::b_fnameRWX_USER()
{
   return &_b_fnameRWX_USER;
}

b_param *b_fname::b_fnameR_GRP()
{
   return &_b_fnameR_GRP;
}

b_param *b_fname::b_fnameNONE()
{
   return &_b_fnameNONE;
}

b_param *b_fname::b_fnameNOT_EXIST_FILE()
{
   return &_b_fnameNOT_EXIST_FILE;
}

b_param *b_fname::b_fnameNOT_EXIST_DIR()
{
   return &_b_fnameNOT_EXIST_DIR;
}

b_param *b_fname::b_fnameOPEN_WRITE()
{
   return &_b_fnameOPEN_WRITE;
}

b_param *b_fname::b_fnameOPEN_READ()
{
   return &_b_fnameOPEN_READ;
}

b_param *b_fname::b_fnameCLOSED()
{
   return &_b_fnameCLOSED;
}

b_param *b_fname::b_fnameLINK()
{
   return &_b_fnameLINK;
}

b_param *b_fname::b_fnameEMPTY()
{
   return &_b_fnameEMPTY;
}

b_param *b_fname::b_fnameNON_EMPTY()
{
   return &_b_fnameNON_EMPTY;
}

b_param *b_fname::b_fnameLOCAL()
{
   return &_b_fnameLOCAL;
}

b_param *b_fname::b_fnameTMP()
{
   return &_b_fnameTMP;
}

b_param *b_fname::b_fnameEMBEDDED_SPACE()
{
   return &_b_fnameEMBEDDED_SPACE;
}

b_param *b_fname::b_fnameLONG()
{
   return &_b_fnameLONG;
}

b_param *b_fname::b_fnameSPECIAL_CHAR()
{
   return &_b_fnameSPECIAL_CHAR;
}

b_param *b_fname::b_fnameDIR()
{
   return &_b_fnameDIR;
}

//--------------------------------------------------------------------
//---------------------------------------------------------------------------
//type name return method

b_param *CLASSNAME::typeName()
{
  return &_b_fnameTYPENAME;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int CLASSNAME::distanceFromBase()
{
  return CLASSPARENT::distanceFromBase() +1;
}


//---------------------------------------------------------------------------
void CLASSNAME::typeList(b_param list[], int num)
{
  strcpy(list[num],(char *) typeName());
  CLASSPARENT::typeList(list,num+1);
}
//--------------------------------------------------------------------
  void *CLASSNAME::access(b_param data[])
{
  if (strcmp(data[0],(char *)typeName())!=0)
    return CLASSPARENT::access(data);

  //ACCESS CODE
b_fname_r_user = 0;
b_fname_w_user = 0;
b_fname_x_user = 0;
b_fname_rwx_user = 0;
b_fname_r_grp = 0;
b_fname_none = 0;
b_fname_not_exist_file = 0;
b_fname_not_exist_dir = 0;
b_fname_open_write = 0;
b_fname_open_read = 0;
b_fname_closed = 0;
b_fname_link = 0;
b_fname_empty = 0;
b_fname_non_empty = 0;
b_fname_local = 0;
b_fname_tmp = 0;
b_fname_embedded_space = 0;
b_fname_long = 0;
b_fname_special_char = 0;
b_fname_dir = 0;
   int dataPTR =0;


   dataPTR++;
if (strcmp(data[dataPTR],_b_fnameR_USER)==0)
      b_fname_r_user = 1;
   else if (strcmp(data[dataPTR],_b_fnameW_USER)==0)
      b_fname_w_user = 1;
   else if (strcmp(data[dataPTR],_b_fnameX_USER)==0)
      b_fname_x_user = 1;
   else if (strcmp(data[dataPTR],_b_fnameRWX_USER)==0)
      b_fname_rwx_user = 1;
   else if (strcmp(data[dataPTR],_b_fnameR_GRP)==0)
      b_fname_r_grp = 1;
   else if (strcmp(data[dataPTR],_b_fnameNONE)==0)
      b_fname_none = 1;
  else
    {
      cerr<<"Error: Unknown setting for the "
          <<"PERMISSIONS"
          <<" dial of the data object "
          <<CLASS_STRING
          <<". "<<endl
          <<"The offending string is : "
          <<data[dataPTR]
          <<endl;
      exit(1);
    }

   dataPTR++;
if (strcmp(data[dataPTR],_b_fnameNOT_EXIST_FILE)==0)
      b_fname_not_exist_file = 1;
   else if (strcmp(data[dataPTR],_b_fnameNOT_EXIST_DIR)==0)
      b_fname_not_exist_dir = 1;
   else if (strcmp(data[dataPTR],_b_fnameOPEN_WRITE)==0)
      b_fname_open_write = 1;
   else if (strcmp(data[dataPTR],_b_fnameOPEN_READ)==0)
      b_fname_open_read = 1;
   else if (strcmp(data[dataPTR],_b_fnameCLOSED)==0)
      b_fname_closed = 1;
   else if (strcmp(data[dataPTR],_b_fnameLINK)==0)
      b_fname_link = 1;
  else
    {
      cerr<<"Error: Unknown setting for the "
          <<"STATE"
          <<" dial of the data object "
          <<CLASS_STRING
          <<". "<<endl
          <<"The offending string is : "
          <<data[dataPTR]
          <<endl;
      exit(1);
    }

   dataPTR++;
if (strcmp(data[dataPTR],_b_fnameEMPTY)==0)
      b_fname_empty = 1;
   else if (strcmp(data[dataPTR],_b_fnameNON_EMPTY)==0)
      b_fname_non_empty = 1;
  else
    {
      cerr<<"Error: Unknown setting for the "
          <<"CONTENTS"
          <<" dial of the data object "
          <<CLASS_STRING
          <<". "<<endl
          <<"The offending string is : "
          <<data[dataPTR]
          <<endl;
      exit(1);
    }

   dataPTR++;
if (strcmp(data[dataPTR],_b_fnameLOCAL)==0)
      b_fname_local = 1;
   else if (strcmp(data[dataPTR],_b_fnameTMP)==0)
      b_fname_tmp = 1;
   else if (strcmp(data[dataPTR],_b_fnameEMBEDDED_SPACE)==0)
      b_fname_embedded_space = 1;
   else if (strcmp(data[dataPTR],_b_fnameLONG)==0)
      b_fname_long = 1;
   else if (strcmp(data[dataPTR],_b_fnameSPECIAL_CHAR)==0)
      b_fname_special_char = 1;
   else if (strcmp(data[dataPTR],_b_fnameDIR)==0)
      b_fname_dir = 1;
  else
    {
      cerr<<"Error: Unknown setting for the "
          <<"NAME"
          <<" dial of the data object "
          <<CLASS_STRING
          <<". "<<endl
          <<"The offending string is : "
          <<data[dataPTR]
          <<endl;
      exit(1);
    }

 char fname[128];
 char tempStr[64];
 char linkName[BMAX_STR +4];

 int dirFlag = 0;
 int emptyFlag = 0;
 int linkFlag = 0;

 mode_t permissionMode = 777;

 static int fname_count = 0; /* count for multiple instances */
 char fname_count_str [127]; /* count, in string format */

 

 FILE* logFile = NULL;

 if ((logFile = fopen ("/tmp/templateLog.txt","a+")) == NULL)
 {
 exit(99);
 }
 


 
 if (mkdir(TESTDIR,S_IRWXU|S_IRWXG|S_IRWXO)!=0) /* create test directory, u+rwx */
 {
 
 if (errno != EEXIST) 
 {
 fprintf(logFile, "problems creating test directory - b_fname\n");
 fclose(logFile);
 exit(99);
 }
 }
 
 
 system("rm -rf testdir/testfile_fname*");
 system("rm -rf testdir/testsubdir_fname");
 system ("rm -rf /tmp/ballista_test_b_fname*");


if (b_fname_r_user==1)
{

 permissionMode = 0400;
 
}

if (b_fname_w_user==1)
{

 permissionMode = 0200;
 
}

if (b_fname_x_user==1)
{
 
 permissionMode = 0100;
 
}

if (b_fname_rwx_user==1)
{

 permissionMode = 0700;
 
}

if (b_fname_r_grp==1)
{
 
 permissionMode = 0040;
 
}

if (b_fname_none==1)
{

 permissionMode = 0000;
 
}

if (b_fname_local==1)
{

 fnameString = fname;
 strcpy(fnameString,TESTFILE);
 
}

if (b_fname_dir==1)
{

 fnameString = fname;
 strcpy(fnameString,TESTSUBDIR);
 
}

if (b_fname_embedded_space==1)
{

 fnameString = fname;
 strcpy(fnameString,TESTFILE);
 strcat(fnameString,"_Embedded Space");
 
}

if (b_fname_long==1)
{

 fnameString = (char*) malloc(sizeof(char) * BMAX_STR);
 if (fnameString == NULL)
 { 
 fprintf (logFile, "b_fname LONG - malloc failed - value not tested\n");
 fclose(logFile);
 exit(99);
 }
 strcpy(fnameString, TESTFILE);
 int i;
 for (i = strlen(fnameString); i < (BMAX_STR -2); i++)
 {
 fnameString[i] = 'a';
 }
 fnameString[BMAX_STR-1] = '\0';
 
}

if (b_fname_special_char==1)
{

 fnameString = fname;
 strcpy(fnameString,TESTFILE);
 strcat(fnameString,"_SPECIAL!@#$%^&*(){}[]<>?/-CHAR");
 
}

if (b_fname_tmp==1)
{

 fnameString = fname;
 strcpy(fnameString,"/tmp/ballista_test_b_fname");
 
}

if (b_fname_empty==1)
{

 emptyFlag = 1;
 
}

if (b_fname_non_empty==1)
{

 emptyFlag = 0;
 
}

if (b_fname_not_exist_file==1)
{

 strcpy(tempStr, "rm -f ");
 strcat(tempStr, fnameString);
 _theVariable = fnameString;
 
}

if (b_fname_not_exist_dir==1)
{

 strcpy(tempStr, "rm -rf ");
 strcat(tempStr, TESTDIR);
 system(tempStr);
 _theVariable = fnameString;
 
}

if (b_fname_link==1)
{

 linkFlag = 1;
 
}

if (b_fname_open_write==1 || b_fname_open_read==1 || b_fname_closed==1 || b_fname_link==1)
{

 
 if (!linkFlag)
 {
 filePtr = fopen(fnameString, "w+");
 }
 else
 {
 strcpy(linkName,fnameString);
 strcat(linkName,"LINK");
 filePtr = fopen(linkName, "w+");
 } 
 if (filePtr == NULL)
 {
 fprintf(logFile, "b_fname OPEN_WRITE, OPEN_READ, LINK, or CLOSED had problems opening file %s - value not tested\n");
 fclose(logFile);
 exit(99);
 } 

 if (!emptyFlag)
 {
 fprintf(filePtr, "This is a test file!\n");
 }
 fclose(filePtr);

 if(!linkFlag)
 { 
 chmod(fnameString, permissionMode);
 }
 else
 {
 chmod(linkName, permissionMode);
 }
 
}

if (b_fname_open_read==1)
{

 filePtr = fopen(fnameString, "r+");
 if (filePtr == NULL)
 {
 fprintf(logFile, "b_fname OPEN_READ had problems opening file %s - value not tested\n");
 fclose(logFile);
 exit(99);
 }
 _theVariable = fnameString;
 
}

if (b_fname_open_write==1)
{

 filePtr = fopen(fnameString, "w+");
 if (filePtr == NULL)
 {
 fprintf(logFile, "b_fname OPEN_WRITE had problems opening file - value not tested\n");
 fclose(logFile);
 exit(99);
 }
 _theVariable = fnameString;
 
}

if (b_fname_link==1)
{

 if (link(linkName,fnameString) == -1)
 {
 fprintf(logFile,"b_fname LINK -error creating link - value not tested\n");
 fclose(logFile);
 exit(99);
 }
 chmod(fnameString, permissionMode);
 _theVariable = fnameString;
 
}

 cp_fnameStr = fnameString;
 fclose(logFile);
   return &_theVariable;
}

//--------------------------------------------------------------------
  int CLASSNAME::commit(b_param tname)
{
  if (strcmp(tname,(char *)typeName())!=0)
    return CLASSPARENT::commit(tname);
  //COMMIT CODE HERE
//generated
   return 0;
}

//--------------------------------------------------------------------
  int CLASSNAME::cleanup(b_param tname)
{
  if (strcmp(tname,(char *)typeName())!=0)
    return CLASSPARENT::cleanup(tname);
      
  //CLEANUP CODE
//generated

if (b_fname_open_read==1 || b_fname_open_write==1)
{

 fclose(filePtr);
 
}

if (b_fname_link==1)
{

 unlink(cp_fnameStr);
 
}

if (b_fname_long==1)
{

 free(cp_fnameStr);
 
}

 system ("rm -rf testdir/testfile_fname*");
 system ("rm -rf testdir/testsubdir_fname");
 rmdir(TESTDIR);

if (b_fname_tmp==1)
{

 system ("rm -rf /tmp/ballista_test_b_fname*");
 
}
   return 0;
}

//--------------------------------------------------------------------
int CLASSNAME::numDials(b_param tname)
{
  if (!strcmp(tname,(char *)typeName()))
    return NUMBER_OF_DIALS;
  else return CLASSPARENT::numDials(tname);
      
}


//---------------------------------------------------------------------------
int CLASSNAME::numItems(b_param tname,int dialNumber)
{
  if (strcmp(tname,(char *)typeName())!=0)
    return CLASSPARENT::numItems(tname,dialNumber);
  switch (dialNumber)
    {
      //NUMITEMS SWITCH CASES HERE
      //generated

   case 1:
      return 6;
      break;

   case 2:
      return 6;
      break;

   case 3:
      return 2;
      break;

   case 4:
      return 6;
      break;
//---------------------------------------------------------------------------
      //end generated

    default:
      cerr<<"Error, invalid dial number passed to "
          <<CLASS_STRING<<"::numItems\n"
          <<"Please check declaration files.  Dial number passed was "
          <<dialNumber<<endl;
      exit(1);
    }
  return 0;
}

//---------------------------------------------------------------------------
b_param *CLASSNAME::paramName(b_param tname,
                                   int dialNumber,
                                   int position)

{
  if (strcmp(tname,(char *)typeName())!=0)
    return CLASSPARENT::paramName(tname,dialNumber,position);
  
  switch (dialNumber)
    {
      //PARAMNAME SWITCH CASES HERE
      //generated

case 1:
 switch (position)
   {
      case 1:
         return b_fnameR_USER();
         break;
      case 2:
         return b_fnameW_USER();
         break;
      case 3:
         return b_fnameX_USER();
         break;
      case 4:
         return b_fnameRWX_USER();
         break;
      case 5:
         return b_fnameR_GRP();
         break;
      case 6:
         return b_fnameNONE();
         break;

      default:
          cerr<<"Error, invalid position number passed to "
              <<CLASS_STRING<<"::paramName\n"
              <<"Please check declaration files.  Dial number passed was"
              <<dialNumber<<" position "<<position<<".\n";
          exit(1);
   }
   break;

case 2:
 switch (position)
   {
      case 1:
         return b_fnameNOT_EXIST_FILE();
         break;
      case 2:
         return b_fnameNOT_EXIST_DIR();
         break;
      case 3:
         return b_fnameOPEN_WRITE();
         break;
      case 4:
         return b_fnameOPEN_READ();
         break;
      case 5:
         return b_fnameCLOSED();
         break;
      case 6:
         return b_fnameLINK();
         break;

      default:
          cerr<<"Error, invalid position number passed to "
              <<CLASS_STRING<<"::paramName\n"
              <<"Please check declaration files.  Dial number passed was"
              <<dialNumber<<" position "<<position<<".\n";
          exit(1);
   }
   break;

case 3:
 switch (position)
   {
      case 1:
         return b_fnameEMPTY();
         break;
      case 2:
         return b_fnameNON_EMPTY();
         break;

      default:
          cerr<<"Error, invalid position number passed to "
              <<CLASS_STRING<<"::paramName\n"
              <<"Please check declaration files.  Dial number passed was"
              <<dialNumber<<" position "<<position<<".\n";
          exit(1);
   }
   break;

case 4:
 switch (position)
   {
      case 1:
         return b_fnameLOCAL();
         break;
      case 2:
         return b_fnameTMP();
         break;
      case 3:
         return b_fnameEMBEDDED_SPACE();
         break;
      case 4:
         return b_fnameLONG();
         break;
      case 5:
         return b_fnameSPECIAL_CHAR();
         break;
      case 6:
         return b_fnameDIR();
         break;

      default:
          cerr<<"Error, invalid position number passed to "
              <<CLASS_STRING<<"::paramName\n"
              <<"Please check declaration files.  Dial number passed was"
              <<dialNumber<<" position "<<position<<".\n";
          exit(1);
   }
   break;
    default:
      cerr<<"Error, invalid dial number passed to "
          <<CLASS_STRING<<"::paramName\n"
          <<"Please check declaration files.  Dial number passed was "
          <<dialNumber<<endl;
      exit(1);

    }
  return NULL;
}

//---------------------------------------------------------------------------
