{"id":29091107,"url":"https://github.com/devmuaz/dart-io-ffi","last_synced_at":"2026-05-06T11:31:28.071Z","repository":{"id":301593438,"uuid":"1009752195","full_name":"devmuaz/dart-io-ffi","owner":"devmuaz","description":"A Dart console application that demonstrates Foreign Function Interface (FFI) by reading text files using native macOS APIs.","archived":false,"fork":false,"pushed_at":"2025-06-27T17:15:42.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-27T17:45:10.999Z","etag":null,"topics":["c","cpp","dart","ffi","native"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devmuaz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-27T16:45:38.000Z","updated_at":"2025-06-27T17:15:46.000Z","dependencies_parsed_at":"2025-06-27T17:45:14.833Z","dependency_job_id":"555fae66-70d0-421d-abbf-0b0637648f45","html_url":"https://github.com/devmuaz/dart-io-ffi","commit_stats":null,"previous_names":["devmuaz/dart-io-ffi"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/devmuaz/dart-io-ffi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmuaz%2Fdart-io-ffi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmuaz%2Fdart-io-ffi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmuaz%2Fdart-io-ffi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmuaz%2Fdart-io-ffi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devmuaz","download_url":"https://codeload.github.com/devmuaz/dart-io-ffi/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmuaz%2Fdart-io-ffi/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262382729,"owners_count":23302296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","dart","ffi","native"],"created_at":"2025-06-28T06:05:35.030Z","updated_at":"2026-05-06T11:31:23.051Z","avatar_url":"https://github.com/devmuaz.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FFI File IO\n\nA Dart console application that demonstrates Foreign Function Interface (FFI) by reading text files using native C APIs. This project showcases how to bridge Dart and C code to perform file I/O operations using low-level POSIX system calls.\n\n## Full Medium Article\n\nYou can read the full toturial article on my Medium: [Beyond Dart: Tapping into C's Power with FFI](https://devmuaz.medium.com/beyond-dart-tapping-into-cs-power-with-ffi-e3ef49990d56)\n\n## Features\n\n- **Native File Reading**: Uses POSIX APIs (`open`, `read`, `fstat`) for file operations\n- **FFI Integration**: Seamless bridge between Dart and C code using `dart:ffi`\n- **Memory Management**: Proper allocation and cleanup with automatic memory management\n- **File Validation**: Built-in file existence checking using `access()` system call\n- **Error Handling**: Comprehensive error handling for file operations and edge cases\n- **Cross-Platform**: Designed for macOS but easily adaptable to other Unix-like systems\n\n## Project Structure\n\n```\ndart_io_ffi/\n├── bin/\n│   └── main.dart              # Main application entry point\n├── lib/\n│   └── native_file_reader.dart # Dart FFI wrapper class\n├── native/\n│   ├── file_reader.c          # C implementation of file operations\n│   ├── file_reader.h          # C header file with function declarations\n│   └── build/\n│       └── libfile_reader.dylib # Compiled shared library\n├── build.sh                   # Build script for the native library\n├── pubspec.yaml              # Dart package configuration\n└── README.md                 # This file\n```\n\n## Dependencies\n\n- **Dart SDK**: ^3.5.3\n- **ffi**: ^2.1.3 - For Foreign Function Interface support\n- **path**: ^1.8.0 - For cross-platform path manipulation\n\n## Building and Running\n\n### 1. Build the Native Library\n\nFirst, compile the C code into a shared library:\n\n```bash\n./build.sh\n```\n\nThis will create `native/build/libfile_reader.dylib` on macOS.\n\n### 2. Install Dart Dependencies\n\n```bash\ndart pub get\n```\n\n### 3. Run the Application\n\n```bash\ndart run bin/main.dart \u003cfile_path\u003e\n```\n\n**Example:**\n\n```bash\ndart run bin/main.dart README.md\ndart run bin/main.dart pubspec.yaml\n```\n\n## How It Works\n\n### C Implementation (`native/file_reader.c`)\n\nThe C code provides three main functions:\n\n- `read_file(const char *file_path)`: Opens a file, reads its contents, and returns a malloc'd string\n- `file_exists(const char *file_path)`: Checks if a file exists using the `access()` system call\n- `free_string(char *str)`: Properly frees memory allocated by `read_file`\n\n### Dart FFI Wrapper (`lib/native_file_reader.dart`)\n\nThe `NativeFileReader` class:\n\n1. Loads the shared library dynamically\n2. Maps C functions to Dart function signatures\n3. Handles string conversion between Dart and C\n4. Manages memory allocation and cleanup\n5. Provides a clean Dart API for file operations\n\n### Main Application (`bin/main.dart`)\n\n- Accepts a file path as a command-line argument\n- Validates file existence before attempting to read\n- Displays file content and metadata\n- Handles errors gracefully\n\n## Technical Details\n\n### FFI Function Signatures\n\n```dart\ntypedef ReadFileC = Pointer\u003cUtf8\u003e Function(Pointer\u003cUtf8\u003e);\ntypedef FileExistsC = Int32 Function(Pointer\u003cUtf8\u003e);\ntypedef FreeStringC = Void Function(Pointer\u003cUtf8\u003e);\n```\n\n### Memory Management\n\n- C functions allocate memory using `malloc()`\n- Dart code ensures proper cleanup by calling `free_string()`\n- Native UTF-8 strings are converted to Dart strings safely\n- All temporary pointers are freed in `finally` blocks\n\n## Error Handling\n\nThe application handles various error conditions:\n\n- Non-existent files\n- Permission errors\n- Memory allocation failures\n- Invalid file paths\n- System call failures\n\n## Platform Notes\n\n- **macOS**: Uses `.dylib` shared libraries\n- **Linux**: Would use `.so` shared libraries\n- **Windows**: Would use `.dll` dynamic libraries\n\nTo adapt for other platforms, modify the library extension in `_getLibraryPath()` and update the build script accordingly.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevmuaz%2Fdart-io-ffi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevmuaz%2Fdart-io-ffi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevmuaz%2Fdart-io-ffi/lists"}