MealMaster: Decoding text file format to database

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

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
It works with all versions which I have tested.

Bernd
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: MealMaster: Decoding text file format to database

Post by electrochrisso »

Fangbeast, please don't send srod's undies, my goat must stay pure. :lol:

My code is a bit basic at the moment and probably only suited to v8.05, as it just looks at the recipe title and the end MMMMM, I have not studied the other formats, perhaps you can give me info on the various formats that are available, so I can make the most robust reader.
I will probably just stick to reading and displaying existing files for the time being, looks like you are probably going to grab each recipe and port them over to MYSql database, infratec's code is probably the way for you to go.
PureBasic! Purely the best 8)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:41 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: MealMaster: Decoding text file format to database

Post by electrochrisso »

Want to stick them into an sqlite database but there are loads of formatting issues (sigh).
Thats what I meant, I got the databases mixed up, infratec's latest code seems to do good formatting that will go easily into sqlite, some of the enumerations were missing, below is the modified version, try it out, the debugs are where the formatted data goes, infratec has put the sqlite code in, but I think you will have to create the database and all the other stuff that is needed to finnish it off. :)

Code: Select all

Enumeration
  #RecipeNone
  #RecipeCategory
  #RecipeYield
  #RecipeIngredients
  #RecipeMethod
EndEnumeration

UseSQLiteDatabase()

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 = #RecipeIngredients
              Break
            EndIf
          Next i
        Case #RecipeIngredients
          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
PureBasic! Purely the best 8)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:42 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

electrochrisso wrote:Thats what I meant, I got the databases mixed up, infratec's latest code seems to do good formatting that will go easily into sqlite, some of the enumerations were missing...
It was an improved version, which means the main code was offered before (see first page).
The missing stuff is written there. :mrgreen: :mrgreen: :mrgreen:

So it was a small puzzle :wink:

Bernd
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: MealMaster: Decoding text file format to database

Post by electrochrisso »

Changed markers, invisible formatting characters, lines that reflow in some but not in others, lines that don't reflow but should. Paragraphs with no carriage return, etc, etc.
You should know FangBeast, Programming wasn't meant to be easy, although PB makes the job easier. :)
It was an improved version, which means the main code was offered before (see first page).
The missing stuff is written there. :mrgreen: :mrgreen: :mrgreen:

So it was a small puzzle :wink:
You make it too easy infratec. :lol:
PureBasic! Purely the best 8)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

You should know FangBeast, Programming wasn't meant to be easy, although PB makes the job easier. :)
Ho, ho, ho, a comedian yet. I think I have a lot to do:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:42 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

a bit modified:

Code: Select all

; - Window Constants

Enumeration 1
  #Window_MenuMaster
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

; - Gadget Constants

Enumeration 1
  ; Window_MenuMaster
  #MenuBar_MenuMaster_Files
  #MenuBar_MenuMaster_OpenRecipes

  #Gadget_MenuMaster_cRecipes
  #Gadget_MenuMaster_Area34
  #Gadget_MenuMaster_Recipes
  #Gadget_MenuMaster_cVersion
  #Gadget_MenuMaster_Area37
  #Gadget_MenuMaster_cTitle
  #Gadget_MenuMaster_Area39
  #Gadget_MenuMaster_lVersion
  #Gadget_MenuMaster_Version
  #Gadget_MenuMaster_lTitle
  #Gadget_MenuMaster_Title
  #Gadget_MenuMaster_lCategories
  #Gadget_MenuMaster_Categories
  #Gadget_MenuMaster_lServes
  #Gadget_MenuMaster_Serves
  #Gadget_MenuMaster_cIngredients
  #Gadget_MenuMaster_Area49
  #Gadget_MenuMaster_Ingredients
  #Gadget_MenuMaster_cInstructions
  #Gadget_MenuMaster_Area52
  #Gadget_MenuMaster_Instructions
  #Gadget_MenuMaster_cControl
  #Gadget_MenuMaster_Area55
  #Gadget_MenuMaster_Save
  #Gadget_MenuMaster_Export
  #Gadget_MenuMaster_Import
  #Gadget_MenuMaster_Print
  #Gadget_MenuMaster_Help
  #Gadget_MenuMaster_Search
  #Gadget_MenuMaster_Exit
  #Gadget_MenuMaster_lControl1
  #Gadget_MenuMaster_lControl2
  #Gadget_MenuMaster_lControl3
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

