Task 126: .DART File Format

Task 126: .DART File Format

1. List of Properties

Based on the specifications, .DART files are text-based source code files for the Dart programming language. They do not have a binary structure or specific headers like many proprietary formats. Instead, they contain plain text following Dart syntax rules (object-oriented, C-like syntax). The "properties intrinsic to its file system" refer to standard file system metadata associated with any file, including .DART files. These properties are not unique to the format but are accessible via file system APIs in most operating systems.

Here is the list of key file system properties:

  • File Name: The name of the file (e.g., "main.dart").
  • File Path: The full path to the file on the system.
  • File Size: The size of the file in bytes.
  • Creation Time: The timestamp when the file was created.
  • Modification Time: The timestamp when the file was last modified.
  • Access Time: The timestamp when the file was last accessed.
  • Permissions: The file's access mode (e.g., read/write/execute permissions, often represented as an octal value like 0644).
  • Owner UID: The user ID of the file owner.
  • Group GID: The group ID of the file.
  • Inode Number: The inode number (unique identifier on Unix-like systems).

Note: Some properties (e.g., inode, UID, GID) may not be available or relevant on all platforms (e.g., Windows vs. Unix-like). In browser-based JavaScript, access is limited to basic properties like name, size, type, and last modified date.

3. Ghost Blog Embedded HTML JavaScript for Drag-and-Drop

This is an embeddable HTML snippet with JavaScript that can be placed in a Ghost blog post (or any HTML context). It creates a drop zone where users can drag and drop a .DART file. Upon dropping, it dumps the available properties to the screen using the browser's File API. Note: Browser security limits access to full file system metadata; only basic properties are shown. The content can be read and displayed if needed, but here we focus on properties.

Drag and drop a .DART file here

4. Python Class

import os
import time

class DARTFile:
    def __init__(self, path):
        self.path = path
        if not os.path.exists(path) or not path.endswith('.DART'):
            raise ValueError("Invalid .DART file path")
        self.stat = os.stat(path)

    def read(self):
        """Read and decode the file content as text."""
        with open(self.path, 'r', encoding='utf-8') as f:
            return f.read()

    def write(self, content):
        """Write text content to the file."""
        with open(self.path, 'w', encoding='utf-8') as f:
            f.write(content)

    def print_properties(self):
        """Print all file system properties."""
        print(f"File Name: {os.path.basename(self.path)}")
        print(f"File Path: {self.path}")
        print(f"File Size: {self.stat.st_size} bytes")
        print(f"Creation Time: {time.ctime(self.stat.st_ctime)}")
        print(f"Modification Time: {time.ctime(self.stat.st_mtime)}")
        print(f"Access Time: {time.ctime(self.stat.st_atime)}")
        print(f"Permissions: {oct(self.stat.st_mode)}")
        print(f"Owner UID: {self.stat.st_uid}")
        print(f"Group GID: {self.stat.st_gid}")
        print(f"Inode Number: {self.stat.st_ino}")

# Example usage:
# dart_file = DARTFile('example.DART')
# print(dart_file.read())
# dart_file.write('New content')
# dart_file.print_properties()

5. Java Class

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DARTFile {
    private final Path path;
    private final BasicFileAttributes attrs;

    public DARTFile(String filePath) throws IOException {
        this.path = Paths.get(filePath);
        if (!Files.exists(path) || !filePath.endsWith(".DART")) {
            throw new IllegalArgumentException("Invalid .DART file path");
        }
        this.attrs = Files.readAttributes(path, BasicFileAttributes.class);
    }

    public String read() throws IOException {
        return Files.readString(path);
    }

    public void write(String content) throws IOException {
        Files.writeString(path, content);
    }

    public void printProperties() throws IOException {
        System.out.println("File Name: " + path.getFileName());
        System.out.println("File Path: " + path.toAbsolutePath());
        System.out.println("File Size: " + attrs.size() + " bytes");
        System.out.println("Creation Time: " + formatDate(attrs.creationTime().toMillis()));
        System.out.println("Modification Time: " + formatDate(attrs.lastModifiedTime().toMillis()));
        System.out.println("Access Time: " + formatDate(attrs.lastAccessTime().toMillis()));

        // POSIX-specific (Unix-like)
        try {
            PosixFileAttributes posixAttrs = Files.readAttributes(path, PosixFileAttributes.class);
            System.out.println("Permissions: " + PosixFilePermissions.toString(posixAttrs.permissions()));
            System.out.println("Owner UID: " + posixAttrs.owner().getName());  // Name instead of UID
            System.out.println("Group GID: " + posixAttrs.group().getName());  // Name instead of GID
            System.out.println("Inode Number: " + attrs.fileKey());  // Approximation
        } catch (UnsupportedOperationException e) {
            System.out.println("POSIX attributes not supported on this platform.");
        }
    }

    private String formatDate(long millis) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(millis));
    }

    // Example usage:
    // public static void main(String[] args) throws IOException {
    //     DARTFile dartFile = new DARTFile("example.DART");
    //     System.out.println(dartFile.read());
    //     dartFile.write("New content");
    //     dartFile.printProperties();
    // }
}

