<commit_before>/** 
 * Copyright 2005-2007 <NAME>, <NAME>, <NAME>
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.builtin;

import java.io.File;
import org.apache.oro.io.GlobFilenameFilter;
import com.xruby.runtime.lang.*;
import com.xruby.runtime.value.*;

class Dir_chdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "No a directory - " + dir);
		}
		System.setProperty("user.dir", file.getAbsolutePath());
		return ObjectFactory.fixnum0;		
	}
}

class Dir_getwd extends RubyNoArgMethod {
	protected RubyValue run(RubyValue receiver, RubyBlock block) {
		return ObjectFactory.createString(System.getProperty("user.dir"));	
	}
}

class Dir_mkdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (file.mkdir()){
			return ObjectFactory.fixnum0;
		}
		throw new RubyException(RubyRuntime.RuntimeErrorClass, "Can't create directory - " + dir);
	}
}

class Dir_rmdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "Not a directory - " + dir);
		}
		if (file.delete()){
			return ObjectFactory.fixnum0;
		}
		throw new RubyException(RubyRuntime.RuntimeErrorClass, "Can't delete directory - " + dir);
	}
}

class Dir_entries extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "Not a directory - " + dir);
		}

		RubyArray files = new RubyArray();
		for (String f: file.list()){
			files.add(ObjectFactory.createString(f));
		}
		
		return files;
	}
}

class Dir_array_access extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		return Dir_glob.glob(receiver, arg);
	}
}

class Dir_glob extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		if (null == block) {
			return glob(receiver, arg);
		} else {
			return glob(receiver, arg, block);
		}
	}

	static RubyValue glob(RubyValue receiver, RubyValue arg) {
		String s = RubyTypesUtil.convertToString(arg).toString();
		String dir;
		String pattern;
		int index = s.lastIndexOf('/');
		if (index < 0) {
			dir = ".";
			pattern = s;
			
		} else {
			dir = s.substring(0, index);
			pattern = s.substring(index + 1);
		}
		
		RubyArray files = new RubyArray();
		File file = new File(dir);
		for (String f: file.list(new GlobFilenameFilter(pattern))){
			files.add(ObjectFactory.createString(dir + "/" + f));
		}
		
		return files;
	}

	static RubyValue glob(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String s = RubyTypesUtil.convertToString(arg).toString();
		String dir;
		String pattern;
		int index = s.lastIndexOf('/');
		if (index < 0) {
			dir = ".";
			pattern = s;
			
		} else {
			dir = s.substring(0, index);
			pattern = s.substring(index + 1);
		}
		
		File file = new File(dir);
		for (String f: file.list(new GlobFilenameFilter(pattern))){
			block.invoke(receiver, new RubyArray(ObjectFactory.createString(dir + "/" + f)));
		}

		return ObjectFactory.nilValue;
	}
}

public class DirClassBuilder {
	public static void initialize() {<commit_msg>avoid null reference<commit_after>/** 
 * Copyright 2005-2007 <NAME>, <NAME>, <NAME>
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.builtin;

import java.io.File;
import org.apache.oro.io.GlobFilenameFilter;
import com.xruby.runtime.lang.*;
import com.xruby.runtime.value.*;

class Dir_chdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "No a directory - " + dir);
		}
		System.setProperty("user.dir", file.getAbsolutePath());
		return ObjectFactory.fixnum0;		
	}
}

class Dir_getwd extends RubyNoArgMethod {
	protected RubyValue run(RubyValue receiver, RubyBlock block) {
		return ObjectFactory.createString(System.getProperty("user.dir"));	
	}
}

class Dir_mkdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (file.mkdir()){
			return ObjectFactory.fixnum0;
		}
		throw new RubyException(RubyRuntime.RuntimeErrorClass, "Can't create directory - " + dir);
	}
}

class Dir_rmdir extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "Not a directory - " + dir);
		}
		if (file.delete()){
			return ObjectFactory.fixnum0;
		}
		throw new RubyException(RubyRuntime.RuntimeErrorClass, "Can't delete directory - " + dir);
	}
}

class Dir_entries extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String dir = RubyTypesUtil.convertToString(arg).toString();
		File file = new File(dir);
		if (!file.isDirectory()){
			throw new RubyException(RubyRuntime.RuntimeErrorClass, "Not a directory - " + dir);
		}

		RubyArray files = new RubyArray();
		for (String f: file.list()){
			files.add(ObjectFactory.createString(f));
		}
		
		return files;
	}
}

class Dir_array_access extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		return Dir_glob.glob(receiver, arg);
	}
}

class Dir_glob extends RubyOneArgMethod {
	protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
		if (null == block) {
			return glob(receiver, arg);
		} else {
			return glob(receiver, arg, block);
		}
	}

	static RubyValue glob(RubyValue receiver, RubyValue arg) {
		String s = RubyTypesUtil.convertToString(arg).toString();
		String dir;
		String pattern;
		int index = s.lastIndexOf('/');
		if (index < 0) {
			dir = ".";
			pattern = s;
			
		} else {
			dir = s.substring(0, index);
			pattern = s.substring(index + 1);
		}
		
		RubyArray a = new RubyArray();
		File file = new File(dir);
		String[] files = file.list(new GlobFilenameFilter(pattern));
		if (null != files) {
			for (String f : files){
				a.add(ObjectFactory.createString(dir + "/" + f));
			}
		}
		return a;
	}

	static RubyValue glob(RubyValue receiver, RubyValue arg, RubyBlock block) {
		String s = RubyTypesUtil.convertToString(arg).toString();
		String dir;
		String pattern;
		int index = s.lastIndexOf('/');
		if (index < 0) {
			dir = ".";
			pattern = s;
			
		} else {
			dir = s.substring(0, index);
			pattern = s.substring(index + 1);
		}
		
		File file = new File(dir);
		String[] files = file.list(new GlobFilenameFilter(pattern));
		if (null != files) {
			for (String f : f){
				block.invoke(receiver, new RubyArray(ObjectFactory.createString(dir + "/" + f)));
			}
		}
		return ObjectFactory.nilValue;
	}
}

public class DirClassBuilder {
	public static void initialize() {
{'fraction_non_alphanumeric': 0.12269622374017271, 'fraction_numerical': 0.0048975383425699185, 'mean_word_length': 4.225589225589226, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '5520750', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2404, 'n_words': 640}