' nevrax_macros_98.dsm
'
' Copyright (C) 2000-2005 Nevrax. All rights reserved.
'
' The redistribution, use and modification in source or binary forms of
' this software is subject to the conditions set forth in the copyright
' document ("Copyright") included with this distribution.
'
'------------------------------------------------------------------------------
' FILE DESCRIPTION: Nevrax Visual Studio macro file
' when Scripting.FileSystemObject cannot be instanciated
' $Id: nevrax_macros_98.dsm,v 1.21 2005/01/03 14:46:02 cado Exp $
'------------------------------------------------------------------------------


' *** NevraxNewClass ***


' NevraxInsertFileHeader
' Utility Sub for NevraxNewClass()
' Author : Olivier Cado
Sub NevraxInsertFileHeader( filename, productname )
	ActiveDocument.Selection.StartOfDocument
	ActiveDocument.Selection = _
		"/** \file " + filename + vbLf + _
		" * <File description>" + vbLf + _
		" *" + vbLf + _
		" */" + vbLf + vbLf
End Sub


Function IsUpCase( str )
	IsUpcase = ( str = Ucase(str) )
End Function


' NevraxClassNameToFileName
' Utility Function for NevraxNewClass()
' 1/08/2000 : now analyses the first character
' Author : Olivier Cado
Function NevraxClassNameToFileName( classname )
	beginningpos = 1
	first = left(classname,1)
	if ((first="C") or (first="E") or (first="I")) then
		if len(classname)>1 then
			if IsUpCase( mid(classname,2,1) ) then
				beginningpos = 2
			end if
		end if
	end if
	filename = lcase(mid(classname,beginningpos,1))
	for i = beginningpos+1 to len( classname )
		charact = mid(classname,i,1 )
		if IsUpCase( charact ) then
			if i+1 <= len( classname ) then
				if not IsUpCase( mid(classname,i+1,1) ) then
					filename = filename + "_"
				end if
			else
				filename = filename + "_"
			end if
		end if
		filename = filename + lcase(charact)
	next
	NevraxClassNameToFileName = filename
End Function


' NevraxProjectOpen
' Utility Function for NevraxNewClass()
' Author : Olivier Cado
Function NevraxProjectOpen( projname )
	found = 0
	dim proj
	for i = 1 to Projects.Count
		if Projects(i).Name = projname then
			found = i
			exit for
		end if
	next
	NevraxProjectOpen = found
End Function


' Global variable
Dim CurrentDirectoryName
Dim CurrentProgrammerName


