numpty needs help! loading text files

BlitzMax Forums/BlitzMax Beginners Area/numpty needs help! loading text files

Walla(Posted 2010) [#1]
Hello folks.

I have not used blitzmax for a long time and I am very rusty. I am also having some health problems at the moment which is very annoying and causes me trouble with concentration span.

I want to remake a little tool as a small project to get myself going with. The process of it I can figure out, but I am flailing around with the planning/organising and what is the best way to approach it, so I wanted to see if anyone has some help for me.

The spec is this:

A set of folders exist below the exe's location. Each folder contains a bunch of text files. One text file is a sequence of strings seperated by commas or somesuch. The rest of the text files are named the same as one of the various strings in the sequence file, and contain all the different variations.

e.g.

sequence is "np,v,np",

text files exist: np.txt and v.txt.

np.txt contains "the dog"," the cat","the mat"
v.txt contains "ate","watched"

See what I want to do here?

Now this would be straightforward for me if I knew what I wanted to have in terms of the different categories etc. but I want to make it open-ended. That's the bit which bends my tiny mind in half!

Imagine you make a new folder and make a bunch of your own custom text files, how can I setup my program to scan through the folder and pickup the arbitrary number of things in there?

I hope a kind soul can help me!


Midimaster(Posted 2010) [#2]
The commands
ReadDir   ;open a existing directory
NextFile   ;crawling to this directory

...in combination with
FileType   ; detecting folders

...will help you. They can crawl trough any directory, find all files and subdirs:
SuperStrict
Global Path$="C:/Basic/"
Print Path
SearchDir(Path)

Function SearchDir(Path$)
   Local Directory%, currFile$
   Directory = ReadDir(Path)
   Repeat
      currFile=NextFile$(Directory)
      If currFile="" Then Exit
      If currFile="." Or currFile=".." Continue
      If FileType(Path + currFile) = 2 Then
         Print "****************new Directory:" + currFile
         SearchDir (Path+currFile+"/")
      Else
         Print "File:" + Path+currFile
      End If
   Forever
   CloseDir Directory
End Function


The FileType command is able to detect directories. So you have to react in the case of finding another "Directory" with a new search. The new directory becomes the new path and you can start a new search.

The results of NextFile are filenames, a folder names or empty String, when ready. The results "." or ".." are symbolic paths, that can show the "parent directory". So you are able also to search backward throught the harddisc until you reach "C:/". In your case we cancel this cases with this line...
If currFile="." Or currFile=".." Continue

..., because you are only interested in sub-folders.


degac(Posted 2010) [#3]
Other useful commands are LoadDir and LoadText, as your file are plain text.


Walla(Posted 2010) [#4]
cool thanks for the help, i had a clunkier version of this working (I hadn't found the filetype command yet)

So I'm thinking I should probably create a type to represent a file, and have some methods/functions to load and parse the text file into a field of the type (an array if that can be of any length - slices I recall can do some funky stuff)

Then the main type can have a linked list to hold any number of text-files' worth of categories, as well as holding the data for the structures to be filled in from the collected components.

Hm, I will take a first stab at this approach tomorrow. I guess I should go in small steps, every time I have started to do it I get bogged down and give up!

Thanks a lot for the tips guys, I will let you know how I go.


Walla(Posted 2010) [#5]
ok so i am starting in at the deep end. i will post my messy code so if there is anything stupid let me know

it doesnt work yet and theres bound to be load of mistakes but somehow i need some sort of backbone there before i can start fleshing it out to actually do anything - lol - especially when a lot of this gubbins is unfamiliar turf.

thanks for your helps so far - now, please don't laugh at the noob -

SuperStrict
'3ch

'######################################################################

Type element

	Field name$
	Field path$
	Field items:TList = CreateList()

	Function setup:element(_path$, _name$)
		Local a:element = New element
		a.name$ = _name$
		a.path$ = _path$
		Return a
	End Function

	Method e_load()
		'somehow parse the element list into a new element
		Local file:TStream=OpenFile(Self.path$)
		While Not Eof
			Local s$ = ReadLine(file)
			Self.items.addlast(s$)
		Wend
	End Method

End Type

Type theme
	Field name$
	Field element_bag:TList	
	Field structures:TList
	
	Function setup:theme(path$)
		Local this:theme = New theme
		this.name = path$
		this.element_bag = CreateList()
		this.structures = CreateList()
		
		'we go into the path and chomp through the text files inside:
		Local thedir% = ReadDir(path$)
		Repeat
			Local currFile$=NextFile$(thedir)
		      If currFile="" Then Exit
		      If currFile="." Or currFile=".." Continue
		      If FileType(currFile) = 2 Then
				
		      Else
				' we have a file.
				'is it the structure file or a new element?  handle differently
				If currfile = "structure.txt"
					'somehow parse the structure file into the theme data
				Else
					Local ne:element = element.setup((currFile$),(currFile$))
					this.element_bag.addlast(ne)
				EndIf
		      End If
	  	Forever
		Return this
	End Function
End Type


Type topicgen
	Global basedir$ = CurrentDir()
	Field output:String
	
	Function setup:topicgen()
		Print "topicgen setup:"
		Local this:topicgen = New topicgen
		this.populate(Self.basedir)
		Return this
	End Function
	
	Method populate(_path$)
		Print "topicgen populate:"
		Local thedir% = ReadDir(_path$)
	 	Repeat
	
		      Local currFile$=NextFile$(thedir)
		      If currFile="" Then Exit
		      If currFile="." Or currFile=".." Continue
		      If FileType(currFile) = 2 Then
		      	'Print "directory: "+_path$+"/"+currfile
				'we go into the folder, this is one theme
				'
				theme.setup(_path$+"/"+currfile)
		      Else
		         'Print "File:" + Path+currFile
		      End If
	  	Forever

		' there is 1 special case,m a structures.txt file which defines the patterns of elements, we will read this first and error if it is not present.
		
		' crawl directory, for each text file create a new element and parse the file's contents into the object's fields
	End Method

	Method get_topics:String(n:Int)
		Local topics$ = "new topic: "
		' build new topic via rules
		Return topics$
	End Method
End Type

Global tg:topicgen = topicgen.setup()

For Local i:Int = 1 To 10 
	Local nt$ = tg.get_topics(1)
	Print nt$
Next




Walla(Posted 2010) [#6]
yeah i have now got it va.lidating the folders and hoovering up relevant stuff. i think i can do this now, thanks again for the assistances! very helpful getting my head around things.

i forgot how fun blitzmax can be, so modular and tidy. :D


Walla(Posted 2010) [#7]
i have it working now, thanks so much for your helps!