* Make globals g_HomeDirectory and g_WorkDirectory an std::string instead of char[1024]

* Use a function to return the current directory as an std::string (rather than writing it into a buffer that's given along)
 * #define snprintf _snprintf so we can use that as well...
 * Some const correctness

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@3181 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-12-26 18:26:40 +00:00
parent 8708174859
commit c7894a8a01
6 changed files with 102 additions and 77 deletions

View File

@ -33,6 +33,12 @@
#include "bteditview.h"
//#include "debugwin.h"
#include <string>
#include <boost/scoped_array.hpp>
using std::string;
using boost::scoped_array;
CBTEditCommandLineInfo g_cmdInfo;
void CBTEditCommandLineInfo::ParseParam(const char* pszParam,BOOL bFlag,BOOL bLast)
@ -82,8 +88,7 @@ void CBTEditCommandLineInfo::ParseParam(const char* pszParam,BOOL bFlag,BOOL bLa
/////////////////////////////////////////////////////////////////////////////
// CBTEditApp initialization
char g_HomeDirectory[1024];
char g_WorkDirectory[1024];
string g_HomeDirectory, g_WorkDirectory;
HCURSOR g_Wait;
HCURSOR g_Pointer;
@ -142,7 +147,7 @@ class CBTEditApp : public CWinApp
RUNTIME_CLASS(CBTEditView));
AddDocTemplate(pDocTemplate);
GetCurrentDirectory(sizeof(g_HomeDirectory),g_HomeDirectory);
g_HomeDirectory = getCurrentDirectory();
g_Wait = ::LoadCursor(NULL,IDC_WAIT);
g_Pointer = LoadCursor(IDC_POINTER);
@ -196,3 +201,18 @@ BEGIN_MESSAGE_MAP(CBTEditApp, CWinApp)
END_MESSAGE_MAP()
CBTEditApp theApp;
std::string getCurrentDirectory()
{
// Determine the required buffer size to contain the current directory
const DWORD curDirSize = GetCurrentDirectory(0, NULL);
scoped_array<char> curDir(new char[curDirSize]);
// Retrieve the current directory
GetCurrentDirectory(curDirSize, curDir.get());
MessageBox(0, curDir.get(), "curDir.get()", MB_OK);
// Return the current directory as a STL string
return std::string(curDir.get());
}

View File

@ -29,6 +29,9 @@
#endif
#include "resource.h" // main symbols
#include <string>
#define snprintf _snprintf
class CBTEditCommandLineInfo : public CCommandLineInfo
{
@ -53,3 +56,5 @@ public:
};
extern CBTEditCommandLineInfo g_cmdInfo;
extern std::string getCurrentDirectory();

View File

@ -48,10 +48,12 @@
#include <string>
using std::string;
#define MAX_FILESTRING 512
extern char g_HomeDirectory[1024];
extern char g_WorkDirectory[1024];
extern string g_HomeDirectory;
extern string g_WorkDirectory;
extern BOOL g_OverlayTypes;
extern BOOL g_OverlayZoneIDs;
@ -638,7 +640,7 @@ void CBTEditDoc::InitialiseData(void)
// m_DirectDrawView->SetFilter(TRUE);
GetCurrentDirectory(sizeof(g_WorkDirectory),g_WorkDirectory);
g_WorkDirectory = getCurrentDirectory();
// m_DirectDrawView = new CDirectDraw(NULL,NULL,FALSE,
// &DesiredProfile);
@ -699,14 +701,14 @@ void CBTEditDoc::InitialiseData(void)
// Read in all the objects.
// char Name[256];
// strcpy(Name,g_HomeDirectory);
// strcpy(Name,g_HomeDirectory.c_str());
// strcat(Name,"\\Data\\Startup.txt");
// m_HeightMap->ReadObjects(Name);
m_HeightMap->GetTextureSize(&m_TextureHeight,&m_TextureWidth);
// char Name[256];
// strcpy(Name,g_HomeDirectory);
// strcpy(Name,g_HomeDirectory.c_str());
// strcat(Name,"\\Data\\Structures.txt");
// if(!m_HeightMap->ReadStructureStats(Name)) { // Temp to test structure file parser.
// MessageBox(NULL,"Error parsing file.","Structures.txt",MB_OK);
@ -2432,8 +2434,8 @@ BOOL CBTEditDoc::GetFilePath(char *FilterList,char *ExtType,char *Filter,BOOL Op
strcpy(FullPath,Tmp.GetBuffer(0));
}
GetCurrentDirectory(sizeof(g_WorkDirectory),g_WorkDirectory);
DebugPrint("New working directory %s\n",g_WorkDirectory);
g_WorkDirectory = getCurrentDirectory();
DebugPrint("New working directory %s\n", g_WorkDirectory.c_str());
SetButtonLapse();
@ -5646,7 +5648,7 @@ std::string EditorDataFileName(const std::string& fileName)
// Try the data directory.
_splitpath(fileName.c_str(), Drive, Dir, FName, Ext);
sprintf(AltName, "%s\\data\\%s%s", g_HomeDirectory, FName, Ext);
snprintf(AltName, sizeof(AltName), "%s\\data\\%s%s", g_HomeDirectory.c_str(), FName, Ext);
return std::string(AltName);
}

