#include "stdinc.h"
#include "auxfns.h"

//----------------------------------------------------------------------------------------------------------------------
// INPUT: zero terminated string
// OUTPUT: new allocated copy of the input string
// PURPOSE/ACTION: 
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
char* DupStr(const char *source)
{
  return source?strcpy(new char[lstrlen(source)+1], source):NULL;
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: optional filename
// OUTPUT: new allocated string containing the application path + optional filename  
// PURPOSE/ACTION: to return what it returns
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
char* GetAppPathPlus(const char* filename)
{
  char AppPath[_MAX_PATH];
  GetModuleFileName(NULL, AppPath,_MAX_PATH);
  char * tmp = GetBasePath(AppPath);
  if(filename)
  { strcpy(AppPath,tmp);
    strcat(AppPath,filename);
    delete[] tmp;
    tmp = DupStr(AppPath);
  }
  return tmp;
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: filename(path included)
// OUTPUT: new allocated string containing the file's path
// PURPOSE/ACTION: !
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
char* GetBasePath(const char* name)
{
  char BasePath[_MAX_PATH] = "";
  _splitpath(name,BasePath,NULL,NULL,NULL);
  _splitpath(name,NULL,&BasePath[strlen(BasePath)],NULL,NULL);
  return DupStr(BasePath);
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT:  base path, absolute path + optional file name
// OUTPUT: path( + optional filename) relative to the inputted base path 
// PURPOSE/ACTION: !
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
char* GetRelPath(const char* pBase, const char* Abs)
{
  char* Base = GetBasePath(pBase);
  char buff[_MAX_PATH] = ".\\";
  if(!strnicmp(Base,Abs,strlen(Base))) strcat(buff,&Abs[strlen(Base)]);
  else if(Base[0]==Abs[0] && strlen(Abs)>2) strcpy(buff,&Abs[2]);
  else strcpy(buff,Abs);
  delete[] Base;
  return DupStr(buff);
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: base path, relative path + optional file name
// OUTPUT: absolute path + optional filename
// PURPOSE/ACTION: !
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
char* GetAbsPath(const char* pBase, const char* Rel)
{
  char buff[_MAX_PATH];
  char* Base = GetBasePath(pBase);
  strcpy(buff,Base);
  delete[] Base;
  if(Rel[0] == '.') strcat(buff,&Rel[2]);
  else if(Rel[0] == '\\') strcpy(&buff[2],Rel);
  else strcpy(buff,Rel);
  return DupStr(buff);
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: handle of disk file, zero terminated string
// OUTPUT: TRUE if successful
// PURPOSE/ACTION: loads a string from disk (override of the template function)
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
BOOL StoreItem(HANDLE file, char* string)
{
  DWORD bw, size = sizeof(DWORD), len = string?strlen((char*)string):0;
  WriteFile( file, &len, size, &bw, NULL);
  if(bw != size) return FALSE;
  bw = 0;
  if(string && len)WriteFile( file, string, len, &bw, NULL);
  return (bw == len);
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: handle of disk file, reference to zero terminated string
// OUTPUT: TRUE if successful, "string" will contain the result
// PURPOSE/ACTION: stores a string to disk (override of the template function)
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
BOOL LoadItem(HANDLE file, char*& string)
{
  string = NULL;
  DWORD br,size = sizeof(DWORD), len;
  ReadFile(file, &len, size, &br, NULL);
  if(br != size) return FALSE;
  if(len>0)
  { 
    if(!(string = new char[len+1])) return FALSE;
    ReadFile(file, string, len, &br, NULL);
    string[len] = 0;
  }
  else br = 0;
  if(br != len) {delete[] string; string = NULL;}
  return (br == len);
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: handle of disk file, filename(with absolute path), base path
// OUTPUT: TRUE if successful
// PURPOSE/ACTION: stores a filename to disk using a relative path
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
BOOL StoreFileName(HANDLE file, char* fname, char* BasePath)
{
  char* relname = NULL;
  BOOL res = StoreItem(file, fname?relname = GetRelPath(BasePath,fname):fname);
  SafeDelArray(relname);
  return res;
}

//----------------------------------------------------------------------------------------------------------------------
// INPUT: handle of disk file, base path, reference to pointer
// OUTPUT: TRUE if successful. "fname" will point to a newly allocated string containing the file name
// PURPOSE/ACTION: stores a filename to disk using a relative path
// CREATED/COMMENTED: JULY 25, 1996   UBI SOFT ROMANIA  MIXER TEAM (Catalin Cocos, Bogdan Ghenea, Madalin Moise)
//----------------------------------------------------------------------------------------------------------------------
BOOL LoadFileName(HANDLE file, char*& fname, char* BasePath)
{
  fname = NULL;
  char* relname;
  BOOL res = LoadItem(file, relname);
  if(res && relname) fname = GetAbsPath(BasePath, relname);
  SafeDelArray(relname);
  return res;
}

