/*
   b_ptr_fpos_t.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 Wednesday, May 23 at 11:38 AM EDT

TITLE
   b_ptr_fpos_t.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_ptr_fpos_t.h"

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


#define TESTDIR "testdir"
#define TESTFILE "testdir/testfile_fpos_t"
static FILE* filePtr = NULL;
static fpos_t temp_fpos_t;
//--------------------------------------------------------------------

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

  //generated

   strcpy(_b_ptr_fpos_tTYPENAME,CLASS_STRING);
strcpy(_b_ptr_fpos_tBEGINNING,"b_ptr_fpos_t_BEGINNING");
strcpy(_b_ptr_fpos_tMIDDLE,"b_ptr_fpos_t_MIDDLE");
strcpy(_b_ptr_fpos_tEND,"b_ptr_fpos_t_END");
strcpy(_b_ptr_fpos_tMAX_STRUCT,"b_ptr_fpos_t_MAX_STRUCT");
strcpy(_b_ptr_fpos_tMIN_STRUCT,"b_ptr_fpos_t_MIN_STRUCT");
}

//--------------------------------------------------------------------
b_param *b_ptr_fpos_t::b_ptr_fpos_tBEGINNING()
{
   return &_b_ptr_fpos_tBEGINNING;
}

b_param *b_ptr_fpos_t::b_ptr_fpos_tMIDDLE()
{
   return &_b_ptr_fpos_tMIDDLE;
}

b_param *b_ptr_fpos_t::b_ptr_fpos_tEND()
{
   return &_b_ptr_fpos_tEND;
}

b_param *b_ptr_fpos_t::b_ptr_fpos_tMAX_STRUCT()
{
   return &_b_ptr_fpos_tMAX_STRUCT;
}

b_param *b_ptr_fpos_t::b_ptr_fpos_tMIN_STRUCT()
{
   return &_b_ptr_fpos_tMIN_STRUCT;
}

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

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

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
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_ptr_fpos_t_beginning = 0;
b_ptr_fpos_t_middle = 0;
b_ptr_fpos_t_end = 0;
b_ptr_fpos_t_max_struct = 0;
b_ptr_fpos_t_min_struct = 0;
   int dataPTR =0;


   dataPTR++;
if (strcmp(data[dataPTR],_b_ptr_fpos_tBEGINNING)==0)
      b_ptr_fpos_t_beginning = 1;
   else if (strcmp(data[dataPTR],_b_ptr_fpos_tMIDDLE)==0)
      b_ptr_fpos_t_middle = 1;
   else if (strcmp(data[dataPTR],_b_ptr_fpos_tEND)==0)
      b_ptr_fpos_t_end = 1;
   else if (strcmp(data[dataPTR],_b_ptr_fpos_tMAX_STRUCT)==0)
      b_ptr_fpos_t_max_struct = 1;
   else if (strcmp(data[dataPTR],_b_ptr_fpos_tMIN_STRUCT)==0)
      b_ptr_fpos_t_min_struct = 1;
  else
    {
      cerr<<"Error: Unknown setting for the "
          <<"VALUE"
          <<" dial of the data object "
          <<CLASS_STRING
          <<". "<<endl
          <<"The offending string is : "
          <<data[dataPTR]
          <<endl;
      exit(1);
    }

 int position = 0;

if (b_ptr_fpos_t_beginning==1)
{

 position = 1;

}

if (b_ptr_fpos_t_middle==1)
{

 position = 9;

}

if (b_ptr_fpos_t_end==1)
{

 position = 72; 

}

if (b_ptr_fpos_t_beginning==1 || b_ptr_fpos_t_middle==1 || b_ptr_fpos_t_end==1)
{

 int fd;
 char buffer[128];

 
 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,"b_ptr_fpos_t datatype not setup properly - mkdir failed. Function not tested\n");
 fclose(logFile);
 exit(99);
 }
 }

 
 if (unlink (TESTFILE) != 0 )
 {
 if (errno != ENOENT)
 {
 fprintf(logFile,"unlink failed. Function not tested\n");
 fclose(logFile); 
 exit(99);
 }
 }

 if((fd = open (TESTFILE, O_WRONLY | O_CREAT | O_TRUNC, 0644))==-1)
 {
 fprintf(logFile,"b_ptr_fpos_t datatype not setup properly - open failed. Function not tested\n");
 fclose(logFile);
 exit(99);
 }

 write (fd, "I am a test file.\n", 18);
 write (fd, "I am a test file.\n", 18);
 write (fd, "I am a test file.\n", 18);
 write (fd, "I am a test file.\n", 18);

 if (close (fd)!=0) 
 {
 fprintf(logFile,"b_ptr_fpos_t datatype not setup properly - close failed. Function not tested\n");
 fclose(logFile);
 exit(99);
 }

 if ((filePtr = fopen (TESTFILE,"r+")) == NULL)
 {
 fprintf(logFile,"b_ptr_fpos_t datatype not setup properly - fopen failed. Function not tested\n");
 fclose(logFile);
 exit(99);
 }
 
 fread(buffer, sizeof(char), position, filePtr);
 
 if (fgetpos(filePtr, _theVariable) != 0)
 {
 fclose(filePtr);
 fprintf(logFile,"b_ptr_fpos_t datatype not setup properly - fgetpos failed. Function not tested\n");
 fclose(logFile);
 exit(99);
 }

 fclose(logFile);

}

if (b_ptr_fpos_t_max_struct==1)
{

 temp_fpos_t.__pos = MAXINT;
 temp_fpos_t.__state.__count = MAXINT;

}

if (b_ptr_fpos_t_min_struct==1)
{

 temp_fpos_t.__pos = -MAXINT;
 temp_fpos_t.__state.__count = -MAXINT;

}
   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_ptr_fpos_t_beginning==1 || b_ptr_fpos_t_middle==1 || b_ptr_fpos_t_end==1)
{

 FILE* logFile = NULL;

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

 if (filePtr != NULL)
 {
 fclose(filePtr);
 }
 
 
 if (unlink(TESTFILE)!=0)
 { 
 
 if (errno != ENOENT)
 {
 fprintf(logFile,"unlink testfile_fpos_t failed\n");
 }
 }
 fclose(logFile);

}
   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 5;
      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_ptr_fpos_tBEGINNING();
         break;
      case 2:
         return b_ptr_fpos_tMIDDLE();
         break;
      case 3:
         return b_ptr_fpos_tEND();
         break;
      case 4:
         return b_ptr_fpos_tMAX_STRUCT();
         break;
      case 5:
         return b_ptr_fpos_tMIN_STRUCT();
         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;
}

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