Prefixing #defined string with variable.

I have inherited a legacy (1990s) Visual C++ (that is really just C) suite of programs that contain loads of defines (in #include files) such as

#define DiagnosticsLogFile "C:\\FEP\\LOGS\\DIAGNOSTICS_LOG.LOG"

these are then used in the code such as

bSuccess = m_ErrorFile.Open ( DiagnosticsLogFile , TRUE ) ;

My problem is that I have to make the code now run on another disk as our standards require that only the Operating System is on C:

and the applications have to be on another (unspecified) drive.

So as to keep code changes to a minimum, I would like to change the defines to something like

#define DiagnosticsLogFile prepend_disk(":\\FEP\\LOGS\\DIAGNOSTICS_LOG.LOG")

As this is running as a service, I am able to get the disk letter from the Registry, and store it
into a static variable. I am not sure how to do the prepend_disk bit.

I have tried,

char [] prepend_disk (char* path)
{

char myPath [1024];
memset (myPath, '\0', sizeof (myPath));
//
// If this is the first time I am called, get the disk drive from the Registry
//
//Once we get going in a multi threaded environment
// EnterCriticalSection ( &m_csCrit ) ;
if (myDisk [0] == '\0')
{
read_in_drive ();
}
//Once we get going in a multi threaded environment
// LeaveCriticalSection ( &m_csCrit ) ;

myPath [0] = myDisk;
strcat (myPath, path);
return (myPath);
}

which I know is not valid. Has anyone got any suggestions? Thanks.
The #defined stuff can only be modified before compile time...
but the information you need (if I understand you correctly) is only available after compile time.

IMHO you should have a configuration file with all those paths in it that your program loads at startup. All #defined names like DiagnosticsLogFile would become actual global values -- which is fine as they are currently static global data anyway and they have the #define clobber effect.

 
char DiagnosticsLogFile[ MAX_PATH ];
1
2
3
4
the initialization code()
{
  ...
  read initialization file's value for DiagnosticsLogFile, etc 


If you aren't allowed to do that, though, just fix the #defines to concatenate with a global:

1
2
3
char PATH_PREFIX[ MAX_PATH ] = "C";

#define DiagnosticsLogFile std::strcat( (PATH_PREFIX[1]='\0', PATH_PREFIX), ":\\FEP\\LOGS\\DIAGNOSTICS_LOG.LOG" ) 
1
2
3
4
the initialization code()
{
  ...
  *PATH_PREFIX = whatever the drive letter is;

Hope this helps.
I am trying to go with the second option. The first won't work, as I also need to prefix the paths for my initialisation files. As the programs are running as services they (internally) think they are running in %WINDIR%\system32.

I do need the
 
PATH_PREFIX [ MAX_PATH ] 

variable to be accessible to all classes within the program, but I am having "fun" with the initial initialisation of the variable to a known value.
Thanks. After playing with this, I have managed to implement the second solution, and using

1
2
public:
	static char Disk::g_myDisk [4] ;


in Disk.h

and

1
2
3
4
Disk::Disk ()
{
	std::strcpy (Disk::g_myDisk, "?");
}


In the main body (and creating a new Disk object), and getting rid of the CRT_??? warnings.
Now that I understand it. I know why it has to be MAX_PATH long.
Having moved all the defines into one place, and worked out how to make the g_myDisk variable work, the software is now working on my E: drive.

Duoas, thank you so very much.
Topic archived. No new replies allowed.