?? Odi: File I/O

Real-world file system operations and data persistence.

Use Case 1: Configuration File Manager

// config_manager.ifa - JSON config file handling

ayanmo CONFIG_FILE = "app.config.json";

ise load_config() {
    ti (!Odi.exists(CONFIG_FILE)) {
        // Return defaults if no config file
        padap? {
            "theme": "dark",
            "language": "en",
            "auto_save": otito,
            "timeout": 30
        };
    }
    
    ayanmo content = Odi.read(CONFIG_FILE);
    padap? Ogbe.parse_json(content);
}

ise save_config(config) {
    ayanmo json = Ogbe.format_json(config);
    Odi.write(CONFIG_FILE, json);
    Irosu.fo("? Configuration saved");
}

ise get_config(key) {
    ayanmo config = load_config();
    padap? config[key];
}

ise set_config(key, value) {
    ayanmo config = load_config();
    config[key] = value;
    save_config(config);
}

// Usage
Irosu.fo("Theme: " + get_config("theme"));
set_config("theme", "light");
Irosu.fo("New theme: " + get_config("theme"));

Use Case 2: Log File Analyzer

// log_analyzer.ifa - Parse and analyze log files

ise analyze_log(path) {
    ti (!Odi.exists(path)) {
        padap? Okanran.error("Log file not found: " + path);
    }
    
    ayanmo content = Odi.read(path);
    ayanmo lines = Ika.split(content, "\n");
    
    ayanmo stats = {
        "total": 0,
        "errors": 0,
        "warnings": 0,
        "info": 0
    };
    
    fun line ninu lines {
        ti (Ika.len(line) > 0) {
            stats["total"] = stats["total"] + 1;
            
            ti (Ika.contains(line, "[ERROR]")) {
                stats["errors"] = stats["errors"] + 1;
            } bib?k? ti (Ika.contains(line, "[WARN]")) {
                stats["warnings"] = stats["warnings"] + 1;
            } bib?k? ti (Ika.contains(line, "[INFO]")) {
                stats["info"] = stats["info"] + 1;
            }
        }
    }
    
    padap? stats;
}

// Usage
ayanmo stats = analyze_log("server.log");
Irosu.fo("Log Analysis:");
Irosu.fo("  Total lines: " + stats["total"]);
Irosu.fo("  Errors: " + stats["errors"]);
Irosu.fo("  Warnings: " + stats["warnings"]);
Irosu.fo("  Info: " + stats["info"]);

Use Case 3: CSV Data Processor

// csv_processor.ifa - Read and process CSV files

ise read_csv(path) {
    ayanmo content = Odi.read(path);
    ayanmo lines = Ika.split(content, "\n");
    ayanmo rows = [];
    
    fun line ninu lines {
        ti (Ika.len(Ika.trim(line)) > 0) {
            ayanmo cells = Ika.split(line, ",");
            ayanmo rows = Ogunda.push(rows, cells);
        }
    }
    
    padap? rows;
}

ise write_csv(path, rows) {
    ayanmo lines = [];
    fun row ninu rows {
        ayanmo line = Ika.join(row, ",");
        ayanmo lines = Ogunda.push(lines, line);
    }
    ayanmo content = Ika.join(lines, "\n");
    Odi.write(path, content);
}

// Calculate averages from sales.csv
ayanmo data = read_csv("sales.csv");
ayanmo header = Ogunda.first(data);
ayanmo rows = Ogunda.slice(data, 1, Ogunda.len(data));

ayanmo total = 0;
fun row ninu rows {
    ayanmo amount = Ogbe.parse_float(Ogunda.get(row, 2));
    ayanmo total = total + amount;
}

ayanmo avg = total / Ogunda.len(rows);
Irosu.fo("Average sale: $" + avg);

Use Case 4: File Backup System

// backup.ifa - Create timestamped backups

ise backup_file(source) {
    ti (!Odi.exists(source)) {
        Irosu.kigbe("Source file not found: " + source);
        padap? iro;
    }
    
    // Create backup directory
    ti (!Odi.exists("backups")) {
        Odi.mkdir("backups");
    }
    
    // Generate backup filename with timestamp
    ayanmo timestamp = Iwori.format(Iwori.now(), "%Y%m%d_%H%M%S");
    ayanmo parts = Ika.split(source, ".");
    ayanmo name = Ogunda.first(parts);
    ayanmo ext = Ogunda.last(parts);
    ayanmo backup_path = "backups/" + name + "_" + timestamp + "." + ext;
    
    // Copy file
    ayanmo content = Odi.read(source);
    Odi.write(backup_path, content);
    
    Irosu.fo("? Backup created: " + backup_path);
    padap? otito;
}

ise cleanup_old_backups(max_count) {
    ayanmo files = Odi.list("backups");
    ayanmo sorted = Osa.sort(files);
    
    ti (Ogunda.len(sorted) > max_count) {
        ayanmo to_delete = Ogunda.len(sorted) - max_count;
        ayanmo i = 0;
        nigba (i < to_delete) {
            ayanmo file = Ogunda.get(sorted, i);
            Odi.delete("backups/" + file);
            Irosu.fo("Deleted old backup: " + file);
            ayanmo i = i + 1;
        }
    }
}

// Usage
backup_file("database.db");
cleanup_old_backups(5);  // Keep only 5 most recent

Use Case 5: Watch Directory for Changes

// watcher.ifa - Simple file watcher

ise get_file_states(dir) {
    ayanmo files = Odi.list(dir);
    ayanmo states = {};
    
    fun file ninu files {
        ayanmo path = dir + "/" + file;
        ayanmo hash = Irete.sha256(Odi.read(path));
        states[file] = hash;
    }
    
    padap? states;
}

ise watch_directory(dir, interval, callback) {
    ayanmo prev_states = get_file_states(dir);
    Irosu.fo("Watching " + dir + " for changes...");
    
    nigba (otito) {
        Oyeku.sleep(interval);
        ayanmo current = get_file_states(dir);
        
        // Check for changes
        fun file key(current) {
            ti (prev_states[file] == nil) {
                callback("created", file);
            } bib?k? ti (prev_states[file] != current[file]) {
                callback("modified", file);
            }
        }
        
        fun file key(prev_states) {
            ti (current[file] == nil) {
                callback("deleted", file);
            }
        }
        
        ayanmo prev_states = current;
    }
}

// Usage
watch_directory("./src", 1000, |action, file| {
    Irosu.fo("[" + action + "] " + file);
});