<commit_before>package com.github.chrisblutz.trinity.lang.types.nativeutils;

import com.github.chrisblutz.trinity.files.FilePrivilege;
import com.github.chrisblutz.trinity.files.FileUtils;
import com.github.chrisblutz.trinity.lang.TYObject;
import com.github.chrisblutz.trinity.lang.errors.Errors;
import com.github.chrisblutz.trinity.lang.types.bool.TYBoolean;
import com.github.chrisblutz.trinity.lang.types.strings.TYString;
import com.github.chrisblutz.trinity.natives.TrinityNatives;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author <NAME>
 */
class NativeFileSystem {
    
    protected static void register() {
        
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "normalize", (runtime, thisObj, params) -> {
            
            TYObject object = runtime.getVariable("path");
            
            return new TYString(new File(TrinityNatives.cast(TYString.class, object).getInternalString()).getAbsolutePath());
        });
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "create", (runtime, thisObj, params) -> {
            
            TYObject object = runtime.getVariable("path");
            
            try {
                
                File f = new File(TrinityNatives.cast(TYString.class, object).getInternalString());
                if (f.getParentFile() != null && !f.getParentFile().exists()) {
                    
                    if (f.getParentFile().mkdirs()) {
                        
                        Errors.throwError(Errors.Classes.IO_ERROR, runtime, "Unable to create parent directories.");
                    }
                }
                return TYBoolean.valueFor(f.createNewFile());
                
            } catch (IOException e) {
                
                Errors.throwError(Errors.Classes.IO_ERROR, runtime, "An error occurred creating a file at '" + ((TYString) object).getInternalString() + "'.");
            }
            
            return TYBoolean.FALSE;
        });
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "open", (runtime, thisObj, params) -> {
            
            TYObject path = runtime.getVariable("path");
            TYObject privilege = runtime.getVariable("privileges");
            TYObject append = runtime.getVariable("append");
            
            List<FilePrivilege> privileges = new ArrayList<>();
            String privilegeStr = TrinityNatives.cast(TYString.class, privilege).getInternalString();
            if (privilegeStr.contains("r")) {
                
                privileges.add(FilePrivilege.READ);
            }
            if (privilegeStr.contains("w")) {
                
                privileges.add(FilePrivilege.WRITE);<commit_msg>Update file creation to fix parent directory creation<commit_after>package com.github.chrisblutz.trinity.lang.types.nativeutils;

import com.github.chrisblutz.trinity.files.FilePrivilege;
import com.github.chrisblutz.trinity.files.FileUtils;
import com.github.chrisblutz.trinity.lang.TYObject;
import com.github.chrisblutz.trinity.lang.errors.Errors;
import com.github.chrisblutz.trinity.lang.types.bool.TYBoolean;
import com.github.chrisblutz.trinity.lang.types.strings.TYString;
import com.github.chrisblutz.trinity.natives.TrinityNatives;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author <NAME>
 */
class NativeFileSystem {
    
    protected static void register() {
        
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "normalize", (runtime, thisObj, params) -> {
            
            TYObject object = runtime.getVariable("path");
            
            return new TYString(new File(TrinityNatives.cast(TYString.class, object).getInternalString()).getAbsolutePath());
        });
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "create", (runtime, thisObj, params) -> {
            
            TYObject object = runtime.getVariable("path");
            
            try {
                
                File f = new File(TrinityNatives.cast(TYString.class, object).getInternalString());
                String path = f.getAbsolutePath();
                String parent = path.substring(0, path.replace(File.separatorChar, '/').lastIndexOf('/'));
                
                File parentDir = new File(parent);
                if (!parentDir.exists() && !parentDir.mkdirs()) {
                        
                        Errors.throwError(Errors.Classes.IO_ERROR, runtime, "Unable to create parent directories.");
                }
                return TYBoolean.valueFor(f.createNewFile());
                
            } catch (IOException e) {
                
                Errors.throwError(Errors.Classes.IO_ERROR, runtime, "An error occurred creating a file at '" + ((TYString) object).getInternalString() + "'.");
            }
            
            return TYBoolean.FALSE;
        });
        TrinityNatives.registerMethod(TrinityNatives.Classes.FILE_SYSTEM, "open", (runtime, thisObj, params) -> {
            
            TYObject path = runtime.getVariable("path");
            TYObject privilege = runtime.getVariable("privileges");
            TYObject append = runtime.getVariable("append");
            
            List<FilePrivilege> privileges = new ArrayList<>();
            String privilegeStr = TrinityNatives.cast(TYString.class, privilege).getInternalString();
            if (privilegeStr.contains("r")) {
                
                privileges.add(FilePrivilege.READ);
            }
            if (privilegeStr.contains("w")) {
                
                privileges.add(FilePrivilege.WRITE);
{'fraction_non_alphanumeric': 0.11233442284534664, 'fraction_numerical': 0.0001720282126268708, 'mean_word_length': 2.1753140360458767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755522', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1530, 'n_words': 251}