#include "RGSTDAFX.H"

#ifndef __RMDEF_HPP
#include "rmdef.hpp"
#endif

#include "cfuncs.hpp"                                                                                   // This is a global C funtion man.

CObject* CreateRuntimeClassObject( char* szClassName )
{
  CRuntimeClass* pClassRef = FindRuntimeClass( szClassName, 0 );
  if ( pClassRef )
    {
    // allocate a new object based on the class just acquired
    CObject* object = pClassRef->CreateObject();
    if (object == NULL)
      AfxThrowMemoryException();
    return object; 
    }           
  else
    return NULL;
}

CRuntimeClass* PASCAL FindRuntimeClass( char* szClassName, UINT* pwSchemaNum)
{
  CRuntimeClass* pClass;

#ifndef _AFXDLL
  for (pClass = CRuntimeClass::pFirstClass; pClass != NULL; pClass = pClass->m_pNextClass)
  {
    if (lstrcmp(szClassName, pClass->m_lpszClassName) == 0)
      return pClass;
  }
#else
  // all registered CRuntimeClasses should be owned by the app or a DLL
  ASSERT(CRuntimeClass::pFirstClass == NULL);

  // first walk through the app specific classes
  for (pClass = _AfxGetAppData()->pFirstAppClass; pClass != NULL;
    pClass = pClass->m_pNextClass)
  {
    if (lstrcmp(szClassName, pClass->m_lpszClassName) == 0)
      return pClass;
  }
  // now walk through the classes in different DLLs
  CDynLinkLibrary* pDLL;
  for (pDLL = _AfxGetAppData()->pFirstDLL; pDLL != NULL;
    pDLL = pDLL->m_pNextDLL)
  {
    for (pClass = pDLL->m_pFirstSharedClass; pClass != NULL;
      pClass = pClass->m_pNextClass)
    {
      if (lstrcmp(szClassName, pClass->m_lpszClassName) == 0)
        return pClass;
    }
  }
#endif //_AFXDLL

  return NULL; // not found
}

void GetOwnerNameFromDisplay( CString &displayString, CString &owner, CString &name )
{
  // Takes strings in format "name(owner)" and makes a owner and a name.
  
  owner.Empty();
  name.Empty();
  
  if ( !displayString.IsEmpty() )
    {
    // Put into proper format.
    int pos = displayString.Find( '(' );
    CString tempOwner = displayString.Right( displayString.GetLength() - pos );
    CString tempName  = displayString.Left( pos );
  
    // Get rid of parens and spaces for owner.
    for( int i=0; i<tempOwner.GetLength(); i++ )
      {
      if ( tempOwner[i] != '(' && tempOwner[i] != ')' && tempOwner[i] != ' ' )
        {
        owner += tempOwner[i];
        }
      }
    // Ditto for name.
    for( i=0; i<tempName.GetLength(); i++ )
      {
      if ( tempName[i] != '(' && tempName[i] != ')' && tempName[i] != ' ' )
        {
        name += tempName[i];
        }
      }
    }
}

void GetTempDirectory( CString & pathName, CString & aFileName )
{
  // buffer to compare to find temp
  char    firstfour[6];
  
  // get and hold the dos env. don't change anything!!
  LPSTR   lpszEnv = ::GetDOSEnvironment();

  pathName.Empty();

  // Get the temp directory.
  while ( *lpszEnv!=0 && pathName.IsEmpty() ) 
    {
    int len = lstrlen(lpszEnv);
    lstrcpyn( firstfour, lpszEnv, ((len>5)?5:len) );
    if ( !lstrcmpi( "temp", firstfour ) )
      {
      pathName = (const char*)&lpszEnv[5];
      }
    lpszEnv += len + 1;
    }
    
  // If we didn't find the temp, use the windows dir.  
  if ( pathName.IsEmpty() )
    {
    char * buf = new char[8000];
    ::GetWindowsDirectory( buf, 8000 );
    pathName = (const char *)buf;
    delete [] buf;
    }
      
  if ( !aFileName.IsEmpty() )
    {
    // caller wants a pathName appended.
    if ( pathName[pathName.GetLength()-1] != '\\' )
      {
      pathName += '\\';
      }
    pathName += aFileName;
    }
}

int GetOsys_Val = -1;

#define WF_WINNT  0x4000
#define WF_WIN95  0x2000

//****** RETURN OPERATING SYSTEM ENVIRONMENT ********************************//
// AUTHOR:   Madhur Eichberger                                               //
//                                                                           //
// OBJECTIVE:                                                                //
// Determine the current operation system environment                        //
//                                                                           //
// ARGUMENTS:                                                                //
//                                                                           //
// OUTPUT:                                                                   //
// ret: OS_WIN31 Windows 3.1                                                 //
// ret: OS_WIN95 Windows 95                                                  //
// ret: OS_WINNT Windows NT                                                  //
//                                                                           //
// CREATED   07/18/95 MADHUR EICHBERGER                                      //
//***************************************************************************//
int GetOsys()
{
DWORD dwVersion;
int ret;

    //****** CHECK IF FIRST TIME **********************************************
    if (GetOsys_Val > 0)
        return(GetOsys_Val);
    
    //****** CHECK FLAGS FOR OPERATION SYSTEM *********************************
    dwVersion = GetWinFlags();
    ret = OS_WIN31;     // assume win 3.11
    if ((dwVersion & WF_WINNT) != 0) ret = OS_WINNT;
    if ((dwVersion & WF_WIN95) != 0) ret = OS_WIN95;
    GetOsys_Val = ret;  // save for direct access next time
TRACE("GetOsys: ret=%d, GetWinFlags: 0x%lX",ret,dwVersion);
    return(ret);
}

/****** COUNT NUMBER OF ARGUMENTS IN DELIMITTED STRING ***********************/
/* Input:                                                                    */
/* str               = string containing all arguments separated with delim  */
/*                     (no limit on the length !)                            */
/* del               = delimiter character                                   */
/*                                                                           */
/* Output:                                                                   */
/* number of arguments                                                       */
/*****************************************************************************/
int strcnt(char *str,char del)
{
char *p1, *pmax;
int cntarg;

    cntarg = 0;                                    /* reset argument counter */
    pmax = str + strlen(str) - 1;                    /* goto to end of input */
    if (del != ' ') while((pmax >= str)&&(*pmax == ' ')) pmax--; /* cut blanks*/
    p1 = str;
    while ( (p1 <= pmax) && (*p1 == ' ') ) p1++;       /* skip leading blanks */
    while (p1 <= pmax)
        {
        if ((p1 == pmax) && (*p1 != del)) cntarg++; /* simulate last delimter */
        if ((del == ' ') && (*p1 == del) && (*(p1+1) == ' ')) cntarg--; /*skip*/
        if (*(p1++) == del) cntarg++;              /* count normal delimiter */
        }
    return(cntarg);
}
#define MAXSTRARG    100

/****** SELECT ARGUMENT FROM ARGUMENT LIST (DIRECT OUTPUT) *******************/
/* Input:                                                                    */
/* str               = argument list input (no limit on length)              */
/* del               = delimiter character between arguments in 'str'        */
/* start             = first argument to be selected for output              */
/*                     1  = first argument in list                           */
/*                     -1 = last  argument in list (counted from back)       */
/*                     maximal 100 arguments (MAXSTRARG)                     */
/* stop              = last argument to be selected for output               */
/*                     (can be negative if from back)                        */
/* out               = output buffer (limit depending on your allocation)    */ 
/* out_le            = output buffer limit                                   */ 
/*                                                                           */
/*                                                                           */
/* Output:                                                                   */
/* >=0               = number of bytes in out                                */
/* -1                = invalid start stop specification                      */
/*****************************************************************************/
int strfld(char *str,char del,int start,int stop,char *out,int out_le)
{
register char *p;
int i, i1, i2, nmax, cntarg, off[MAXSTRARG + 1];

    /****** CHECK IF VALID RANGE ********************************************/
    out[0] = '\0';
    if (stop == 0) stop = start;
    if( ((start>0) && (start>stop)) || ((start<0) && (start>stop))) return(-1);
    
    /****** GET ACTUAL ARGUMENTS ********************************************/
    cntarg = strcnt(str,del);           /* get real number of args          */
    if( (start ==  99) && (start > cntarg   ))
        {
        start = cntarg;
        }
    if( (start == -99) && (start < (-cntarg)))
        {
        start = -cntarg;
        }
    if (abs(start) > cntarg) return(-2);

    /****** ADJUST START/STOP ***********************************************/
    if( stop > cntarg ) stop  = cntarg;
    if( (stop < 0) && (stop < (-cntarg))) stop  = -cntarg;
    if( start < 0) i1 = cntarg - abs(stop) + 1;
    if( stop  < 0) i2 = cntarg - abs(start) + 1;
    if( start < 0) start = i2;
    if( stop  < 0) stop  = i1;

    /****** BUILD OFFSET LIST ***********************************************/
    p = str;
    if( del == ' ')                     /* skip leading blanks              */
        {
        while (*p == ' ') p++;             
        }
    off[1] = 0;                         /* set the first arg offset         */
    i1 = 2;
    nmax = strlen(p);
    for( i = 0; i < nmax; i++)
        {
        if( p[i] == del)                /* delimitter found (pack blanks)   */
            {
            if (del != ' ')
                {
                off[i1++] = i + 1;
                }
            else{
                if( p[i+1] != del)     /* pack */
                    {
                        off[i1++] = i + 1;
                    }
                }
            }
        }
    off[i1] = i;                        /* save last byte */

    /****** EXTRACT SPECIFIED FIELDS ******************************************/
    i1 = 0;                             /* reset output buffer counter */
    for( i = off[start]; i < (off[stop + 1]); i++)
        {
        if( i1 >= out_le) break;
        out[i1++] = p[i];
        }
    if( out[i1 - 1] == del)             /* supress last delimitter */
        {
        i1--;
        }
    out[i1] = '\0';                     /* set string termination  */
    start = stop = 0;                   /* reset the arguments     */
    return(strlen(out));
}

void GetMatrixCellTitle(char *szTitle,int nRow,int nCol)
{
    int nID = nRow*100 + nCol;
    switch (nID)
    {
    case 0*100 + 0: strcpy(szTitle,"Blank");                   break;
    case 0*100 + 1: strcpy(szTitle,"Column Cell Header");      break;
    case 0*100 + 2: strcpy(szTitle,"Column Total Header");     break;
    case 0*100 + 3: strcpy(szTitle,"Column Grand Total Header"); break;
                       
    case 1*100 + 0: strcpy(szTitle,"Row Header");              break;
    case 1*100 + 1: strcpy(szTitle,"Intersection");            break;
    case 1*100 + 2: strcpy(szTitle,"Column Total");            break;
    case 1*100 + 3: strcpy(szTitle,"Grand Total");             break;

    case 2*100 + 0: strcpy(szTitle,"Row Total Header");        break;
    case 2*100 + 1: strcpy(szTitle,"Row Total");               break;
    case 2*100 + 2: strcpy(szTitle,"Row Grand Total");         break;
    case 2*100 + 3: strcpy(szTitle,"Final Grand Total");       break;

    default:       strcpy(szTitle,"Matrix Body");              break;
    }
}
 
void GetAggrText(char *szAggr,int nAggr)
{
    switch (nAggr)
    {
    case REP_MX_AGGR_NONE: strcpy(szAggr,REP_MX_AGGR_NONE_TXT);  break;
    case REP_MX_AGGR_CNT : strcpy(szAggr,REP_MX_AGGR_CNT_TXT );  break;
    case REP_MX_AGGR_SUM : strcpy(szAggr,REP_MX_AGGR_SUM_TXT );  break;
    case REP_MX_AGGR_AVG : strcpy(szAggr,REP_MX_AGGR_AVG_TXT );  break;
    case REP_MX_AGGR_MIN : strcpy(szAggr,REP_MX_AGGR_MIN_TXT );  break;
    case REP_MX_AGGR_MAX : strcpy(szAggr,REP_MX_AGGR_MAX_TXT );  break;
    case REP_MX_AGGR_MEAN: strcpy(szAggr,REP_MX_AGGR_MEAN_TXT);  break;
    default              : sprintf(szAggr,"AGGR%02d",nAggr);     break;
    }
}

int GetAggrCode(char *szAggr)
{
int ret;

    ret = REP_MX_AGGR_NONE;
    if (strcmp(szAggr,REP_MX_AGGR_CNT_TXT ) == 0) ret = REP_MX_AGGR_CNT;
    if (strcmp(szAggr,REP_MX_AGGR_SUM_TXT ) == 0) ret = REP_MX_AGGR_SUM;
    if (strcmp(szAggr,REP_MX_AGGR_AVG_TXT ) == 0) ret = REP_MX_AGGR_AVG;
    if (strcmp(szAggr,REP_MX_AGGR_MIN_TXT ) == 0) ret = REP_MX_AGGR_MIN;
    if (strcmp(szAggr,REP_MX_AGGR_MAX_TXT ) == 0) ret = REP_MX_AGGR_MAX;
    if (strcmp(szAggr,REP_MX_AGGR_MEAN_TXT) == 0) ret = REP_MX_AGGR_MEAN;
    return(ret);
}
 