6. JavaScript Class (Node.js)

const fs = require('fs');
const pathModule = require('path');

class DARTFile {
  constructor(filePath) {
    this.path = filePath;
    if (!fs.existsSync(filePath) || !filePath.endsWith('.DART')) {
      throw new Error('Invalid .DART file path');
    }
    this.stat = fs.statSync(filePath);
  }

  read() {
    return fs.readFileSync(this.path, 'utf-8');
  }

  write(content) {
    fs.writeFileSync(this.path, content, 'utf-8');
  }

  printProperties() {
    console.log(`File Name: ${pathModule.basename(this.path)}`);
    console.log(`File Path: ${this.path}`);
    console.log(`File Size: ${this.stat.size} bytes`);
    console.log(`Creation Time: ${new Date(this.stat.birthtime).toLocaleString()}`);
    console.log(`Modification Time: ${new Date(this.stat.mtime).toLocaleString()}`);
    console.log(`Access Time: ${new Date(this.stat.atime).toLocaleString()}`);
    console.log(`Permissions: ${this.stat.mode.toString(8)}`);
    console.log(`Owner UID: ${this.stat.uid}`);
    console.log(`Group GID: ${this.stat.gid}`);
    console.log(`Inode Number: ${this.stat.ino}`);
  }
}

// Example usage:
// const dartFile = new DARTFile('example.DART');
// console.log(dartFile.read());
// dartFile.write('New content');
// dartFile.printProperties();

7. C++ Class (Assuming C++ for "c class" as C lacks native classes)

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <ctime>
#include <stdexcept>

class DARTFile {
private:
    std::string path;
    struct stat fileStat;

public:
    DARTFile(const std::string& filePath) : path(filePath) {
        if (stat(filePath.c_str(), &fileStat) != 0 || path.substr(path.find_last_of('.')) != ".DART") {
            throw std::invalid_argument("Invalid .DART file path");
        }
    }

    std::string read() {
        std::ifstream file(path);
        if (!file) {
            throw std::runtime_error("Failed to open file for reading");
        }
        std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
        return content;
    }

    void write(const std::string& content) {
        std::ofstream file(path);
        if (!file) {
            throw std::runtime_error("Failed to open file for writing");
        }
        file << content;
    }

    void printProperties() {
        std::cout << "File Name: " << path.substr(path.find_last_of("/\\") + 1) << std::endl;
        std::cout << "File Path: " << path << std::endl;
        std::cout << "File Size: " << fileStat.st_size << " bytes" << std::endl;
        std::cout << "Creation Time: " << std::ctime(&fileStat.st_ctime);
        std::cout << "Modification Time: " << std::ctime(&fileStat.st_mtime);
        std::cout << "Access Time: " << std::ctime(&fileStat.st_atime);
        std::cout << "Permissions: " << std::oct << fileStat.st_mode << std::endl;
        std::cout << "Owner UID: " << fileStat.st_uid << std::endl;
        std::cout << "Group GID: " << fileStat.st_gid << std::endl;
        std::cout << "Inode Number: " << fileStat.st_ino << std::endl;
    }
};

// Example usage:
// int main() {
//     try {
//         DARTFile dartFile("example.DART");
//         std::cout << dartFile.read() << std::endl;
//         dartFile.write("New content");
//         dartFile.printProperties();
//     } catch (const std::exception& e) {
//         std::cerr << e.what() << std::endl;
//     }
//     return 0;
// }