Templates.prefs generator

Working on new editor enhancements?
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Templates.prefs generator

Post by AZJIO »

You can create Templates.prefs using this code. You need to choose a folder with sources. Only a two -level tree is created, that is, in the folder you should have folders with sources. The sources located in the root catalog or at the third level are not taken into account.

Image

Code: Select all

; AZJIO (2025.04.09)

EnableExplicit

Procedure FileSearch(List Files.s(), dir.s, mask.s = "", depth = 130)
	Protected Name.s, c
	Protected Dim hDir(depth)
	Protected Dim SearchPath.s(depth)

	If Right(dir, 1) <> #PS$
		dir + #PS$
	EndIf

	SearchPath(c) = dir
	hDir(c) = ExamineDirectory(#PB_Any, dir, "*")
	If Not hDir(c)
		ProcedureReturn
	EndIf

	Repeat
		While NextDirectoryEntry(hDir(c))
			Name = DirectoryEntryName(hDir(c))
			If Name = "." Or Name = ".."
				Continue
			EndIf
			If DirectoryEntryType(hDir(c)) = #PB_DirectoryEntry_Directory
				If c >= depth
					Continue
				EndIf
				dir = SearchPath(c)
				c + 1
				SearchPath(c) = dir + Name + #PS$
				hDir(c) = ExamineDirectory(#PB_Any, SearchPath(c), "*")
				If Not hDir(c)
					c - 1
				EndIf
			Else
				If c And GetExtensionPart(Name) = mask And AddElement(Files())
					Files() = SearchPath(c) + Name
				EndIf
			EndIf
		Wend
		FinishDirectory(hDir(c))
		c - 1
	Until c < 0
EndProcedure


Procedure.s ReadFileToVar(Path$)
	Protected id_file, Format, Text$
	
	id_file = ReadFile(#PB_Any, Path$)
	If id_file
		Format = ReadStringFormat(id_file)
		Text$ = ReadString(id_file, Format | #PB_File_IgnoreEOL)
		; 	Text$ = ReadString(id_file, #PB_UTF8 | #PB_File_IgnoreEOL)
		Text$ = ReplaceString(Text$, #CRLF$, "\n")
		CloseFile(id_file)
	EndIf
	
	ProcedureReturn Text$
EndProcedure


Procedure SaveFile(File.s, Text.s)
	Protected Result = #False
	Protected ID = CreateFile(#PB_Any, File)
	If ID
; 		WriteStringFormat(ID, #PB_UTF8)
		Result = WriteString(ID, Text, #PB_UTF8)
		CloseFile(ID)
	EndIf
	ProcedureReturn Result
EndProcedure



Define Templates_prefs$ = "TEMPLATES:1.0" + #CRLF$ + #CRLF$ + "Directory: Examples" + #CRLF$ + "Expanded" + #CRLF$
Define lengthpath, Dir$, Templates_prefs$, indent$, folder$, tmp$, name_f$, folderOld$ ; , filename$
Define indent$ = "  "
Define NewList Files.s()

Dir$ = PathRequester("", GetPathPart(ProgramFilename()))
; Dir$ = "C:\PB\Source\CodeArchive\WinAPI_Library\"
If Not Asc(Dir$)
	End
EndIf

FileSearch(Files(), Dir$, "pb", 1)
If Not ListSize(Files())
	MessageRequester("Error", "Files not found")
	End
EndIf
SortList(Files(), #PB_Sort_Ascending)
lengthpath = Len(Dir$) + 1

FirstElement(Files()) 
folder$ = RTrim(GetPathPart(Mid(Files(), lengthpath)), #PS$)		; get the first folder (получить первую папку)
folderOld$ = folder$
Templates_prefs$ + indent$ + "Directory: " + folder$ + #CRLF$
ForEach Files()
	tmp$ = Mid(Files(), lengthpath) ; folder\ + file (папка\ + файл)
	folder$ = RTrim(GetPathPart(tmp$), #PS$)		; folder (папка)
	name_f$ = GetFilePart(tmp$, #PB_FileSystem_NoExtension) ; File without extension (файл без расширения)
; 	filename$ = GetFilePart(tmp$)	; файл
	
	If folder$ <> folderOld$
		Templates_prefs$ + indent$ + "CloseDirectory" + #CRLF$
		Templates_prefs$ + indent$ + "Directory: " + folder$ + #CRLF$
	EndIf
	folderOld$ = folder$
	
	Templates_prefs$ + indent$ + "  Template: " + name_f$ + #CRLF$
	Templates_prefs$ + indent$ + "    Comment: " + name_f$ + #CRLF$
	Templates_prefs$ + indent$ + "    Code: " + ReadFileToVar(Files()) + #CRLF$
Next
Templates_prefs$ + indent$ + "CloseDirectory" + #CRLF$
Templates_prefs$ + "CloseDirectory" + #CRLF$

SaveFile(GetPathPart(ProgramFilename()) + "Templates.prefs", Templates_prefs$)
MessageRequester("Done", "Done")
End