void WriteAxisConfigFile()

in src/configuration/FileFunctions.cpp [134:296]


void WriteAxisConfigFile( LIST * psDLLNames, int * piConfigInfoArray, CHOICELIST * psChoiceList, bool bMerge, char * pszAxisCpp_Deploy, char cSlash, bool bBackup)
{
	char	szFilename[256];

	sprintf( szFilename, "%s%caxiscpp.conf", pszAxisCpp_Deploy, cSlash);

	cout << endl << "Configuration complete." << endl << endl;

	if( bBackup)
	{
		BackUpExistingConfigFile( pszAxisCpp_Deploy, szFilename, cSlash);
	}

	if( bMerge)
	{
		char *	pszData = NULL;
		long	lFileLength = 0;
		
		if( (lFileLength = ReadFileContents( szFilename, &pszData)) > -1)
		{
			FILE *	pFile = fopen( szFilename, "w");

			if( lFileLength < 5)
			{
				char	szConfigData[512];

				sprintf( szConfigData, "# This header file was created by AxisConfiguration on %s", TimeNow());

				WriteLineToFile( pFile, szConfigData);

				WriteLineToFile( pFile, "# The comment character is '#'\n");
				WriteLineToFile( pFile, "# Available directives are as follows\n");
				WriteLineToFile( pFile, "#(Some of these directives may not be implemented yet)\n");
				WriteLineToFile( pFile, "#\n");
			}

			for( int ieConfigType = eHTTPTransport; ieConfigType < eConfigMax; ieConfigType++)
			{
				int	iChoiceIndex = GetChoiceIndexForConfigType( psChoiceList, (ECONFIGTYPE) ieConfigType);

				if( iChoiceIndex != -1)
				{
					char *	psTag = strstr( pszData, psChoiceList[iChoiceIndex].pszConfigName);

					if( psTag)
					{
						char *	psTagLineStart = strchr( psTag, ':');
						char *	psTagLineEnd = psTag;

						if( psTagLineStart != NULL)
						{
							psTagLineStart++;

							while( *psTagLineEnd != '\n' &&
								   *psTagLineEnd != '\r' &&
								   *psTagLineEnd != '\0' &&
								   psTagLineEnd - pszData < lFileLength)
							{
								psTagLineEnd++;
							}

							int		iTagValueLength = (int) (psTagLineEnd - psTagLineStart);
							char *	pszTagValue = GetTagValue( psDLLNames, piConfigInfoArray, (ECONFIGTYPE) ieConfigType);

							if( pszTagValue != NULL)
							{
								if( (int) strlen( pszTagValue) == iTagValueLength)
								{
									if( StringCompare( psTagLineStart, pszTagValue))
									{
										// No change
									}
									else
									{
										memcpy( psTagLineStart, pszTagValue, iTagValueLength);
									}
								}
								else if( (int) strlen( pszTagValue) > iTagValueLength)
								{
									// New tag value is longer than current tag.
									int	iTagValueLengthDiff = (int) strlen( pszTagValue) - iTagValueLength;
									int	iTagValueOffset = (int) (psTagLineStart - pszData);

									pszData = (char *) realloc( pszData, strlen( pszData) + iTagValueLengthDiff + 1);

									memmove( pszData + iTagValueOffset + iTagValueLength + iTagValueLengthDiff,
										     pszData + iTagValueOffset + iTagValueLength,
											 strlen( pszData + iTagValueOffset + iTagValueLength) + 1);
									memcpy( pszData + iTagValueOffset,
											pszTagValue,
											strlen( pszTagValue));
								}
								else
								{
									// New tag value is shorter than current tag.
									int	iTagValueLengthDiff = iTagValueLength - (int) strlen( pszTagValue);
									int	iTagValueOffset = (int) (psTagLineStart - pszData);

									memmove( pszData + iTagValueOffset + iTagValueLength - iTagValueLengthDiff,
											 pszData + iTagValueOffset + iTagValueLength,
											 strlen( pszData + iTagValueOffset + iTagValueLength) + 1);
									memcpy( pszData + iTagValueOffset,
											pszTagValue,
											strlen( pszTagValue));
								}
							}
						}
					}
					else
					{
						WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, (ECONFIGTYPE) ieConfigType));
					}
				}
				else
				{
					// No choice information
				}
			}
		
		WriteLineToFile( pFile, pszData);

		free( pszData);

		fclose( pFile);
		}
		else
		{
			cout << "Error - The existing configuration file (" << szFilename << ")" << endl
				<< "        could not be read/found.  The new configuration data has" << endl
				<< "        not been written." << endl;
		}
	}
	else
	{
		FILE *		pFile = fopen( szFilename, "w");
		char		szConfigData[512];

		sprintf( szConfigData, "# This header file was created by AxisConfiguration on %s", TimeNow());

		WriteLineToFile( pFile, szConfigData);

		WriteLineToFile( pFile, "# The comment character is '#'\n");
		WriteLineToFile( pFile, "# Available directives are as follows\n");
		WriteLineToFile( pFile, "#(Some of these directives may not be implemented yet)\n");
		WriteLineToFile( pFile, "#\n");
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eServerLog));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eServerWSDD));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eClientLog));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eClientWSDD));
		WriteLineToFile( pFile, "#Node name.\n");
		WriteLineToFile( pFile, "#NodeName: <not set>\n\n");
		WriteLineToFile( pFile, "#Listening port.\n");
		WriteLineToFile( pFile, "#ListenPort: <not set>\n\n");
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eHTTPTransport));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eSMTPTransport));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eXMLParser));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eHTTPChannel));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eHTTPSSLChannel));
		WriteLineToFile( pFile, CreateConfigElement( psDLLNames, piConfigInfoArray, psChoiceList, eSSLOptions));

		fclose( pFile);
	}
}