Re: MealMaster: Decoding text file format to database
Posted: Tue Dec 04, 2012 12:45 pm
Hi Fangbeast,
a more tolerant import routine:It works with all versions which I have tested.
Bernd
a more tolerant import routine:
Code: Select all
Procedure.i ImportRecipe(DB.i, Filename$)
Protected.i Result, File, State, Pos, Count, i
Protected Line$, Text$, Title$, Category$, Yield$, Incredients$, Method$
File = ReadFile(#PB_Any, Filename$)
If File
While Not Eof(File)
Line$ = ReadString(File)
Select State
Case #RecipeNone
Pos = FindString(Line$, "Title:")
If Pos
Title$ = Trim(Mid(Line$, Pos + 7))
Category$ = ""
Yield$ = ""
Incredients$ = ""
Method$ = ""
State = #RecipeCategory
EndIf
Case #RecipeCategory
Pos = FindString(Line$, "Categories:")
If Pos
Category$ = Trim(Mid(Line$, Pos + 12))
State = #RecipeYield
EndIf
Case #RecipeYield
Count = Len(Line$)
For i = 1 To Count
If Val(Mid(Line$, i)) > 0
Yield$ = Trim(Mid(Line$, i))
State = #RecipeIncredients
Break
EndIf
Next i
Case #RecipeIncredients
If Val(Trim(Line$)) > 0
Line$ = Trim(Line$)
Pos = FindString(Line$, " ")
If Pos
Incredients$ + Trim(Left(Line$, Pos)) + #CRLF$
Incredients$ + Trim(Mid(Line$, Pos)) + #CRLF$
Else
Incredients$ + Trim(Line$) + #CRLF$
EndIf
Else
If Trim(Left(Line$, 3)) <> ""
If Left(Line$, 5) = "MMMMM"
Incredients$ + Trim(Mid(Line$, 6), "-") + #CRLF$
Else
Method$ = Line$ + #CRLF$
State = #RecipeMethod
EndIf
Else
Line$ = Trim(Line$)
If Left(Line$, 1) = "-"
Incredients$ = Left(Incredients$, Len(Incredients$) - 2) + " " + Trim(Mid(Line$, Pos), "-") + #CRLF$
Else
Incredients$ + Trim(Mid(Line$, Pos), "-") + #CRLF$
EndIf
EndIf
EndIf
Case #RecipeMethod
If Len(Line$) = 5 And (Line$ = "MMMMM" Or Line$ = "-----")
Debug "Title: " + Title$
Debug "Category: " + Category$
Debug "Yield: " + Yield$
Debug "Incredients: " + Incredients$
Debug "Method: " + Method$
Debug "---------------------------------------------"
If IsDatabase(DB)
SQL$ = "INSERT INTO ... "
SQL$ + "VALUES ("
SQL$ + "'" + Category$ + "',"
SQL$ + "'" + Title$ + "',"
SQL$ + "'" + Yield$ + "',"
SQL$ + "'" + Incredients$ + "',"
SQL$ + "'" + Method$ + "')"
DatabaseUpdate(DB, SQL$)
EndIf
State = #RecipeNone
Else
If Len(Line$) <> 0
Method$ + Line$ + #CRLF$
EndIf
EndIf
EndSelect
Wend
CloseFile(File)
EndIf
ProcedureReturn Result
EndProcedure
Define Filename$
Filename$ = OpenFileRequester("Choose a Meal-Master file", "", "MealMasterFile|*.mmf|TextFile|*.txt|All|*.*", 0)
If Filename$ <> ""
ImportRecipe(0, Filename$)
EndIf
Bernd