MealMaster: Decoding text file format to database

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
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:50 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:50 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:51 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:51 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:51 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

Here my 'universal' import:

Code: Select all

EnableExplicit

Enumeration
  #RecipeType_Unknown
  #RecipeType_MealMaster
  #RecipeType_MasterCook
  #RecipeType_MasterCookMX2
  #RecipeType_RecipeML
  #RecipeType_NowYoureCooking
  #RecipeType_FoodDataExchange
  #RecipeType_RecipeMuncher
EndEnumeration




Structure RecipeData
  Version.s
  Owner.s
  Title.s
  Categories.s
  Serves.s
  Source.s
  Ingredients.s
  Instructions.s
  PictureName.s
  LastUpdate.s
  Record.s
EndStructure




Procedure RecipeClearStructure(*Recipe.RecipeData)
  
  *Recipe\Version = ""
  *Recipe\Owner = ""
  *Recipe\Title = ""
  *Recipe\Categories = ""
  *Recipe\Serves = ""
  *Recipe\Source = ""
  *Recipe\Ingredients = ""
  *Recipe\Instructions = ""
  *Recipe\PictureName = ""
  *Recipe\LastUpdate  = ""
  
EndProcedure



Procedure.s RecipeCRLFSpaceTrim(Line$)
  
  Line$ = Trim(Line$)
  Line$ = Trim(Line$, #CR$)
  Line$ = Trim(Line$, #LF$)
  Line$ = Trim(Line$, #CR$)
  Line$ = Trim(Line$)
  
  ProcedureReturn Line$
  
EndProcedure





Procedure.i RecipeIntoDB(*Recipe.RecipeData)
  
  Protected Result.i
  Protected File.i
  
  
  Debug "Version:       " + *Recipe\Version
  Debug "Owner:         " + *Recipe\Owner
  Debug "Title:         " + *Recipe\Title
  Debug "Categories:    " + *Recipe\Categories
  Debug "No of serves:  " + *Recipe\Serves
  Debug "Source:        " + *Recipe\Source
  Debug "Ingredients:   " + *Recipe\Ingredients
  Debug "Instructions:  " + *Recipe\Instructions
  Debug "Picture name:  " + *Recipe\PictureName
  Debug "Last update:   " + *Recipe\LastUpdate 
  Debug "--------------------------------------"
  
;   File = OpenFile(#PB_Any, "export.txt")
;   If File
;     FileSeek(File, Lof(File))
;     WriteStringN(File, "Version: " + *Recipe\Version)
;     WriteStringN(File, "Owner: " + "*Recipe\Owner)
;     WriteStringN(File, "Title: " + *Recipe\Title)
;     WriteStringN(File, "Categories: " + *Recipe\Categories)
;     WriteStringN(File, "No of serves: " + *Recipe\Serves)
;     WriteStringN(File, "Source: " + *Recipe\Source)
;     WriteStringN(File, "Ingredients: " + *Recipe\Ingredients)
;     WriteStringN(File, "Instructions: " + *Recipe\Instructions)
;     WriteStringN(File, "Picture name: " + *Recipe\PictureName)
;     WriteStringN(File, "Last update: " + *Recipe\LastUpdate)
;     WriteStringN(File, "--------------------")
;     CloseFile(File)
;   EndIf
  
  Result = #True
  
  ProcedureReturn Result
  
EndProcedure




Procedure.s RecipeImportMealMasterIngredient(Ingredient$)
  
  Protected Result$
  
  
  Result$ = Trim(Left(Ingredient$, 7)) + "~"
  Result$ + Trim(Mid(Ingredient$, 8, 3)) + "~"
  Result$ + Trim(Mid(Ingredient$, 12))
  
  ProcedureReturn Result$
  
EndProcedure




Procedure.i RecipeImportMealMaster(File.i)
  
  Protected.i Result, State, Pos, Count, i, LastLineWasEmpty, FirstIngredientFound, DoubleColumn, Comment
  Protected Line$, Recipe.RecipeData, Help$, Char$
  Protected NewList IngredientList.s()
  
  
  While Not Eof(File)
    
    Line$ = ReadString(File, #PB_UTF8)
    
    Select State
      ; No recipe found yet
      Case 0
        Pos = FindString(Line$, "Meal-Master")
        If Pos
          RecipeClearStructure(@Recipe)
          ClearList(IngredientList())
          State + 1
        EndIf
        ; Recipe version number found
      Case 1
        Pos = FindString(Line$, "Title:")
        If Pos
          Recipe\Title = Trim(Mid(Line$, Pos + 7))
          State + 1
        EndIf
        ; Recipe category found
      Case 2
        Pos = FindString(Line$, "Categories:")
        If Pos
          Recipe\Categories = RemoveString(Mid(Line$, Pos + 12), " ")
          State + 1
        EndIf
        ; Recipe yield found
      Case 3
        Count = Len(Line$)
        For i = 1 To Count
          If Val(Mid(Line$, i)) > 0
            Recipe\Serves = Str(Val(Mid(Line$, i)))
            State + 1
            Break
          EndIf
        Next i
        ; Recipe ingredients found
      Case 4
        If Trim(Line$) <> ""
          
          If FindString(Line$, "-----")
            LastLineWasEmpty = #False
            Line$ = RemoveString(Line$, "MMMMM")
            Line$ = Trim(Line$)
            Recipe\Ingredients + "~~" + Trim(Line$, "-") + "|"
            Debug "comment 1: " + Trim(Line$, "-")
          Else
            
            Comment = #False
            
            If LastLineWasEmpty
              Char$ = Trim(Left(Line$, 3))
              If Char$ <> ""
                If Char$ <> ":" And Asc(Char$) > '9'
                  
                  ForEach IngredientList()
                    Recipe\Ingredients + RecipeImportMealMasterIngredient(IngredientList()) + "|"
                  Next
                  
                  Recipe\Instructions = Trim(Line$)
                  State + 1
                EndIf
              Else
                If Trim(Left(Line$, 7)) = ""
                  Recipe\Ingredients + "~~" + Trim(Line$) + "|"
                  Comment = #True
                EndIf
              EndIf
            EndIf
            
            If Not Comment And State = 4
            
              If Not FirstIngredientFound
                
                If Len(Line$) > 40
                  DoubleColumn = #True
                EndIf
              
                FirstIngredientFound = #True
              EndIf
              
              If DoubleColumn
                AddElement(IngredientList())
                IngredientList() = Mid(Line$, 42)
                Line$ = Left(Line$, 41)
              EndIf
              
              Recipe\Ingredients + RecipeImportMealMasterIngredient(Line$) + "|"
              
            EndIf
            
          EndIf
          LastLineWasEmpty = #False
        Else
          LastLineWasEmpty = #True
        EndIf
        
        ; Recipe method found
      Case 5
        If Len(Line$) = 5 And (Line$ = "MMMMM" Or Line$ = "-----")
          Recipe\Categories = Trim(Recipe\Categories, ",")
          Recipe\Ingredients = ReplaceString(Recipe\Ingredients, "|~~-", " ")
          Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
          Result = RecipeIntoDB(@Recipe)
          State = 0
        Else
          If Len(Line$) <> 0
            Recipe\Instructions + Line$ + #LF$
          EndIf
        EndIf
    EndSelect
    
  Wend
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeImportMasterCook(File.i)
  
  Protected.i Result, State, Pos
  Protected Line$, Recipe.RecipeData
  
  
  While Not Eof(File)
    
    Line$ = ReadString(File)
    
    Select State
      Case 0
        If FindString(Line$, "Exported")
          RecipeClearStructure(@Recipe)
          State + 1
        EndIf
      Case 1
        If Line$ <> ""
          Recipe\Title = Trim(Line$)
          State + 1
        EndIf
      Case 2
        Pos = FindString(Line$, "Recipe By     :")
        If Pos
          Recipe\Owner = Trim(Mid(Line$, Pos + 15))
          State + 1
        EndIf
      Case 3
        Pos = FindString(Line$, "Serving Size  :")
        If Pos
          Recipe\Serves = Str(Val(Mid(Line$, Pos + 15)))
          State + 1
        EndIf
      Case 4
        Pos = FindString(Line$, "Categories    :")
        If Pos
          Line$ = Mid(Line$, Pos + 15)
          Recipe\Categories + Trim(Left(Line$, 33))
          Line$ = Trim(Mid(Line$, 33))
          If Line$ <> ""
            Recipe\Categories + "," + Line$
          EndIf
          State + 1
        EndIf
      Case 5
        Pos = FindString(Line$, "Amount")
        If Pos
          State + 1
        Else
          Line$ = Trim(Line$)
          If Line$ <> ""
            Recipe\Categories + "," + Trim(Left(Line$, 33))
            Line$ = Trim(Mid(Line$, 33))
            If Line$ <> ""
              Recipe\Categories + "," + Line$
            EndIf
          EndIf
        EndIf
      Case 6
        If Line$ = ""
          Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
          State + 1
        Else
          If Left(Line$, 1) <> "-"
            Recipe\Ingredients + Trim(Left(Line$, 8)) + "~"
            Recipe\Ingredients + Trim(Mid(Line$, 11, 12)) + "~"
            Recipe\Ingredients + Trim(Mid(Line$, 25)) + "|"
          EndIf
        EndIf
      Case 7
        If FindString(Line$, "- - - -")
          Result = RecipeIntoDB(@Recipe)
          State = 0
        Else
          Recipe\Instructions + Line$ + #LF$
        EndIf
    EndSelect
    
  Wend
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeImportMasterCookMX2_XML(*CurrentNode, CurrentSublevel.i, *Recipe.RecipeData)
 
  Protected NodeName$, *ChildNode, AttributeName$, qty$, unit$, name$
 
 
  If XMLNodeType(*CurrentNode) = #PB_XML_Normal
    NodeName$ = GetXMLNodeName(*CurrentNode)
    ;Debug NodeName$
   
    If NodeName$ = "RcpE"
      If *Recipe\Title <> ""
        *Recipe\Categories = Trim(*Recipe\Categories, ",")
        *Recipe\Ingredients = Trim(*Recipe\Ingredients, "|")
        RecipeIntoDB(*Recipe)
        RecipeClearStructure(*Recipe)
      EndIf
      
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "name"
            *Recipe\Title = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "author"
            *Recipe\Owner = XMLAttributeValue(*CurrentNode)
          EndIf
        Wend
      EndIf
    ElseIf NodeName$ = "Serv"
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "qty"
            *Recipe\Serves = XMLAttributeValue(*CurrentNode)
          EndIf
        Wend
      EndIf
    ElseIf NodeName$ = "CatT"
      *Recipe\Categories + RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode)) + ","
    ElseIf NodeName$ = "IngR"
      name$ = "" : unit$ = "" : qty$ = ""
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "name"
            name$ = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "unit"
            unit$ = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "qty"
            qty$ = XMLAttributeValue(*CurrentNode)
          EndIf
        Wend
      EndIf
      *Recipe\Ingredients + qty$ + "~" + unit$ + "~" + name$ + "|"
    ElseIf NodeName$ = "DirT"
      *Recipe\Instructions + RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode)) + #LF$
    EndIf
   
    *ChildNode = ChildXMLNode(*CurrentNode)
   
    While *ChildNode <> 0
      RecipeImportMasterCookMX2_XML(*ChildNode, CurrentSublevel + 1, *Recipe)
      *ChildNode = NextXMLNode(*ChildNode)
    Wend   
   
  EndIf
  
EndProcedure




Procedure.i RecipeImportMasterCookMX2(File.i)
  
  Protected.i Result, XML
  Protected.q Size
  Protected *Buffer, *Node
  Protected Recipe.RecipeData
  Protected Bug$
  
  
  Size = Lof(File)
  *Buffer = AllocateMemory(Size)
  If *Buffer
    If ReadData(File, *Buffer, Size) = Size
      
      ; bugfix for standalone at wrong place
      Bug$ = PeekS(*Buffer, 100)
      Bug$ = ReplaceString(Bug$, "standalone=" + #DQUOTE$ + "yes" + #DQUOTE$, "                ")
      Bug$ = ReplaceString(Bug$, "standalone=" + #DQUOTE$ + "no" + #DQUOTE$, "               ")
      Debug Bug$
      CopyMemory(@Bug$, *Buffer, 100)
      
      XML = CatchXML(#PB_Any, *Buffer, Size)
      If XML
        *Node = MainXMLNode(XML)
        
        If *Node
          RecipeImportMasterCookMX2_XML(*Node, 0, @Recipe.RecipeData)
        EndIf
        
        FreeXML(XML)
        
        Recipe\Categories = Trim(Recipe\Categories, ",")
        Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
        
        Result = RecipeIntoDB(@Recipe)
        
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure 






Procedure.i RecipeImportRecipeML_XML(*CurrentNode, CurrentSublevel.i, *Recipe.RecipeData)
 
  Protected NodeName$, *ChildNode
  Static HeadFlag.i, IngredientsFlag.i
 
 
  If XMLNodeType(*CurrentNode) = #PB_XML_Normal
    NodeName$ = GetXMLNodeName(*CurrentNode)
    ;Debug NodeName$
   
    If NodeName$ = "recipe"
      If *Recipe\Title <> ""
        RecipeIntoDB(*Recipe)
        RecipeClearStructure(*Recipe)
      EndIf
    ElseIf NodeName$ = "head"
      HeadFlag = #True
      IngredientsFlag = #False
    ElseIf NodeName$ = "title"
      If HeadFlag
        *Recipe\Title = GetXMLNodeText(*CurrentNode)
        HeadFlag = #False
      ElseIf IngredientsFlag
        *Recipe\Ingredients + "~~" + GetXMLNodeText(*CurrentNode)
      EndIf
    ElseIf NodeName$ = "cat"
      *Recipe\Categories + GetXMLNodeText(*CurrentNode) + ","
    ElseIf NodeName$ = "yield"
      *Recipe\Serves + GetXMLNodeText(*CurrentNode)
    ElseIf NodeName$ = "ingredients"
      IngredientsFlag = #True
    ElseIf NodeName$ = "qty"
      *Recipe\Ingredients + "~" + GetXMLNodeText(*CurrentNode)
    ElseIf NodeName$ = "unit"
      *Recipe\Ingredients + "~" + GetXMLNodeText(*CurrentNode)
    ElseIf NodeName$ = "item"
      *Recipe\Ingredients + "~" + GetXMLNodeText(*CurrentNode) + "|"
    ElseIf NodeName$ = "step"
      *Recipe\Instructions + GetXMLNodeText(*CurrentNode) + #LF$
    EndIf
   
    *ChildNode = ChildXMLNode(*CurrentNode)
   
    While *ChildNode <> 0
      RecipeImportRecipeML_XML(*ChildNode, CurrentSublevel + 1, *Recipe)
      *ChildNode = NextXMLNode(*ChildNode)
    Wend   
   
  EndIf
  
EndProcedure




Procedure.i RecipeImportRecipeML(File.i)
  
  Protected.i Result, XML
  Protected.q Size
  Protected *Buffer, *Node
  Protected Recipe.RecipeData
  
  
  Size = Lof(File)
  *Buffer = AllocateMemory(Size)
  If *Buffer
    If ReadData(File, *Buffer, Size) = Size
      XML = CatchXML(#PB_Any, *Buffer, Size)
      If XML
        *Node = MainXMLNode(XML)
        
        If *Node
          RecipeImportRecipeML_XML(*Node, 0, @Recipe.RecipeData)
        EndIf
        
        FreeXML(XML)
        
        Recipe\Categories = Trim(Recipe\Categories, ",")
        Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
        
        Result = RecipeIntoDB(@Recipe)
        
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.s RecipeImportNowYoureCookingIngredient(Ingredient$)
  
  Protected Result$, Help$, i.i, Length.i, Pos.i
  
  Length = Len(Ingredient$)
  i = 1
  Help$ = Mid(Ingredient$, i, 1)
  While (Asc(Help$) < '9' Or Help$ = " " ) And i < Length
    Result$ + Mid(Ingredient$, i, 1)
    i + 1
    Help$ = Mid(Ingredient$, i, 1)
  Wend
  If Right(Result$, 2) = "  "
    Help$ = "A"
  EndIf
  Result$ = Trim(Result$) + "~"
  
  If Help$ = "A"
    Result$ + "~"
  Else
    Pos = FindString(Ingredient$, " ", i)
    If Pos
      Result$ + Mid(Ingredient$, i, Pos - i) + "~"
      i = Pos
    EndIf
  EndIf
  
  Result$ + Trim(Mid(Ingredient$, i))
  
  ProcedureReturn Result$
  
EndProcedure




Procedure.i RecipeImportNowYoureCooking(File.i)
  
  Protected.i Result, State, Pos, Help
  Protected Line$, Recipe.RecipeData
  
  
  While Not Eof(File)
    
    Line$ = ReadString(File)
    
    Select State
      Case 0
        If Left(Line$, 5) = "@@@@@"
          RecipeClearStructure(@Recipe)
          State + 1
        EndIf
      Case 1
        If Trim(Line$) <> ""
          Recipe\Title = Trim(Line$)
          State + 1
        EndIf
      Case 2
        If Trim(Line$) <> ""
          Recipe\Categories = RemoveString(Line$, " ")
          Help = #False
          State + 1
        EndIf
      Case 3
        If Trim(Line$) = ""
          If Help
            Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
            State + 1
          Else
            Help = #True
          EndIf
        Else
          Recipe\Ingredients + RecipeImportNowYoureCookingIngredient(Line$) + "|"
        EndIf
      Case 4
        If Left(Line$, 6) = "Yield:"
          Recipe\Serves = Str(Val(Mid(Line$, 7)))
          State + 1
        Else
          Recipe\Instructions + Line$ + #LF$
        EndIf
      Case 5
        If Left(Line$, 16) = "** Exported from"
          Result = RecipeIntoDB(@Recipe)
          State = 0
        EndIf
        
    EndSelect
    
  Wend
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeImportFoodDataExchange_XML(*CurrentNode, CurrentSublevel.i, *Recipe.RecipeData)
  
  Protected NodeName$, *ChildNode, AttributeName$
  Protected Filename$, File$, *Buffer, FileSize.i, File.i, Count.i, i.i, FilePart$, FileExt$
  
  If XMLNodeType(*CurrentNode) = #PB_XML_Normal
    NodeName$ = GetXMLNodeName(*CurrentNode)
    ;Debug NodeName$
    
    If NodeName$ = "Recipe"
      
      If *Recipe\Title <> ""
        RecipeIntoDB(*Recipe)
        RecipeClearStructure(*Recipe)
      EndIf
      
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "Name"
            *Recipe\Title = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "Servings"
            *Recipe\Serves = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "RecipeTypes"
            *Recipe\Categories = RemoveString(XMLAttributeValue(*CurrentNode), " ")
          ElseIf AttributeName$ = "Source"
            *Recipe\Source = Trim(XMLAttributeValue(*CurrentNode))
          EndIf
        Wend
      EndIf
    ElseIf NodeName$ = "RecipeIngredient"
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "Quantity"
            *Recipe\Ingredients + XMLAttributeValue(*CurrentNode) + "~"
          ElseIf AttributeName$ = "Unit"
            *Recipe\Ingredients + XMLAttributeValue(*CurrentNode) + "~"
          ElseIf AttributeName$ = "Ingredient"
            *Recipe\Ingredients + XMLAttributeValue(*CurrentNode) + "|"
          EndIf
        Wend
      EndIf
    ElseIf NodeName$ = "ProcedureText"
      *Recipe\Instructions + GetXMLNodeText(*CurrentNode) + #LF$
    ElseIf NodeName$ = "SourceImage"
      If ExamineXMLAttributes(*CurrentNode)
        While NextXMLAttribute(*CurrentNode)
          AttributeName$ = XMLAttributeName(*CurrentNode)
          If AttributeName$ = "FileType"
            FileExt$ = XMLAttributeValue(*CurrentNode)
          ElseIf AttributeName$ = "FileName"
            Filename$ = XMLAttributeValue(*CurrentNode)
          EndIf
        Wend
      EndIf
      *Recipe\PictureName = Filename$ + "." + FileExt$
;       
;       File = CreateFile(#PB_Any, GetPathPart(ProgramFilename()) + "pictures\" + Filename$ + "." + FileExt$)
;       If File
;         File$ = GetXMLNodeText(*CurrentNode)
;         
;         File$ = RemoveString(File$, #LF$)
;         File$ = Trim(File$, #CR$)
;         Count = CountString(File$, #CR$)
;         For i = 1 To Count
;           FilePart$ = StringField(File$, i, #CR$)
;           If Len(FilePart$)
;             *Buffer = AllocateMemory(Len(FilePart$))
;             If *Buffer
;               FileSize = Base64Decoder(@FilePart$, Len(FilePart$), *Buffer, MemorySize(*Buffer))
;               WriteData(File, *Buffer, FileSize)
;               FreeMemory(*Buffer)
;             EndIf
;           EndIf
;         Next i
;         CloseFile(File)
;         File$ = ""
;       EndIf
    EndIf
    
    *ChildNode = ChildXMLNode(*CurrentNode)
    
    While *ChildNode <> 0
      RecipeImportFoodDataExchange_XML(*ChildNode, CurrentSublevel + 1, *Recipe)
      *ChildNode = NextXMLNode(*ChildNode)
    Wend   
    
  EndIf 
  
EndProcedure




Procedure.i RecipeImportFoodDataExchange(File.i)
  
  Protected.i Result, XML
  Protected.q Size
  Protected *Buffer, *Node
  Protected Recipe.RecipeData
  
  
  Size = Lof(File)
  *Buffer = AllocateMemory(Size)
  If *Buffer
    If ReadData(File, *Buffer, Size) = Size
      XML = CatchXML(#PB_Any, *Buffer, Size)
      If XML
        *Node = MainXMLNode(XML)
        
        If *Node
          RecipeImportFoodDataExchange_XML(*Node, 0, @Recipe.RecipeData)
        EndIf
        
        FreeXML(XML)
        
        Recipe\Categories = Trim(Recipe\Categories, ",")
        Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
        
        Result = RecipeIntoDB(@Recipe)
        
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeImportRecipeMuncher_XML(*CurrentNode, CurrentSublevel.i, *Recipe.RecipeData)
  
  Protected NodeName$, *ChildNode
  
  
  If XMLNodeType(*CurrentNode) = #PB_XML_Normal
    NodeName$ = GetXMLNodeName(*CurrentNode)
    ;Debug NodeName$
    
    If NodeName$ = "RecipeItem"
      If *Recipe\Title <> ""
        RecipeIntoDB(*Recipe)
        RecipeClearStructure(*Recipe)
      EndIf
    ElseIf NodeName$ = "Version"
      *Recipe\Version = GetXMLNodeText(*CurrentNode)
    ElseIf NodeName$ = "Owner"
      *Recipe\Owner = GetXMLNodeText(*CurrentNode)
    ElseIf NodeName$ = "Title"
      *Recipe\Title = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "Categories"
      *Recipe\Categories =  RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "Serves"
      *Recipe\Serves = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "Source"
      *Recipe\Source = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "Ingredients"
      *Recipe\Ingredients = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "Instructions"
      *Recipe\Instructions = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "PictureName"
      *Recipe\PictureName = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    ElseIf NodeName$ = "LastUpdate"
      *Recipe\LastUpdate = RecipeCRLFSpaceTrim(GetXMLNodeText(*CurrentNode))
    EndIf
    
    *ChildNode = ChildXMLNode(*CurrentNode)
    
    While *ChildNode <> 0
      RecipeImportRecipeMuncher_XML(*ChildNode, CurrentSublevel + 1, *Recipe)
      *ChildNode = NextXMLNode(*ChildNode)
    Wend    
    
  EndIf
    
EndProcedure




Procedure.i RecipeImportRecipeMuncher(File.i)
  
  Protected.i Result, XML
  Protected.q Size
  Protected *Buffer, *Node
  Protected Recipe.RecipeData
  
  
  Size = Lof(File)
  *Buffer = AllocateMemory(Size)
  If *Buffer
    If ReadData(File, *Buffer, Size) = Size
      XML = CatchXML(#PB_Any, *Buffer, Size)
      If XML
        *Node = MainXMLNode(XML)
        
        If *Node
          RecipeImportRecipeMuncher_XML(*Node, 0, @Recipe.RecipeData)
        EndIf
        
        FreeXML(XML)
        
        Recipe\Categories = Trim(Recipe\Categories, ",")
        Recipe\Ingredients = Trim(Recipe\Ingredients, "|")
        
        Result = RecipeIntoDB(@Recipe)
        
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeFileType(File.i)
  
  Protected Result.i, Line$
  
  
  Result = #RecipeType_Unknown
  
  Repeat
    Line$ = ReadString(File)
    If FindString(Line$, "Meal-Master")
      Result = #RecipeType_MealMaster
    ElseIf FindString(Line$, " MasterCook")
      Result = #RecipeType_MasterCook
    ElseIf FindString(Line$, "<mx2")
      Result = #RecipeType_MasterCookMX2
    ElseIf FindString(Line$, "<recipe>")
      Result = #RecipeType_RecipeML
    ElseIf FindString(Line$, "Now You're Cooking")
      Result = #RecipeType_NowYoureCooking
    ElseIf FindString(Line$, "<fdx ")
      Result = #RecipeType_FoodDataExchange
    ElseIf FindString(Line$, "<RecipeMuncher")
      Result = #RecipeType_RecipeMuncher
    EndIf
  Until Eof(File) Or Result <> #RecipeType_Unknown
  
  FileSeek(File, 0)
  
  ProcedureReturn Result
  
EndProcedure




Procedure.i RecipeFileImport(Filename$)
  
  Protected Result.i, File.i, FileType.i
  
  
  File = ReadFile(#PB_Any, Filename$)
  If File
    
    FileType = RecipeFileType(File)
    ;Debug FileType
    
    Select FileType
      Case #RecipeType_MealMaster
        Result = RecipeImportMealMaster(File.i)
      Case #RecipeType_MasterCook
        Result = RecipeImportMasterCook(File.i)
      Case #RecipeType_MasterCookMX2
        Result = RecipeImportMasterCookMX2(File.i)
      Case #RecipeType_RecipeML
        Result = RecipeImportRecipeML(File.i)
      Case #RecipeType_NowYoureCooking
        Result = RecipeImportNowYoureCooking(File.i)
      Case #RecipeType_FoodDataExchange
        Result = RecipeImportFoodDataExchange(File.i)
      Case #RecipeType_RecipeMuncher
        Result = RecipeImportRecipeMuncher(File.i)
      Default
        MessageRequester("Error", GetFilePart(Filename$) + " has an unknown format.")
    EndSelect
    
    CloseFile(File)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Define Filename$

Filename$ = OpenFileRequester("Choose a recipe file", "", "*.*|*.*", 0)
If Filename$ <> ""
  If RecipeFileImport(Filename$)
    MessageRequester("Info", GetFilePart(Filename$) + " successfully imported.")
  EndIf
EndIf
I added MasterCook.

You have to add the MealMaster stuff in the procedure RecipeImportMealMaster(File.i)

Bernd

[EDIT] RecipeML added
[EDIT] MealMaster added (still need work)
[EDIT] FDX added
[EDIT] Fixed RecipeML (ingredients title)
[EDIT] Fixed FDX multiple recipes in one file
[EDIT] Fixed NYC
[EDIT] Next try for MealMaster
[EDIT] Added RecipeMuncher
[EDIT] Added MasterCook MX2
Last edited by infratec on Sun Jan 20, 2013 3:44 pm, edited 9 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 »

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

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

I don't downloaded your files, so I can say nothing about it :oops:

I added

Code: Select all

Result = #True
in the code above.
(At the end of the debug outputs in MasterCook)

At the moment I take a look at RecipeML.

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:51 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:52 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

I edited my Importer above.

RecipeML is added.

I also changed the stuff a bit:
Now there is a Recipe structure.
This was neccessary of course of the recursive XML stuff.
I added a procedure RecipeIntoDB(*Recipe.RecipeStructure)
Here arrive each single recipe and you can do your database stuff.

Now I look again after MealMaster.

Bernd
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

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:52 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:52 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: MealMaster: Decoding text file format to database

Post by infratec »

Hi Fangbeast,

edit my code above:

Inserted the MealMaster stuff, but stilll not > 90%
Added FDX (Food Data eXchange Format)

But I found only one example in this format.
So it needs more testing.

Bernd
Post Reply