Skip to content

Clarify file position as being byte offsets (M24, M26) - #624

Merged
thinkyhead merged 2 commits into
MarlinFirmware:masterfrom
EvansJahja:patch-1
Aug 21, 2026
Merged

Clarify file position as being byte offsets (M24, M26)#624
thinkyhead merged 2 commits into
MarlinFirmware:masterfrom
EvansJahja:patch-1

Conversation

@EvansJahja

@EvansJahja EvansJahja commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

I was trying to resume printing from a byte offset in file. When I read the description, it wasn't clear what "position" meant, I almost misinterpret it as line number.

I would like to make it clear for future readers.

@EvansJahja EvansJahja changed the title Clarify description for resume position parameter Clarify file position as being byte offsets (M24, M26) Aug 3, 2026
@EvansJahja
EvansJahja marked this pull request as ready for review August 3, 2026 11:48
@DerAndere1

DerAndere1 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

For M32, the parameter S is the starting file offset. I guess it is also a byte offset?

Can we explain a platform independent way of getting the byte offset of a unique multi-line string in an ASCII-encoded text file ? Maybe mention a VS code extension and explain how to use it or provide something like this AI-generated python script?

import re

def find_multiline_byte_offset(file_path: str, search_string: str) -> int:
    """
    Finds the exact byte offset of a multi-line ASCII string in a file,
    handling any platform-specific line breaks (\r\n, \n, \r) seamlessly.
    """
    # 1. Read the file as raw bytes to preserve exact byte positions
    with open(file_path, 'rb') as f:
        file_bytes = f.read()
    
    # 2. Split search string by any newline variant to isolate text content
    search_lines = search_string.splitlines()
    
    # 3. Escape and encode each line into ASCII bytes
    encoded_lines = [re.escape(line.encode('ascii')) for line in search_lines]
    
    # 4. Join lines with a regex pattern that matches any platform line break
    # This allows a match regardless of whether the file uses CRLF, LF, or CR
    line_break_pattern = b'(?:\\r\\n|\\r|\\n)'
    regex_pattern = line_break_pattern.join(encoded_lines)
    
    # 5. Search the raw bytes
    match = re.search(regex_pattern, file_bytes)
    
    if match:
        return match.start()  # Returns the exact byte offset
    return -1  # Return -1 if the string is not found

# --- Example Usage ---
if __name__ == "__main__":
    # Define your multi-line target string (using standard \n in your script)
    target_string = "G1 X20\nG1 Y20"
    file_name = "example_ascii_file.txt"
    
    # Simple test file creation with Windows-style line endings (\r\n)
    dummy_content = b"G80; absolute positioning\r\nG28; home\r\nG1 X10 Y10 Z100 F500\r\nG1 X20\r\nG1 Y20\r\nG1 X10\r\nM400; finish moves"
    with open(file_name, "wb") as test_file:
        test_file.write(dummy_content)
        
    # Execute search
    offset = find_multiline_byte_offset(file_name, target_string)
    
    if offset != -1:
        print(f"Success: Multi-line string found at byte offset {offset}.")
    else:
        print("Error: Multi-line string not found.")

@EvansJahja

Copy link
Copy Markdown
Contributor Author

The intention for my edit is so that anyone reading would understand that 'file position' here is the same as byte offset.

Can we explain a platform independent way of getting the byte offset of...

Scope of this PR is for the G-code comments, which are reference documents. I appreciate your idea that this should be explained with a more "step-by-step" fashion, I think it's a wonderful idea! But because it is a 'How To', it is better to put it on a separate place such as in ./_features folder explaining recovery in general.

@thinkyhead

Copy link
Copy Markdown
Member

For the updated site I'm going for more of a "User Manual" approach, so this will definitely be an important detail to emphasize in the File Printing section.

@thinkyhead
thinkyhead merged commit e7822c8 into MarlinFirmware:master Aug 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants