Link txt files

BlitzMax Forums/BlitzMax Programming/Link txt files

Yue(Posted May) [#1]
Hi, how can I merge multiple files into one. For example have.
Main.txt
Variables.txt
Functions.txt

And have a single file linking those three, for example.
ScriptMain.txt


Grisu(Posted May) [#2]
How about zipping them as "ScriptMain.txt"?

Or write some code that extracts the data from the three files and puts it all into one file with sections: [Main], [Var] and [Func]?

There are tons of options depending on what you need.


Yue(Posted May) [#3]
@grisu


The idea is that I have several txt files that are script, but what I need is to create a single file that contains the contents of all the script files.

For example I have
Main.txt.
Variables.txt.

Then there would be a single file for example start.txt which contains the contents of main.txt and variables.txt.


Rooster(Posted May) [#4]
The way I would do it is by putting the length of each part at the start of the file.


Yue(Posted May) [#5]

// file 1
int Cube;


// File 2 Script.
int Update()
{

   TurnEntity ( Cube, 0.0f, 1.0f, 0.0f, 0 );


}


Only File.txt
// file 1
int Cube;




int Update()
{

   TurnEntity ( Cube, 0.0f, 1.0f, 0.0f, 0 );


}


How can I do this? Any simple example?


Grisu(Posted May) [#6]
I used the filenames you gave above. This example also writes a header, so you can read out each part later.

Example code V1:


Or is this too simple?


Yue(Posted May) [#7]
@grisu

Error here, return Identifer 'Tfielsteram' Not found.


Grisu(Posted May) [#8]
Change to "TStream". Typo when I made the code "SuperStrict". Fixed my example. Sorry!


Yue(Posted May) [#9]



[Main]

void start()

{

		

	

	



	xAppTitle ("APP");

	xGraphics3D(800, 600, 32, 0, 1);

	

		int camara = xCreateCamera(0);

		int sol = xCreateLight(0);

		int cubo = xCreateCube(0);



		xPositionEntity ( cubo, 0, 0, 10,0 );	

		

	while (!xKeyDown(1))

	{

		

		info();

		xUpdateWorld(1.0f);

		xTurnEntity (cubo, 1, 1, 1, 0);

		xRenderWorld(1.0f, 0 );

		xFlip();



	}



	

	

	

}





void info()

{





	xAppTitle("HOLA");





}
[Variables]
void variables()

{







}






[Functions]
void Function()

{







}




I greatly appreciate the help.


You think you can compile all the txt in a single file that is found for example in a directory called Scripts.


TomToad(Posted May) [#10]
SuperStrict
Local File:String = LoadText("Main.txt")+LoadText("Variables.txt")+LoadText("Functions.txt")
SaveText(File,"ScriptMain.txt")



Yue(Posted May) [#11]
@TomToad

oO??


Can you go through a directory for example called Scripts, to join all the txt files in one?


TomToad(Posted May) [#12]
To combine all .txt files together (untested)
SuperStrict
Local dirHandle:Int = ReadDir("Scripts")
If Not dirHandle Then RuntimeError("Cannot read from directory ~qScripts~q")

Local Script:String = ""
Repeat
	Local File:String = NextFile(dirHandle)
	If File = "" Then Exit
	If ExtractExt(File) <> "txt" Then Continue
	Script :+ LoadText(File)
Forever

SaveText(Script,"Scripts\ScriptMain.txt")



Yue(Posted May) [#13]
TomToad, here return Unhandled Exception: Error reading froms tream.


Script :+ LoadText(File)<<


Grisu(Posted May) [#14]
Here's my little code.
It scans for "scr" files inside the script dir.

Example code V2:
' Grisu - Three to One V2

SuperStrict 

Const SCRIPTS_DIR:String   = "scripts\"

Global file:TStream

' Open merged file for writing...
file:TStream=WriteFile("Merged.txt")
If Not file RuntimeError "Error writing file!" 


' Find all files inside the folder "scripts"...
Local dir_scripts:String[]=LoadDir(SCRIPTS_DIR,True)
Local temp:String 
Local i:Int

' Select only the ones with the extension "scr"	
For temp$=EachIn dir_scripts

   	If ExtractExt(temp$) = "scr" Then 

      ' Header for current file  
       WriteLine file, "/// "+temp$+" ///"

      ' Read data into temp array 
       Local Array:String[]=LoadText(SCRIPTS_DIR+temp$).split("~n")
       Local line_counter:Int=Array.length-1

       For i:Int=0 To line_counter
         WriteLine file, Array[i]
       Next 

     Else 
      ' Not a script file!
     EndIf
Next ' dir_scripts

CloseStream file ' Close merged file
End 


As I said before, you have tons of options. :)


Grisu(Posted May) [#15]
As I don't know, if you need to count the lines of the files or need some sort of header structure... here's another version:

Example code V3:
' Grisu - Three to One V3

SuperStrict 

Const SCRIPTS_DIR:String   = "scripts\"

Global Array:String
Global file:TStream

' Find all files inside the folder "scripts"...
Local dir_scripts:String[]=LoadDir(SCRIPTS_DIR,True)
Local temp:String 

For temp$=EachIn dir_scripts

' Select only the ones with the extension "scr"	
   	If ExtractExt(temp$) = "scr" Then 

      ' Read data into one temp string 
       Array=Array+LoadText(SCRIPTS_DIR+temp$)

     Else 
      ' Not a script file!
     EndIf
Next ' dir_scripts

' Save all in one file
SaveText(Array, "Merged_scripts.txt")
End 



Yue(Posted May) [#16]
@Grisu

It's amazing, thank you very much, the second example works fine, the third one only creates the file, but it does not contain anything inside it.


The idea of all this is that I am using angelscript for my robot project where the user can implement script to modify the behavior of the game, and angelscript from my ignorance does not have a directive #include.

However I am very happy, because if you can have an alternative.


Yue(Posted May) [#17]
Ok, work here, i change ext file. :)


Grisu(Posted May) [#18]
Strange, that the third one isn't working for you. Does on my machine. Just retested it. The second version is a bit slower as it saves the data line by line to the merged file.

Glad that I could help. And I was faster than Brucey or Skid! :D

P.S.: Buenas noches! Will get some sleep now.


Yue(Posted May) [#19]
Ok, Thanks You, here run perfect. :) Buenas noches!!


degac(Posted May) [#20]
Quick question (after reading all the post).
What if the 'order' of linking the single .txt files is important?

I mean, maybe you need to setup variables FIRST than calling a function etc


Yue(Posted May) [#21]
Hi, degac, it's a very good question. In the case that the variables, constants, functions must be left up first, for later the functions of entry to the script. They are many things that can be done. Although today in the work it is possible to create a label that allows to use a directive to include something new to establish the order of the union of the files.


TomToad(Posted May) [#22]
AngelScript does have an #Include directive. You need to pass the script to the Script Builder add-on which will do the pre-processing for you. I'm not sure what you are using to interface AS with BlitzMax, so I can't help you there, but the explanation and c++ example is in the docs. http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_build.html


Yue(Posted May) [#23]
@TomToad

Currently I only use notepad, where the mod for script angel loads the script, reads it and executes it.

But I can not figure out how to do the #Include directive.

Code mod SCript Angel.


Some functions at first sight by name can understand them, but others do not.