Re: MealMaster: Decoding text file format to database
Posted: Sun Jan 06, 2013 9:42 pm
Moderator, Please remove this post, outdated.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
If Len(Line.s) = 5 And (Line.s = "MMMMM" Or Line.s = "-----")
Code: Select all
If Line.s = "MMMMM" Or Line.s = "-----"
Code: Select all
Debug "----------**********//////////\\\\\\\\\\**********----------"
;CloseFile(RecipeFileHandle.i )
;ProcedureReturn
State = #RecipeNone
I do want it to go through the whole file. It's doing that but not ending when it has finished and I don't know why.to get just one recipe. Other wise it will run thru the whole file.
That was me, found it on a free icon site when I first d/l the program, been meaning to upload it, but kept forgetting....Fangbeast wrote:Whoever uploaded the icon, it looks good even in 48x48 view on my desktop, thanks for that.
Code: Select all
Enumeration
#RecipeNone
#RecipeVersion
#RecipeCategory
#RecipeServes
#RecipeIngredients
#RecipeInstructions
EndEnumeration
; Get and display recipe details when a title is clicked on
Procedure ImportMealMaster()
;
Protected.i Result.i, File.i, State.i, Pos.i, Count, i
Protected Line.s, Text.s, Version.s, Title.s, Category.s, Yield.s, Ingredients.s, Method.s
;
RecipeFileName.s = OpenFileRequester("Choose a Meal-Master file", "", "MealMasterFile|*.mmf|TextFile|*.txt|All|*.*", 1)
;
If RecipeFileName.s <> ""
;
RecipeFileHandle.i = ReadFile(#PB_Any, RecipeFileName.s)
;
If RecipeFileHandle.i
;
While Not Eof(RecipeFileHandle.i)
; Read a line of text from the file
Line.s = ReadString(RecipeFileHandle.i)
; Check which part of the file we are processing
Select State
; No recipe found yet
Case #RecipeNone
Pos = FindString(Line.s, "Meal-Master")
If Pos
Version.s = Trim(Mid(Line.s, Pos + 11))
For i = 1 To Len(Version)
If ValF(Mid(Version, i)) > 0
Version = Mid(Version, i)
Break
EndIf
Next i
Title.s = ""
Category.s = ""
Yield.s = ""
Ingredients.s = ""
Method.s = ""
State = #RecipeVersion
EndIf
; Recipe version number found
Case #RecipeVersion
Pos = FindString(Line.s, "Title:")
If Pos
Title.s = Trim(Mid(Line.s, Pos + 7))
State = #RecipeCategory
EndIf
; Recipe category found
Case #RecipeCategory
Pos = FindString(Line.s, "Categories:")
If Pos
Category.s = Trim(Mid(Line.s, Pos + 12))
State = #RecipeServes
EndIf
; Recipe yield found
Case #RecipeServes
Count = Len(Line.s)
For i = 1 To Count
If Val(Mid(Line.s, i)) > 0
Yield.s = Trim(Mid(Line.s, i))
State = #RecipeIngredients
Break
EndIf
Next i
; Recipe ingredients found
Case #RecipeIngredients
If Trim(Line) <> ""
If Val(Trim(Line.s)) > 0
Line.s = Trim(Line.s)
Pos = FindString(Line.s, " ")
If Pos
Ingredients.s + Trim(Left(Line.s, Pos)) + " "
Ingredients.s + Trim(Mid(Line.s, Pos)) + #LF$
Else
Ingredients.s + Trim(Line.s) + #LF$
EndIf
Else
If Trim(Left(Line.s, 3)) <> ""
If Left(Line.s, 5) = "MMMMM"
Ingredients.s + Trim(Mid(Line.s, 6), "-") + #LF$
Else
Method.s = Line.s + #LF$
State = #RecipeInstructions
EndIf
Else
Line.s = Trim(Line.s)
If Left(Line.s, 1) = "-"
Ingredients.s = Left(Ingredients.s, Len(Ingredients.s) - 2) + " " + Trim(Mid(Line.s, Pos), "-") + #LF$
Else
Ingredients.s + Trim(Mid(Line.s, Pos), "-") + #LF$
EndIf
EndIf
EndIf
EndIf
; Recipe method found
Case #RecipeInstructions
If Len(Line.s) = 5 And (Line.s = "MMMMM" Or Line.s = "-----")
;Debug Version.s
;Debug Title.s
;Debug Category.s
;Debug Ingredients.s
;Debug Yield.s
;Debug Method.s
;Debug "----------**********//////////\\\\\\\\\\**********----------"
RecipeCounter + 1
Debug Str(RecipeCounter.i) + " Recipes"
State = #RecipeNone
Else
If Len(Line.s) <> 0
Method.s + Line.s + #LF$
EndIf
EndIf
; No more headings to see
EndSelect
; Continue processing the current recipe until the end marker is found
Wend
CloseFile(RecipeFileHandle.i)
Else
Debug "File wasn't opened slughead, no file handle"
EndIf
Else
Debug "User cancelled the file open operation"
EndIf
EndProcedure
ImportMealMaster()
No worries mate. I ended up registering an account with Box and downloaded your code.Fangbeast wrote:@Lost I just had a quick look at BitBucket and it's not for me. Way, way too complicated for my life!!!
@Everyone else. I have uploaded the latest version 'RecipeMuncher0.00b.7z' to Box.com, same link as before.