#include "RGSTDAFX.H"
#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;
    }
}

extern "C" 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                                      //
//***************************************************************************//
extern "C" 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);
}
