Repository: raspberrypi/pico-sdk
File: src/rp2_common/pico_lwip/tools/makefsdata.py (Python)

Code before:
    results.append({'data': bytes(data, "utf-8"), 'comment': comment});

    # user agent
    data = f"Server: {HTTPD_SERVER_AGENT}\r\n"
    comment = f"\"Server: {HTTPD_SERVER_AGENT}\" ({len(data)} chars)"
    results.append({'data': bytes(data, "utf-8"), 'comment': comment});

    if file.suffix not in LWIP_HTTPD_SSI_EXTENSIONS:
        # content length
        file_size = file.stat().st_size
        data = f"Content-Length: {file_size}\r\n"
        comment = f"\"Content-Length: {file_size}\" ({len(data)} chars)"
        results.append({'data': bytes(data, "utf-8"), 'comment': comment});

    # content type
    data = f"Content-Type: {content_type}\r\n\r\n"
    comment = f"\"Content-Type: {content_type}\" ({len(data)} chars)"
    results.append({'data': bytes(data, "utf-8"), 'comment': comment});

    # file contents
    data = file.read_bytes()
    comment = f"raw file data ({len(data)} bytes)"
    results.append({'data': data, 'comment': comment});

    return results;

def process_file_list(fd, input):
    data = []
    fd.write("#include \"lwip/apps/fs.h\"\n")
    fd.write("\n")
    # generate the page contents
    input_dir = None
    for name in input:
        file = Path(name)
        if not file.is_file():
            raise RuntimeError(f"File not found: {name}")
        # Take the input directory from the first file
        if input_dir is None:
            input_dir = file.parent
        results = process_file(input_dir, file)

        # make a variable name
        var_name = str(file.relative_to(input_dir))
        var_name = var_name.replace(".", "_")
        var_name = var_name.replace("/", "_")
        data_var = f"data_{var_name}"
        file_var = f"file_{var_name}"

        # variable containing the raw data
        fd.write(f"static const unsigned char {data_var}[] = {{\n")
        for entry in results:

Review comment (refactor):
I guess using `len(data)` as the suffix means that if the first and 6th items are named identically, then the "first duplicate name" would get a suffix of `_5`, which may be unexpected?

An alternative approach might be to add a function like:
```python3
unique_names = set()
def get_unique_name(name):
    if name in unique_names:
        counter = 1
        new_name = f"{name}_{counter}"
        while new_name in unique_names:
            counter += 1
            new_name = f"{name}_{counter}"
        name = new_name
    unique_names.add(name)
    return name
```
and then you could just do `var_name = get_unique_name(var_name)` and then any duplicates would just be numbered sequentially (starting at 1).

But of course this is your PR, so it's up to you / @peterharperuk if you decide to incorporate this suggestion :slightly_smiling_face: 

Code after:
    results.append({'data': data, 'comment': comment});

    return results;

def process_file_list(fd, input):
    data = []
    fd.write("#include \"lwip/apps/fs.h\"\n")
    fd.write("\n")
    # generate the page contents
    input_dir = None
    for name in input:
        file = Path(name)
        if not file.is_file():
            raise RuntimeError(f"File not found: {name}")
        # Take the input directory from the first file
        if input_dir is None:
            input_dir = file.parent
        results = process_file(input_dir, file)

        # make a variable name
        var_name = str(file.relative_to(input_dir))
        var_name = re.sub(r"\W+", "_", var_name, flags=re.ASCII)

        # Add a suffix if the variable name is used already
        if any(d["data_var"] == f"data_{var_name}" for d in data):
            var_name += f"_{len(data)}"

        data_var = f"data_{var_name}"
        file_var = f"file_{var_name}"

        # variable containing the raw data
        fd.write(f"static const unsigned char {data_var}[] = {{\n")
        for entry in results:
            fd.write(f"\n    /* {entry['comment']} */\n")
            byte_count = 0;
            for b in entry['data']:
                if byte_count % 16 == 0:
                    fd.write("    ")
                byte_count += 1
                fd.write(f"0x{b:02x},")
                if byte_count % 16 == 0:
                    fd.write("\n")
            if byte_count % 16 != 0:
                fd.write("\n")
        fd.write(f"}};\n\n")

        # set the flags
        flags = "FS_FILE_FLAGS_HEADER_INCLUDED"
        if file.suffix not in LWIP_HTTPD_SSI_EXTENSIONS:
            flags += " | FS_FILE_FLAGS_HEADER_PERSISTENT"
        else: