<commit_before>/**
 * Copyright (C) 2011-2012 MK124
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if defined(WIN32)

#pragma comment (lib, "jvm.lib")
#pragma comment (lib, "jawt.lib")

#define _CRT_SECURE_NO_WARNINGS
#include <io.h>

#endif

#if defined(LINUX)
#include <dirent.h>
#include "linux.h"
#endif


#include <jni.h>
#include <stdio.h>
#include <string.h>

#include "jni_core.h"


JavaVM *jvm = NULL;


int jni_jvm_create( JNIEnv** env, const char* clspath )
{
	if( jvm != NULL ) return -1;

	char clspathOpt[2048] = "-Djava.class.path=";
	strcat( clspathOpt, clspath );
	
	JavaVMOption options[3];
	options[0].optionString = clspathOpt;
	options[1].optionString = "-Djava.library.path=./plugins";
	options[2].optionString = "-verbose:gc";
	//options[3].optionString = "-Djava.compiler=NONE";

	JavaVMInitArgs vm_args;
	vm_args.version = JNI_VERSION_1_6;
	vm_args.options = options;
	vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
	vm_args.ignoreUnrecognized = JNI_FALSE;

	jint res = JNI_CreateJavaVM(&jvm, (void**)env, &vm_args);
	if (res < 0) return -3;

	return 0;
}

int jni_jvm_printExceptionStack( JNIEnv *env )
{
	if( !env->ExceptionCheck() ) return 1;

	jthrowable throwable = env->ExceptionOccurred();
	jclass throwableCls = env->GetObjectClass(throwable);
	jmethodID printStackTraceId = env->GetMethodID(throwableCls, "printStackTrace", "()V");
	if( !printStackTraceId ) return -4;

	env->CallVoidMethod( throwable, printStackTraceId );
	env->ExceptionClear();

	return 0;
}

int jni_jvm_constructObject( JNIEnv *env, jclass jcls, jobject *pjobj )
{
	if( !jvm ) return -1;

	jmethodID jmid = env->GetMethodID(jcls, "<init>", "()V");
	if( !jmid ) return -2;

	*pjobj = env->NewObject( jcls, jmid );
	if( !*pjobj )
	{
		jni_jvm_printExceptionStack( env );
		return -3;
	}

	return 0;
}

int jni_jvm_destroy( JNIEnv *env )
{
	if( !jvm ) return -1;

	if ( env->ExceptionOccurred() ) env->ExceptionDescribe();
	jvm->DestroyJavaVM();

	env = NULL;
	jvm = NULL;
	return 0;
}

int findAndGenerateClassPath(const char* searchPath, char* classPath)
{
	char basepath[512];

	strcpy( basepath, searchPath );
	for( int i=strlen(basepath)-1; i>=0; i-- )
		if( basepath[i] == '/' ) { basepath[i+1] = 0; break; }

#if defined(WIN32)
		_finddata_t finddata;
		int hfind = _findfirst(searchPath, &finddata);
		if( hfind < 0 ) return -2;

		do 
		{
			strcat( classPath, basepath );
			strcat( classPath, finddata.name );
			strcat( classPath, ";" );
		} while ( !_findnext(hfind, &finddata) );
		_findclose( hfind );
#else
		DIR *dir = opendir(basepath);
		struct dirent entry;
		struct dirent* entryPtr = NULL;

		readdir_r(dir, &entry, &entryPtr);
		if(entryPtr == NULL) return -2;

		while(entryPtr != NULL)
		{
			if(strncmp(entry.d_name, ".", PATH_MAX) ==0 ||
				strncmp(entry.d_name, "..", PATH_MAX) ==0)
			{
				readdir_r(dir, &entry, &entryPtr);
				continue;
			}

			int len = strlen(entry.d_name);

			if(strcmp(strlwr(&(entry.d_name[len-4])), ".jar") != 0)
			{
				readdir_r(dir, &entry, &entryPtr);
				continue;
			}

			strcat( classPath, basepath );
			strcat( classPath, entry.d_name );
			strcat( classPath, ";" );

			readdir_r(dir, &entry, &entryPtr);
		}

		closedir(dir);
#endif

		classPath[ strlen(classPath)-1 ] = 0;
		return 0;
}
<commit_msg>Linux path separator is ':'.<commit_after>/**
 * Copyright (C) 2011-2012 MK124
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if defined(WIN32)

#pragma comment (lib, "jvm.lib")
#pragma comment (lib, "jawt.lib")

#define _CRT_SECURE_NO_WARNINGS
#include <io.h>

#endif

#if defined(LINUX)
#include <dirent.h>
#include "linux.h"
#endif


#include <jni.h>
#include <stdio.h>
#include <string.h>

#include "jni_core.h"


JavaVM *jvm = NULL;


int jni_jvm_create( JNIEnv** env, const char* clspath )
{
	if( jvm != NULL ) return -1;

	char clspathOpt[2048] = "-Djava.class.path=";
	strcat( clspathOpt, clspath );
	
	JavaVMOption options[3];
	options[0].optionString = clspathOpt;
	options[1].optionString = "-Djava.library.path=./plugins";
	options[2].optionString = "-verbose:gc";
	//options[3].optionString = "-Djava.compiler=NONE";

	JavaVMInitArgs vm_args;
	vm_args.version = JNI_VERSION_1_6;
	vm_args.options = options;
	vm_args.nOptions = sizeof(options) / sizeof(JavaVMOption);
	vm_args.ignoreUnrecognized = JNI_FALSE;

	jint res = JNI_CreateJavaVM(&jvm, (void**)env, &vm_args);
	if (res < 0) return -3;

	return 0;
}

int jni_jvm_printExceptionStack( JNIEnv *env )
{
	if( !env->ExceptionCheck() ) return 1;

	jthrowable throwable = env->ExceptionOccurred();
	jclass throwableCls = env->GetObjectClass(throwable);
	jmethodID printStackTraceId = env->GetMethodID(throwableCls, "printStackTrace", "()V");
	if( !printStackTraceId ) return -4;

	env->CallVoidMethod( throwable, printStackTraceId );
	env->ExceptionClear();

	return 0;
}

int jni_jvm_constructObject( JNIEnv *env, jclass jcls, jobject *pjobj )
{
	if( !jvm ) return -1;

	jmethodID jmid = env->GetMethodID(jcls, "<init>", "()V");
	if( !jmid ) return -2;

	*pjobj = env->NewObject( jcls, jmid );
	if( !*pjobj )
	{
		jni_jvm_printExceptionStack( env );
		return -3;
	}

	return 0;
}

int jni_jvm_destroy( JNIEnv *env )
{
	if( !jvm ) return -1;

	if ( env->ExceptionOccurred() ) env->ExceptionDescribe();
	jvm->DestroyJavaVM();

	env = NULL;
	jvm = NULL;
	return 0;
}

int findAndGenerateClassPath(const char* searchPath, char* classPath)
{
	char basepath[512];

	strcpy( basepath, searchPath );
	for( int i=strlen(basepath)-1; i>=0; i-- )
		if( basepath[i] == '/' ) { basepath[i+1] = 0; break; }

#if defined(WIN32)
		_finddata_t finddata;
		int hfind = _findfirst(searchPath, &finddata);
		if( hfind < 0 ) return -2;

		do 
		{
			strcat( classPath, basepath );
			strcat( classPath, finddata.name );
			strcat( classPath, ";" );
		} while ( !_findnext(hfind, &finddata) );
		_findclose( hfind );
#else
		DIR *dir = opendir(basepath);
		struct dirent entry;
		struct dirent* entryPtr = NULL;

		readdir_r(dir, &entry, &entryPtr);
		if(entryPtr == NULL) return -2;

		while(entryPtr != NULL)
		{
			if(strncmp(entry.d_name, ".", PATH_MAX) ==0 ||
				strncmp(entry.d_name, "..", PATH_MAX) ==0)
			{
				readdir_r(dir, &entry, &entryPtr);
				continue;
			}

			int len = strlen(entry.d_name);

			if(strcmp(strlwr(&(entry.d_name[len-4])), ".jar") != 0)
			{
				readdir_r(dir, &entry, &entryPtr);
				continue;
			}

			strcat( classPath, basepath );
			strcat( classPath, entry.d_name );
			strcat( classPath, ":" );

			readdir_r(dir, &entry, &entryPtr);
		}

		closedir(dir);
#endif

		classPath[ strlen(classPath)-1 ] = 0;
		return 0;
}

{'fraction_non_alphanumeric': 0.14984910116782574, 'fraction_numerical': 0.015745965096444037, 'mean_word_length': 4.0949197860962565, 'pattern_counts': {'":': 1, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5735001', 'n_tokens_mistral': 3101, 'n_tokens_neox': 2849, 'n_words': 710}