View File

@ -45,6 +45,8 @@
#include <string>
#include <algorithm>
using std::string;
//#define MIPMAP_TILES
/*
@ -152,8 +154,8 @@ extern CUndoRedo *g_UndoRedo;
//#define TESTFLAGS // If defined then write random flags when doing flood fill.
extern char g_HomeDirectory[1024];
extern char g_WorkDirectory[1024];
extern string g_HomeDirectory;
extern string g_WorkDirectory;
extern FILE *OpenEditorFile(const char *FileName);
extern std::string EditorDataFileName(const std::string& fileName);
@ -1396,13 +1398,9 @@ Pixel CHeightMap::PixelRead(int x,int y)
BOOL CHeightMap::PixelCompare(int x,int y,DWORD Tid,DWORD Type,DWORD Flags)
{
if( (m_MapTiles[(y*m_MapWidth)+x].TMapID == Tid) &&
// (m_MapTiles[(y*m_MapWidth)+x].Flags & TF_TYPEMASK == Type) &&
(m_MapTiles[(y*m_MapWidth)+x].Flags & TF_TEXTUREMASK == Flags) ) {
return TRUE;
}
return FALSE;
return (m_MapTiles[y * m_MapWidth + x].TMapID == Tid
// && (m_MapTiles[y * m_MapWidth + x].Flags & TF_TYPEMASK) == Type)
&& (m_MapTiles[y * m_MapWidth + x].Flags & TF_TEXTUREMASK) == Flags);
}
@ -3507,7 +3505,7 @@ _inline float SafeDivide(float a, float b)
}
BOOL CHeightMap::ReadFeatureStats(char *ScriptFile,char *IMDDir,char *TextDir)
BOOL CHeightMap::ReadFeatureStats(const char *ScriptFile, const char* IMDDir, const char* TextDir)
{
BOOL Flanged = TRUE;
BOOL TileSnap = TRUE;
@ -3550,9 +3548,8 @@ BOOL CHeightMap::ReadFeatureStats(char *ScriptFile,char *IMDDir,char *TextDir)
// DebugPrint("%s\n",m_Features[j].featureName);
char Name[256];
sprintf(Name,"%s\\%s%s",g_WorkDirectory,IMDDir,m_Features[j].IMDName);
if(!ReadIMD(Name,m_Features[j].featureName,TextDir,IMD_FEATURE,Flanged,TileSnap,ColourIndex,ShadeMode,j)) {
const string Name = g_WorkDirectory + "\\" + string(IMDDir) + string(m_Features[j].IMDName);
if(!ReadIMD(Name.c_str(), m_Features[j].featureName, TextDir, IMD_FEATURE, Flanged, TileSnap, ColourIndex, ShadeMode, j)) {
char String[256];
sprintf(String,"Feature : %s\nObject : %s",m_Features[j].featureName,Name);
MessageBox(NULL,String,"Error reading file",MB_OK);
@ -3565,7 +3562,7 @@ BOOL CHeightMap::ReadFeatureStats(char *ScriptFile,char *IMDDir,char *TextDir)
}
BOOL CHeightMap::ReadStructureStats(char *ScriptFile,char *IMDDir,char *TextDir)
BOOL CHeightMap::ReadStructureStats(const char* ScriptFile,char *IMDDir,char *TextDir)
{
BOOL Flanged = TRUE;
BOOL TileSnap = TRUE;
@ -3661,9 +3658,9 @@ BOOL CHeightMap::ReadStructureStats(char *ScriptFile,char *IMDDir,char *TextDir)
Parser.Parse(m_Structures[j].IMDName[i],sizeof(m_Structures[j].IMDName[i]));
// DebugPrint("%s\n",m_Structures[j].IMDName[i]);
char Name[256];
sprintf(Name,"%s\\%s%s",g_WorkDirectory,IMDDir,m_Structures[j].IMDName[i]);
if(!ReadIMD(Name,m_Structures[j].StructureName,TextDir,IMD_STRUCTURE,Flanged,TileSnap,ColourIndex,ShadeMode,j,i)) {
const string Name = g_WorkDirectory + "\\" + string(IMDDir) + string(m_Structures[j].IMDName[i]);
if(!ReadIMD(Name.c_str(), m_Structures[j].StructureName, TextDir, IMD_STRUCTURE, Flanged, TileSnap, ColourIndex, ShadeMode, j, i))
{
char String[256];
sprintf(String,"Structure : %s\nObject : %s",m_Structures[j].StructureName,Name);
MessageBox(NULL,String,"Error reading file",MB_OK);
@ -3676,11 +3673,12 @@ BOOL CHeightMap::ReadStructureStats(char *ScriptFile,char *IMDDir,char *TextDir)
Parser.Parse(m_Structures[j].BaseIMD,sizeof(m_Structures[j].BaseIMD));
// DebugPrint("%s\n",m_Structures[j].BaseIMD);
if(strlen(m_Structures[j].BaseIMD) > 1) {
char Name[256];
sprintf(Name,"%s\\%s%s",g_WorkDirectory,IMDDir,m_Structures[j].BaseIMD);
if(!ReadIMD(Name,m_Structures[j].StructureName,TextDir,IMD_STRUCTURE,
Flanged,TileSnap,ColourIndex,ShadeMode,j,0,&BasePlate)) {
if(strlen(m_Structures[j].BaseIMD) > 1)
{
const string Name = g_WorkDirectory + "\\" + string(IMDDir) + string(m_Structures[j].BaseIMD);
if (!ReadIMD(Name.c_str(), m_Structures[j].StructureName, TextDir, IMD_STRUCTURE,
Flanged, TileSnap, ColourIndex, ShadeMode, j, 0, &BasePlate))
{
char String[256];
sprintf(String,"Structure : %s\nObject : %s",m_Structures[j].StructureName,Name);
MessageBox(NULL,String,"Error reading file",MB_OK);
@ -3748,7 +3746,7 @@ TECH_LEVEL CHeightMap::SetTechLevel(char *pLevel)
BOOL CHeightMap::ReadTemplateStats(char *ScriptFile,char *IMDDir,char *TextDir)
BOOL CHeightMap::ReadTemplateStats(const char* ScriptFile, char* IMDDir, char* TextDir)
{
char String[256];
BOOL Ok = TRUE;
@ -3787,9 +3785,9 @@ BOOL CHeightMap::ReadTemplateStats(char *ScriptFile,char *IMDDir,char *TextDir)
// DebugPrint("Bang\n");
// }
char Name[256];
sprintf(Name,"%s\\%s%s",g_HomeDirectory,"Data\\","Icon.PIE");
if(!ReadIMD(Name,m_Templates[i].Name,TextDir,IMD_DROID,FALSE,FALSE,0,NT_SMOOTHNORMALS,i)) {
string Name = g_HomeDirectory + "\\Data\\Icon.PIE";
if(!ReadIMD(Name.c_str(), m_Templates[i].Name, TextDir, IMD_DROID, FALSE, FALSE, 0, NT_SMOOTHNORMALS, i))
{
char String[256];
sprintf(String,"Template : %s\nObject : %s",Name,"Icon.PIE");
MessageBox(NULL,String,"Error reading file",MB_OK);
@ -3985,7 +3983,6 @@ BOOL CHeightMap::ReadMisc(fileParser& Parser,char *Begin,char *End)
BOOL CHeightMap::ReadFeatures(fileParser& Parser,char *Begin,char *End)
{
char String[256];
char Name[256];
char StatsDir[256];
char IMDDir[256];
char TextDir[256];
@ -4017,14 +4014,16 @@ BOOL CHeightMap::ReadFeatures(fileParser& Parser,char *Begin,char *End)
return FALSE;
}
while(1) {
while(1)
{
Parser.Parse(String,sizeof(String));
if(strcmp(String,End) == 0) break;
sprintf(Name,"%s\\%s%s",g_WorkDirectory,StatsDir,String);
if(!ReadFeatureStats(Name,IMDDir,TextDir)) {
DebugPrint("Error loading IMD %s\n",Name);
MessageBox(NULL,"Error reading IMD",Name,MB_OK);
const string Name = g_WorkDirectory + "\\" + string(StatsDir) + string(String);
if (!ReadFeatureStats(Name.c_str(), IMDDir, TextDir))
{
DebugPrint("Error loading IMD %s\n", Name.c_str());
MessageBox(NULL, "Error reading IMD", Name.c_str(), MB_OK);
return FALSE;
}
}
@ -4036,7 +4035,6 @@ BOOL CHeightMap::ReadFeatures(fileParser& Parser,char *Begin,char *End)
BOOL CHeightMap::ReadStructures(fileParser& Parser,char *Begin,char *End)
{
char String[256];
char Name[256];
char StatsDir[256];
char IMDDir[256];
char TextDir[256];
@ -4072,10 +4070,11 @@ BOOL CHeightMap::ReadStructures(fileParser& Parser,char *Begin,char *End)
Parser.Parse(String,sizeof(String));
if(strcmp(String,End) == 0) break;
sprintf(Name,"%s\\%s%s",g_WorkDirectory,StatsDir,String);
if(!ReadStructureStats(Name,IMDDir,TextDir)) {
DebugPrint("Error loading IMD %s\n",Name);
MessageBox(NULL,"Error reading IMD",Name,MB_OK);
const string Name = g_WorkDirectory + "\\" + string(StatsDir) + string(String);
if (!ReadStructureStats(Name.c_str(), IMDDir, TextDir))
{
DebugPrint("Error loading IMD %s\n", Name.c_str());
MessageBox(NULL,"Error reading IMD", Name.c_str(), MB_OK);
return FALSE;
}
}
@ -4087,7 +4086,6 @@ BOOL CHeightMap::ReadStructures(fileParser& Parser,char *Begin,char *End)
BOOL CHeightMap::ReadTemplates(fileParser& Parser,char *Begin,char *End)
{
char String[256];
char Name[256];
char StatsDir[256];
char IMDDir[256];
char TextDir[256];
@ -4123,10 +4121,11 @@ BOOL CHeightMap::ReadTemplates(fileParser& Parser,char *Begin,char *End)
Parser.Parse(String,sizeof(String));
if(strcmp(String,End) == 0) break;
sprintf(Name,"%s\\%s%s",g_WorkDirectory,StatsDir,String);
if(!ReadTemplateStats(Name,IMDDir,TextDir)) {
DebugPrint("Error loading IMD %s\n",Name);
MessageBox(NULL,"Error reading IMD",Name,MB_OK);
const string Name = g_WorkDirectory + "\\" + string(StatsDir) + string(String);
if(!ReadTemplateStats(Name.c_str(), IMDDir, TextDir))
{
DebugPrint("Error loading IMD %s\n", Name.c_str());
MessageBox(NULL,"Error reading IMD", Name.c_str(), MB_OK);
return FALSE;
}
}
@ -4139,7 +4138,6 @@ BOOL CHeightMap::ReadTemplates(fileParser& Parser,char *Begin,char *End)
BOOL CHeightMap::ReadObjects(fileParser& Parser,char *Begin,char *End,int TypeID)
{
char String[256];
char Name[256];
char Description[256];
char Type[256];
char StatsDir[256];
@ -4231,18 +4229,17 @@ BOOL CHeightMap::ReadObjects(fileParser& Parser,char *Begin,char *End,int TypeID
return FALSE;
}
// char Name[256];
sprintf(Name,"%s\\%s%s",g_WorkDirectory,IMDDir,String);
// strcpy(Name,g_HomeDirectory);
// strcat(Name,"\\Data\\");
// strcat(Name,String);
char *Desc = NULL;
if(_stricmp(Description,"NONAME")) {
Desc = Description;
}
if(!ReadIMD(Name,Desc,TextDir,TypeID,Flanged,TileSnap,ColourIndex,ShadeMode)) {
DebugPrint("Error loading IMD %s\n",Name);
MessageBox(NULL,"Error reading IMD",Name,MB_OK);
const string Name = g_WorkDirectory + "\\" + string(IMDDir) + string(String);
if(!ReadIMD(Name.c_str(), Desc, TextDir, TypeID, Flanged, TileSnap, ColourIndex, ShadeMode))
{
DebugPrint("Error loading IMD %s\n", Name.c_str());
MessageBox(NULL,"Error reading IMD", Name.c_str(), MB_OK);
return FALSE;
}
NumObjects++;
@ -4258,7 +4255,7 @@ BOOL CHeightMap::ReadObjects(fileParser& Parser,char *Begin,char *End,int TypeID
//
// Returns TRUE if succesfull.
//
BOOL CHeightMap::ReadIMD(char *FileName,char *Description,char *TextDir,int TypeID,BOOL Flanged,BOOL Snap,int ColourKeyIndex,NORMALTYPE NType,
BOOL CHeightMap::ReadIMD(const char* FileName,char *Description, const char* TextDir,int TypeID,BOOL Flanged,BOOL Snap,int ColourKeyIndex,NORMALTYPE NType,
int StructureIndex,int PlayerIndex,C3DObject *Object)
{
char Buffer[80];
@ -4359,16 +4356,16 @@ BOOL CHeightMap::ReadIMD(char *FileName,char *Description,char *TextDir,int Type
DebugPrint("## %s ## \n",texfile);
std::string TName;
string TName;
if((TextDir) && (TextDir[0] != 0))
{
TName = std::string(g_WorkDirectory) + "\\" + std::string(TextDir) + std::string(texfile);
TName = g_WorkDirectory + "\\" + string(TextDir) + string(texfile);
}
else
{
// set the path for the texture to the path used for the .IMD
TName = std::string(Drive) + std::string(Dir) + std::string(texfile);
TName = string(Drive) + string(Dir) + string(texfile);
}
// Read the specified bitmap.
@ -4379,7 +4376,7 @@ BOOL CHeightMap::ReadIMD(char *FileName,char *Description,char *TextDir,int Type
std::ifstream* PCXFile = new std::ifstream(TName.c_str(), std::ios_base::binary);
if (!PCXFile->is_open())
{
TName = std::string(Drive) + std::string(Dir) + std::string(texfile);
TName = string(Drive) + string(Dir) + string(texfile);
// Reconstruct std::ifstream object;
// A simple PCXFile->close() followed by PCXFile->open(...) should work, but doesn't
@ -6890,8 +6887,8 @@ BOOL CHeightMap::ReadObjectList(FILE *Stream)
/* 12/4/99
if(strcmp(FeatureSet,"(null)")) {
sprintf(FullPath,"%s\\%s",g_WorkDirectory,FeatureSet);
// strcpy(FullPath,g_WorkDirectory);
sprintf(FullPath,"%s\\%s",g_WorkDirectory.c_str(),FeatureSet);
// strcpy(FullPath,g_WorkDirectory.c_str());
// strcat(FullPath,"\\Data\\");
// strcat(FullPath,FeatureSet);
if(!ReadIMDObjects(FullPath)) {

View File

@ -850,10 +850,10 @@ class CHeightMap : public CChnkIO
void UpdateLargest(D3DVECTOR &v,D3DVECTOR &Largest);
// Public functions related to objects.
BOOL ReadFeatureStats(char *ScriptFile,char *IMDDir,char *TextDir);
BOOL ReadStructureStats(char *ScriptFile,char *IMDDir,char *TextDir);
BOOL ReadFeatureStats(const char* ScriptFile, const char* IMDDir, const char* TextDir);
BOOL ReadStructureStats(const char* ScriptFile, char *IMDDir,char *TextDir);
TECH_LEVEL CHeightMap::SetTechLevel(char *pLevel);
BOOL ReadTemplateStats(char *ScriptFile,char *IMDDir,char *TextDir);
BOOL ReadTemplateStats(const char* ScriptFile, char* IMDDir, char* TextDir);
BOOL ReadObjectNames(char *FileName);
int MatchObjName(char *IDString);
@ -872,7 +872,7 @@ class CHeightMap : public CChnkIO
return m_Num3DObjects;
}
BOOL ReadIMD(char *FileName,char *Description,char *TextDir,int TypeID,BOOL Flanged = FALSE,BOOL Snap = FALSE,int ColourKeyIndex = FALSE,NORMALTYPE NType = NT_DEFAULTNORMALS,
BOOL ReadIMD(const char* FileName, char *Description, const char* TextDir,int TypeID,BOOL Flanged = FALSE,BOOL Snap = FALSE,int ColourKeyIndex = FALSE,NORMALTYPE NType = NT_DEFAULTNORMALS,
int StructureIndex = 0,int PlayerIndex = 0,C3DObject *Object=NULL);
void RenderIMD(C3DObject *Object);
void RenderFlatIMD(C3DObject *Object);

View File

@ -33,7 +33,9 @@
#include <fstream>
#include <string>
extern char g_HomeDirectory[1024];
using std::string;
extern string g_HomeDirectory;
/////////////////////////////////////////////////////////////////////////////
// CTextureSelector
@ -129,7 +131,7 @@ BOOL CTextureSelector::Read(DWORD NumTextures,char **TextureList,DWORD TextureWi
{
char tmpCharArr[1024];
GetCurrentDirectory(sizeof(tmpCharArr), tmpCharArr);
std::string CurDir(tmpCharArr);
string CurDir(tmpCharArr);
CurDir += "\\";
CurDir += FileName;
MessageBox(NULL, "Error reading PCX", CurDir.c_str(), MB_OK);
@ -170,8 +172,7 @@ BOOL CTextureSelector::Read(DWORD NumTextures,char **TextureList,DWORD TextureWi
m_Height = (SpriteNum / (m_Width / m_TextureWidth)) * m_TextureHeight;
}
std::string Name(g_HomeDirectory);
Name += "\\Data\\Buttons.PCX";
string Name = g_HomeDirectory + "\\Data\\Buttons.PCX";
std::ifstream PCXFile(Name.c_str(), std::ios_base::binary);
if (!PCXFile.is_open())