Results 1 to 21 of 21

Thread: Best way to Open a File And Parse It?

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Arrow Best way to Open a File And Parse It?

    Hello, I am working on my own File Format and then parse it after opening the file... But I am having a couple of issues with the speed of the procedure so I would like to ask the following:

    1.- Should I open the whole file and then parse it? Or should I open it part by part and parse it that way? Which would be the best way?
    2.- Which will be the best way to save the file so it won't be so big? Right now I am saving it as ASCII, I think.
    3.- Which would be the best way to attach strings? I read plenderj's string attachment procedure but something is wrong with it... Should I use an array of Bytes? If so... How can I do that?
    4.- Which would be the best way to draw a picture on a form? I use the API set Pixel... Some would say BitBtl but since the File Format is of my own, how could I use it?

    Thanks for your attention... Don't answer all questions if you can't, if you are able to help me with just one of them I will be very happy.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to Open a File And Parse It?

    1- Yes, as long as it isn't many megabytes.
    2- Nothing wrong with text files
    3- Attach to what? Just use
    VB Code:
    1. Print #1, myStr
    4- the LINE method works, and lets you draw and fill boxes using different colors.

  3. #3

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    Thanks for answering, David. What I am doing is to create my own format for saving pictures but they are a little bigger than gifs and it takes longer to load a text file and parse it than doing it from a bitmap and I think it's because of the time it takes to manipulate strings.

    The file can turn into MBs if the picture is big and that's why I am asking...

    (Added: )
    When saving a picture I need to attach a new part to the string that is holding the whole file. I do it this way:


    VB Code:
    1. strFile = strFile & strLine
    Last edited by Tec-Nico; Aug 20th, 2005 at 10:46 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Best way to Open a File And Parse It?

    I don't see why you are creating your own picture format. There are plenty of good ones out there. JPG are compressed, but lose if you zoom in close enough.
    RAW is the newest format out there. I saw it demo'd the other night.
    It is loseless, and a little bigger than jpg. Newer digital cameras use it.

  5. #5

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    I am creating the format for my own program.. so the painting will be faster and the picture would store some features that would be easier to read from the program than saving them into a different file. Am I making sense?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Best way to Open a File And Parse It?

    Yep. I would use a UDT header at the start of the file to describe the picture following it, and then read it in two parts, UDT first, and using the descriptive info in the UDT, read the picture itself. You can use Get in binary mode to read bits of files.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Best way to Open a File And Parse It?

    Uh, I just read your bit of post about strings. Don't use them. Use byte arrays, much much quicker. You have control over exactly where everything is at all times; something that's hard to achieve with strings unless you know exactly what you're doing, and it is much more complex.

  8. #8

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    could you give me an example penagate?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Best way to Open a File And Parse It?

    Sure.

    Load a UDT file header:
    VB Code:
    1. ' Something like this
    2. Type FileHeaderThing
    3.     szPictureName As String * 255
    4.     lpPictureData As Long    ' Pointer to the start of the picture data within the file
    5.     cchPictureLen As Long    ' Length of the picture in bytes
    6.     dwWidthPx As Long
    7.     dwHeightPx As Long
    8.     dwBitsppx As Long
    9. End Type
    10.  
    11. ' Open the file
    12. Dim lFF As Long: lFF = FreeFile()
    13. Open [filename] For Binary Lock Write As #lFF
    14.  
    15. ' Read the header
    16. Dim udtHeader As FileHeaderThing
    17. Get #lFF, , udtHeader
    18.  
    19. ' Get the picture data
    20. Dim chPicture() As Byte
    21. ReDim chPicture(udtHeader.cchPictureLen)
    22. Get #lFF, (udtHeader.lpPictureData), chPicture

    Hope that gets you started

  10. #10

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    and what about using arrays of bytes instead of strings?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Best way to Open a File And Parse It?

    Um, under the " 'Get the picture data" heading

    How you would display a picture contained in a byte array, I don't know. That is a different matter. Are you creating your own picture format or saving a StdPicture sort of object?

  12. #12

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    I see it.. but I meant for when I would have to read every pixel from a picturebox in order to save the file.. Instead of using the string I am putting all into I would like to see how to do something like:

    a = a & "b"

    Should I use the redim everytime I am going to add a new byte? How can I handle it when I am going to save it?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Best way to Open a File And Parse It?

    You are reading each pixel individually?? Oh goodness, that is going to be slow... Just Put the contents of the picture into the file, and then when you want to read it, read it into a StdPicture object.

    VB Code:
    1. ' Save it
    2. Dim udtHeader As FileHeaderThing
    3.  
    4. ' fill the UDT with details
    5. udtHeader.lpPictureData = LenB(udtHeader) + 1
    6.  
    7. Open [filename] For Binary Lock Write As #lFF
    8. Put #lFF, , udtHeader
    9. Put #lFF, (udtHeader.lpPictureData), picPicture.Picture
    10.  
    11. ' Read it
    12. Dim objPic As StdPicture
    13. Get #lFF, , udtHeader
    14. Get #lFF, (udtHeader.lpPictureData), objPic
    15.  
    16. Set picPicture = objPic
    17. Set objPic = Nothing

    I think that should work.

  14. #14

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    yes, because i am translating the picture into my format.. i am going to continue this tomorrow.. i am very tired now. thanks for your answers and i hope i can ask you more if i have some other doubts.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  15. #15

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    Hmmm... Penagate, I have another doubt.
    The problem is that the size of every line of the picture has a different length.

    Example:
    Let's say there are 30 red pixels... So my data would look like:

    255x30

    How would I be able to read that when using Bytes? And how would I be able to store it? Right now I am still playing with the code but I would love to have more pointers so I wouldn't have to spend a lot of time analyzing and trying to understand what I am doing..
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  16. #16
    Addicted Member VbMafia's Avatar
    Join Date
    Sep 2004
    Location
    Pilipinas
    Posts
    177

    Re: Best way to Open a File And Parse It?

    You can use API to do not process
    " I never did anything worth doing entirely by accident.... Almost none of my inventions were derived in that manner. They were achieved by having trained myself to be analytical and to endure and tolerate hard work."

    " Many of life's failures are experienced by people who did not realize how close they were to success when they gave up. "
    - Thomas Alva Edison-

    In God We Trust

  17. #17
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Best way to Open a File And Parse It?

    This might help with the graphics problems

    Those all are 8-bit images made in real time, but it shouldn't be impossible to change it to 32-bits. It actually might be easier to handle it in 32-bits as you don't need to worry about "dividable by four bytes per row" thingy. The codes introduce the use of DIBs, Device Independent Bitmaps.

    If interested, I also have a Transport Tycoon Deluxe GRF file reader, which reads several sprites from a TTD graphics file and displays the selected one. 8-bit as well, so no many changes in the techinal side...

  18. #18

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    Thank you for your help guys.

    When I thought about handling byte arrays instead of strings your name came to my mind, Merri. That was also because of the speed issue.

    I appreciate every comment made to me regarding this project that is about to be started. After making some tests with get and set pixel API, I developed my own format but translating it was pretty slow and so was reading it from the file even when drawing the picture was faster.

    What's special about this format? It might have been already discovered by Merri: The pictures are built out of what could be a description, so the colors might change depending on this. The idea is to have general pictures and then customize them based on small text descriptions.

    The problem here is that the files need to be the smallest possible and able to support animations.. They are going to be send through the internet and the transfer should be fast enough so people wouldn't notice when it happens.

    I am going to take a look at the MetaBalls project and I am interested in the Transport Tycoon Delux GRF file reader and in everyone's suggestions. Thank you to all
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  19. #19
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Best way to Open a File And Parse It?

    Here is the GRF reader. Originally meant to be a convertor, but I never got as far as coding the editing and saving. TTD community is still alive very well and the game hasn't died. Going strong for a ten years old game

    Anyways, you ought to find good tips for handling a file in the project.
    Attached Files Attached Files

  20. #20

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: Best way to Open a File And Parse It?

    Thanks for the reader... I will keep posting my doubts here if it doesn't bother you, guys. A ten year-old game? is it going to be finished soon?

    I hope the tips are in english this time, and again, I really appreciate your help!
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  21. #21
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Best way to Open a File And Parse It?

    Quote Originally Posted by Tec-Nico
    A ten year-old game? is it going to be finished soon?
    Seems very unlikely, they talk actively about TTD development on IRC, there are several projects going on which include creation of new vehicles, new tools for building them, TTDPatch which edits the original game, OpenTTD which is a custom coded C version of the game... just a lot of stuff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width