; - MenuBar Constants

Enumeration 1
  #MenuBar_MenuMaster
EndEnumeration

#MenuBarIndex = #PB_Compiler_EnumerationValue

; - Image Constants

Enumeration 1
  #Image_MenuMaster_Save
  #Image_MenuMaster_Export
  #Image_MenuMaster_Import
  #Image_MenuMaster_Print
  #Image_MenuMaster_Help
  #Image_MenuMaster_Exit
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

; - Load Images

CatchImage(#Image_MenuMaster_Save,    ?_OPT_MenuMaster_Save)
CatchImage(#Image_MenuMaster_Export,  ?_OPT_MenuMaster_Export)
CatchImage(#Image_MenuMaster_Import,  ?_OPT_MenuMaster_Import)
CatchImage(#Image_MenuMaster_Print,   ?_OPT_MenuMaster_Print)
CatchImage(#Image_MenuMaster_Help,    ?_OPT_MenuMaster_Help)
CatchImage(#Image_MenuMaster_Exit,    ?_OPT_MenuMaster_Exit)

;

DataSection
  _OPT_MenuMaster_Save    : IncludeBinary "Images\save32x32.ico"
  _OPT_MenuMaster_Export  : IncludeBinary "Images\export32x32.ico"
  _OPT_MenuMaster_Import  : IncludeBinary "Images\import32x32.ico"
  _OPT_MenuMaster_Print   : IncludeBinary "Images\printer32x32.ico"
  _OPT_MenuMaster_Help    : IncludeBinary "Images\help32x32.ico"
  _OPT_MenuMaster_Exit    : IncludeBinary "Images\exit32x32.ico"
EndDataSection

;

Procedure.l Window_MenuMaster()
  If OpenWindow(#Window_MenuMaster,57,56,845,640,"Menu Master Quickview",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      SetWindowColor(#Window_MenuMaster,$A2A2A2)
    CreateMenu(#MenuBar_MenuMaster,WindowID(#Window_MenuMaster))
      MenuTitle("Files")
      MenuItem(#MenuBar_MenuMaster_OpenRecipes,"Open recipes")
      ContainerGadget(#Gadget_MenuMaster_cRecipes,5,5,210,535,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cRecipes,#PB_Gadget_BackColor,$BFBFBF)
      ListIconGadget(#Gadget_MenuMaster_Recipes,5,5,200,522,"Recipe names",194,#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        SetGadgetColor(#Gadget_MenuMaster_Recipes,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Recipes,LoadFont(#Gadget_MenuMaster_Recipes,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_MenuMaster_cVersion,220,5,620,45,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cVersion,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_MenuMaster_lVersion,10,15,150,20,"MealMaster version")
        SetGadgetColor(#Gadget_MenuMaster_lVersion,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lVersion,LoadFont(#Gadget_MenuMaster_lVersion,"Comic Sans MS",10,0))
      StringGadget(#Gadget_MenuMaster_Version,160,10,100,25,"",#PB_String_ReadOnly|#PB_String_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_Version,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Version,LoadFont(#Gadget_MenuMaster_Version,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_MenuMaster_cTitle,220,55,620,105,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cTitle,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_MenuMaster_lTitle,10,15,150,20,"Recipe title")
        SetGadgetColor(#Gadget_MenuMaster_lTitle,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lTitle,LoadFont(#Gadget_MenuMaster_lTitle,"Comic Sans MS",10,0))
      StringGadget(#Gadget_MenuMaster_Title,160,10,450,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_Title,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Title,LoadFont(#Gadget_MenuMaster_Title,"Comic Sans MS",10,0))
      TextGadget(#Gadget_MenuMaster_lCategories,10,45,150,20,"Categories")
        SetGadgetColor(#Gadget_MenuMaster_lCategories,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lCategories,LoadFont(#Gadget_MenuMaster_lCategories,"Comic Sans MS",10,0))
      StringGadget(#Gadget_MenuMaster_Categories,160,40,450,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_Categories,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Categories,LoadFont(#Gadget_MenuMaster_Categories,"Comic Sans MS",10,0))
      TextGadget(#Gadget_MenuMaster_lServes,10,75,150,20,"No of serves")
        SetGadgetColor(#Gadget_MenuMaster_lServes,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lServes,LoadFont(#Gadget_MenuMaster_lServes,"Comic Sans MS",10,0))
      StringGadget(#Gadget_MenuMaster_Serves,160,70,125,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_Serves,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Serves,LoadFont(#Gadget_MenuMaster_Serves,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_MenuMaster_cIngredients,220,165,160,375,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cIngredients,#PB_Gadget_BackColor,$BFBFBF)
      ListViewGadget(#Gadget_MenuMaster_Ingredients,5,5,150,365)
        SetGadgetColor(#Gadget_MenuMaster_Ingredients,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Ingredients,LoadFont(#Gadget_MenuMaster_Ingredients,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_MenuMaster_cInstructions,385,165,455,375,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cInstructions,#PB_Gadget_BackColor,$BFBFBF)
      EditorGadget(#Gadget_MenuMaster_Instructions,5,5,445,365)
        SetGadgetColor(#Gadget_MenuMaster_Instructions,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Instructions,LoadFont(#Gadget_MenuMaster_Instructions,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_MenuMaster_cControl,5,545,835,70,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_cControl,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_MenuMaster_Save,5,5,45,45,ImageID(#Image_MenuMaster_Save))
      ButtonImageGadget(#Gadget_MenuMaster_Export,50,5,45,45,ImageID(#Image_MenuMaster_Export))
      ButtonImageGadget(#Gadget_MenuMaster_Import,95,5,45,45,ImageID(#Image_MenuMaster_Import))
      ButtonImageGadget(#Gadget_MenuMaster_Print,140,5,45,45,ImageID(#Image_MenuMaster_Print))
      ButtonImageGadget(#Gadget_MenuMaster_Help,185,5,45,45,ImageID(#Image_MenuMaster_Help))
      StringGadget(#Gadget_MenuMaster_Search,535,25,225,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_MenuMaster_Search,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_MenuMaster_Search,LoadFont(#Gadget_MenuMaster_Search,"Comic Sans MS",10,0))
      ButtonImageGadget(#Gadget_MenuMaster_Exit,780,5,45,45,ImageID(#Image_MenuMaster_Exit))
      TextGadget(#Gadget_MenuMaster_lControl1,5,50,225,15,"  Save      Export   Import     Print       Help")
        SetGadgetColor(#Gadget_MenuMaster_lControl1,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lControl1,LoadFont(#Gadget_MenuMaster_lControl1,"Comic Sans MS",8,0))
      TextGadget(#Gadget_MenuMaster_lControl2,535,50,225,15,"Search for a recipe here...")
        SetGadgetColor(#Gadget_MenuMaster_lControl2,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lControl2,LoadFont(#Gadget_MenuMaster_lControl2,"Comic Sans MS",8,0))
      TextGadget(#Gadget_MenuMaster_lControl3,780,50,45,15,"Exit",#PB_Text_Center)
        SetGadgetColor(#Gadget_MenuMaster_lControl3,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_MenuMaster_lControl3,LoadFont(#Gadget_MenuMaster_lControl3,"Comic Sans MS",8,0))
      CloseGadgetList()
      HideWindow(#Window_MenuMaster,0)
    ProcedureReturn WindowID(#Window_MenuMaster)
  EndIf
EndProcedure

;

Enumeration
  ; ElectroChrisso's variables
  #RecipeVersion
  #RecipeNone
  #RecipeCategory
  #RecipeYield
  #RecipeIngredients
  #RecipeMethod
EndEnumeration

;

Structure ProgramData
  RecipeFileName.s
  RecipeFileHandle.i
 
  QuitValue.i
EndStructure

; Set the system to use SQLite database handling

UseSQLiteDatabase()

;

Global Program.ProgramData

;

Declare   DisplayRecipeTitles()
Declare   DisplayRecipeDetails(FilePos.q)
Declare.i GetRecipeDetails(FilePos.q)

;

Procedure DisplayRecipeTitles()
  
  Protected FilePtr.q, i.i
  
  Program\RecipeFileName = OpenFileRequester("Choose a Meal-Master file", "", "MealMasterFile|*.mmf|TextFile|*.txt|All|*.*", 1)
  If Program\RecipeFileName <> ""
    Program\RecipeFileHandle = ReadFile(#PB_Any,  Program\RecipeFileName)
    If Program\RecipeFileHandle
      ClearGadgetItems(#Gadget_MenuMaster_Recipes)
      SetGadgetText(#Gadget_MenuMaster_Recipes, "")
      i = 0
      Repeat
        FilePtr = Loc(Program\RecipeFileHandle)
        TitleString.s = ReadString(Program\RecipeFileHandle)
        If FindString(TitleString, "Meal-Master", 10)          
          Repeat
            TitleString.s = ReadString(Program\RecipeFileHandle)
          Until Left(TitleString.s, 12) = "      Title:"
          AddGadgetItem(#Gadget_MenuMaster_Recipes, i, Mid(TitleString.s, 13))
          SetGadgetItemData(#Gadget_MenuMaster_Recipes, i, FilePtr)
          i + 1          
        EndIf
        
      Until Eof(Program\RecipeFileHandle)
      CloseFile(Program\RecipeFileHandle)
    EndIf
  EndIf
EndProcedure

;

Procedure DisplayRecipeDetails(FilePos.q)
  If Program\RecipeFileName <> ""
    GetRecipeDetails(FilePos.q)
  EndIf
EndProcedure

;

Procedure.i GetRecipeDetails(FilePos.q)
  Protected.i Result, File, State, Pos, Count, i
  Protected   Line.s, Text.s, Version.s, Title.s, Category.s, Yield.s, Ingredients.s, Method.s
 
  Program\RecipeFileHandle = ReadFile(#PB_Any,  Program\RecipeFileName)
 
  If Program\RecipeFileHandle
    
    FileSeek(Program\RecipeFileHandle, FilePos)
    
    State = #RecipeNone
    
    While FindTitle.s <> "MMMMM"
     
      If FindTitle.s <> "MMMMM"
       
        Line.s = ReadString(Program\RecipeFileHandle)
       
        Select State
           
          Case #RecipeNone
            Pos = FindString(Line.s, "Meal-Master")
            If Pos
              Version.s     = Trim(Mid(Line.s, Pos + 11));:Debug Line.s
              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
           
          Case #RecipeVersion
            Pos = FindString(Line.s, "Title:")
            If Pos
              Title.s       = Trim(Mid(Line.s, Pos + 7))
              State         = #RecipeCategory
            EndIf
           
          Case #RecipeCategory
            Pos = FindString(Line.s, "Categories:")
            If Pos
              Category.s = Trim(Mid(Line.s, Pos + 12))
              State = #RecipeYield
            EndIf
           
          Case #RecipeYield
            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
           
          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    = #RecipeMethod
                  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
           
          Case #RecipeMethod
           
            If Len(Line.s) = 5 And (Line.s = "MMMMM" Or Line.s = "-----")
              ClearGadgetItems(#Gadget_MenuMaster_Ingredients)
              SetGadgetText(#Gadget_MenuMaster_Version       , Version.s)
              SetGadgetText(#Gadget_MenuMaster_Title         , Title.s)
              SetGadgetText(#Gadget_MenuMaster_Categories    , Category.s )
             
              For ShowIngredients.i = 1 To CountString(Ingredients.s, #LF$)
                AddGadgetItem(#Gadget_MenuMaster_Ingredients, -1, StringField(Ingredients.s, ShowIngredients.i, #LF$))
              Next ShowIngredients.i
             
              SetGadgetText(#Gadget_MenuMaster_Serves        , Yield.s)
              SetGadgetText(#Gadget_MenuMaster_Instructions  , Method.s)
              State = #RecipeNone
              CloseFile(Program\RecipeFileHandle)
              ProcedureReturn
            Else
              If Len(Line.s) <> 0
                Method.s + Line.s + #LF$
              EndIf
            EndIf
        EndSelect
      EndIf
    Wend
   
    CloseFile(Program\RecipeFileHandle)
   
  EndIf
EndProcedure

;

If Window_MenuMaster()
  Program\QuitValue = 0
  Repeat
    EventID  =  WaitWindowEvent()
    MenuID   =  EventMenu()
    GadgetID =  EventGadget()
    WindowID =  EventWindow()
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_MenuMaster               : Program\QuitValue = 1
        EndSelect
      Case #PB_Event_Menu
        Select MenuID
          Case #MenuBar_MenuMaster_OpenRecipes  : DisplayRecipeTitles()
        EndSelect
      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_MenuMaster_Recipes
            Select EventType()
              Case #PB_EventType_Change         : DisplayRecipeDetails(GetGadgetItemData(#Gadget_MenuMaster_Recipes, GetGadgetState(#Gadget_MenuMaster_Recipes)))
            EndSelect
          Case #Gadget_MenuMaster_Save          :
          Case #Gadget_MenuMaster_Export        :
          Case #Gadget_MenuMaster_Import        :
          Case #Gadget_MenuMaster_Print         :
          Case #Gadget_MenuMaster_Help          :
          Case #Gadget_MenuMaster_Search        :
          Case #Gadget_MenuMaster_Exit          :
        EndSelect
    EndSelect
  Until Program\QuitValue
  CloseWindow(#Window_MenuMaster)
EndIf
End
The file reading is now optimized :mrgreen:
It shows now the correct recipe, not the one behind.

But it was only a 'quick shot'.
Many things still todo.

Bernd
Last edited by infratec on Mon Dec 31, 2012 4:34 pm, edited 2 times in total.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Hi Fangbeast,
Hello Bernd.
a bit modified: The file reading is now optimized :mrgreen:
That's a very evil grin!!!
It shows now the correct recipe, not the one behind.
Ah well, I don't know what I am doing anyway, hack and slash is the way I work.
But it was only a 'quick shot'. Many things still todo.
Don't I know it!! Look at my 'features' list!! When this is all going properly, I can turn it into a proper open source project. Nothing as fancy as what I have seen out there (beyond me anyway) but a nice little database to handle/search/manipulate 250,000 recipes. (or more)

I appreciate all of your help!.

P.S. Pointers do my head in!

:):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:43 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:43 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi,

I modified my listing above.
Now you also get the version of meal-master
and it is not needed anymore to search the recipe for the details.
(We know now whre it starts :mrgreen: )

That it takes 7 minutes is strange.
I thougt too that the memory stuff should be faster.
Maybe stringfield() is the problem.

I have only short files to test (110kB)

I'll try to reproduce it.

Bernd
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: MealMaster: Decoding text file format to database

Post by Fangbeast »

Moderator, Please remove this post, outdated.
Last edited by Fangbeast on Mon Jan 14, 2013 1:43 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Post Reply