' NevraxNewClass
' DESCRIPTION: Wizard for new class creation
' 1/08/2000 : added input boxes for directories
' 7/09/2000 : added programmer's name dialog and test for file existence (dirs & files)
' 18/09/2000 : namespace, programmer's name saved in a file, file added to the right project
' 12/10/2000 : modified output
' Weird things :
' - CreateObject( Scripting.FileSystemObject ) doesn't work on Win 98, but on 2000
' - Projects.Item( string ) doesn't work. Using number instead
' Author : Olivier Cado
Sub NevraxNewClass()

	' ** Input class name and file name
	ClassName = InputBox( "Bienvenue dans l'assistant de cr�ation de classe." + vbLf + vbLf + _
		"Nom de la nouvelle classe :", "Nouvelle classe (1)" )
	if ClassName = "" then
		Exit Sub
	end if
	Filename = NevraxClassNameToFileName( ClassName )
	' Warning: do not enter an existing filename, or MsDev will crash when attempting to save
	Filename = InputBox( "Nom de fichier sans l'extension:", "Nouvelle classe (2)", Filename )
	if Filename = "" then
		Exit Sub
	end if
	UniqueName = "NL_" + UCase( Filename ) + "_H"
	HFilename = Filename + ".h"
	CppFilename = Filename + ".cpp"

	' ** Directories (NB: input boxes cannot be canceled in this part)
	SrcDirectory = "R:\code\nel\src"
	SrcDirectory = InputBox( "R�pertoire racine (existant) des sources (.cpp)" + vbLf + "(entrer . pour un projet hors-NeL) :","Nouvelle classe (3)", SrcDirectory )
	if SrcDirectory <> "" then
		if right(SrcDirectory,1)<>"\" then
			SrcDirectory = SrcDirectory + "\"
		end if
	else
		IncDirectory = ""
	end if
	IncDirectory = "R:\code\nel\include\nel"
	IncDirectory = InputBox( "R�pertoire racine (existant) des include (.h)" + vbLf + "(ex: R:\code\nel\include\nel pour NeL ; cha�ne vide pour le m�me r�pertoire que les fichiers source ) :","Nouvelle classe (4)", IncDirectory )
	if IncDirectory = "" then
		IncDirectory = SrcDirectory
	else
		if (right(IncDirectory,1)<>"\") then
			IncDirectory = IncDirectory + "\"
		end if
	end if
	CurrentDirectoryName = InputBox( "Nom du r�pertoire de travail (ex: misc)" + vbLf + "(existant dans " + SrcDirectory + _
		" et dans " + IncDirectory + ")" + vbLf + "Ce nom restera m�moris�" + vbLf + "(cha�ne vide pour pour un projet hors-NeL).", "Nouvelle classe (5)", CurrentDirectoryName )
	if CurrentDirectoryName<>"" then
		CurrentDirectoryDir = CurrentDirectoryName + "\"
		Namesp = "NL" + ucase(CurrentDirectoryName)
	end if
	If InStr( IncDirectory, "nel" ) then
		ProdName = "NEL"
		ShortIncDir = "nel/" + CurrentDirectoryName + "/"
	else
		ProdName = "NeL Network Services" ' not Distributed Toolkit Components System anymore
	end if

	FinalCPPdir = SrcDirectory + CurrentDirectoryDir
	FinalHdir = IncDirectory + CurrentDirectoryDir
	FinalCPPfilename = FinalCPPdir + CppFilename
	FinalHfilename = FinalHdir + HFilename

	' ** Check for open project
	if CurrentDirectoryName=""	then
		AddToProject = 0
	else
		AddToProject = NevraxProjectOpen( CurrentDirectoryName )
	end if

	' ** Programmer's name
	ConfigFileName = "R:\code\tool\visual_studio_macros\nevrax_new_class.cfg"
	if CurrentProgrammerName = "" then
		CurrentProgrammerName = "Fox Mulder"
	end if
	CurrentProgrammerName = InputBox( "Votre pr�nom et votre nom (qui restera m�moris� dans un fichier) :", "Nouvelle classe (6)", CurrentProgrammerName )
	if CurrentProgrammerName = "" then
		Exit Sub
	end if

	' ** Input ancestor class name and file name
	NoAncestor = "NO BASE CLASS"
	AncClassName = InputBox( "Nom de la classe de base :", "Nouvelle classe (7)", NoAncestor )
	if AncClassName = "" then
		Exit Sub
	else
		if AncClassName = NoAncestor then
			AncClassName = ""
		else
			AncFilename = InputBox( "Nom de fichier (avec chemin) sans l'extension (ex: nel/misc/toto) :", "Nouvelle classe (8)" )
			if AncFileName = "" then
				Exit Sub
			end if
			AncHFilename = AncFilename + ".h"
		end if
	end if

	' ** Now write .cpp
	Documents.Add( "Text" )
	NevraxInsertFileHeader CppFilename, ProdName
	ActiveDocument.Selection = vbLf + "#include """ + ShortIncDir + HFilename + """" + vbLf + vbLf + vbLf
	if ( CurrentDirectoryName<>"" ) then
		ActiveDocument.Selection = "namespace " + Namesp + " {" + vbLf + vbLf + vbLf
	end if
	ActiveDocument.Selection = "/*" + vbLf + _
		" * Constructor" + vbLf + _
		" */" + vbLf + _
		ClassName + "::" + ClassName + "()" + vbLf + _
		"{" + vbLf + _
		"}" + vbLf + vbLf + vbLf
	if ( CurrentDirectoryName<>"" ) then
		ActiveDocument.Selection = "} // " + Namesp + vbLf
	end if
	' Warning: ActiveDocument.Save raises an "Unknown error" if the directory does not exist"
	ActiveDocument.Save( FinalCPPfilename )
	if AddToProject=0 then
		ActiveProject.AddFile( FinalCPPfilename )
	else
		Projects(AddToProject).AddFile( FinalCPPfilename )
	end if

	' ** Now write .h
	Documents.Add( "Text" )
	NevraxInsertFileHeader HFilename, ProdName
	ActiveDocument.Selection = vbLf + "#ifndef " + UniqueName + vbLf + _
		"#define " + UniqueName + vbLf + vbLf
	ActiveDocument.Selection = "#include ""nel/misc/types_nl.h""" + vbLf
	if AncClassName <> "" then
		ActiveDocument.Selection = "#include """ + AncHFilename + """" + vbLf
	end if
	if ( CurrentDirectoryName<>"" ) then
		ActiveDocument.Selection = vbLf + vbLf + "namespace " + Namesp + " {" + vbLf
	end if
	ActiveDocument.Selection = vbLf + vbLf + _
		"/**" + vbLf + _
		" * <Class description>" + vbLf + _
		" * \author " + CurrentProgrammerName + vbLf + _
		" * \author Nevrax France" + vbLf + _
		" * \date 2005" + vbLf + _
		" */" + vbLf + _
		"class " + ClassName
	if AncClassName <> "" then
		ActiveDocument.Selection = " : public " + AncClassName
	end if
	ActiveDocument.Selection = vbLf + _
		"{" + vbLf + _
		"public:" + vbLf + vbLf + _
		"	/// Constructor" + vbLf + _
		"	" + ClassName + "();" + vbLf + vbLf + _
		"};" + vbLf + vbLf
	if ( CurrentDirectoryName<>"" ) then
		ActiveDocument.Selection = vbLf + "} // " + Namesp + vbLf + vbLf
	end if
	ActiveDocument.Selection = vbLf + "#endif // " + UniqueName + vbLf
	ActiveDocument.Selection = vbLf + "/* End of " + HFilename + " */" + vbLf
	' Warning: ActiveDocument.Save raises an "Unknown error" if the directory does not exist"
	ActiveDocument.Save( FinalHfilename )
	if AddToProject=0 then
		ActiveProject.AddFile( FinalHfilename )
	else
		Projects(AddToProject).AddFile( FinalHfilename )
	end if

End Sub

' *** End of NevraxNewClass ***


' ** NevraxToggleHCPP

' NevraxToggleHCPP
' DESCRIPTION: Opens the .cpp or .h file (toggles) for the current document.
' NOTE: Can be used in conjunction with NevraxNewClass (NevraxToggleHCPP uses the CurrentDirectoryName global variable)
' TIP: Bind this macro to Ctrl+<
' Author : Olivier Cado
Sub NevraxToggleHCPP()

	' ** Get filename extension and ensure it is .h or .cpp
	ActFilename = ActiveDocument.FullName
	pos = InstrRev( ActFilename, "." )
	if ( pos <> 0 ) then
		Ext = mid(ActFilename,pos)		
		if (Ext<>".h" and Ext<>".cpp") then
			msgbox( "Error : Active document is not a .cpp or .h file" )
			exit sub
		end if
	else
		exit sub
	end if
	
	' ** Build the alternative filename
	NewFilename = ""
	if (InStr(ActFilename,"nel\src\")<>0) or (InStr(ActFilename,"nel\include\")<>0) then
		if Ext = ".h" then
			NewFilename = "R:\code\nel\src\"
		else
			NewFilename = "R:\code\nel\include\nel\"
		end if
		' Take the module name (the word between the two last backslashes)
		DirectoryName = left( ActFilename, InstrRev(ActFilename,"\")-1 )
		DirectoryName = mid( DirectoryName, InstrRev(DirectoryName, "\")+1 )
		' Add the rest of the path
		NewFilename = NewFilename + DirectoryName + "\" 
		ShortFilenameDot = mid( ActFilename, InstrRev(ActFilename,"\")+1 )
	else
		ShortFilenameDot = ActFilename
	end if
	ShortFilenameDot = left( ShortFilenameDot, Instr(ShortFilenameDot,".") )
	if Ext=".h" then
		Ext = "cpp"
	else
		Ext = "h"
	end if
	NewFilename = NewFilename + ShortFilenameDot + Ext
 
	' ** Open the alternative file
	Documents.Open( NewFilename )
End Sub