aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/mac_console.c
blob: 669bba307a68a25988b0a520e48053c2f5fab367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// File "mac_console.c"

#include <stdio.h>
#include "mac_console.h"


static	JNIEnv	*gJNIenv			= nil;

static	char	kMessage[]			= "";
static	char	gMessage[BUFSIZ];


//
// InstallJavaConsole
//
void InstallJavaConsole(JNIEnv *env)
{
	gJNIenv = env;
}

//
// UninstallJavaConsole
//
void UninstallJavaConsole(void)
{
	gJNIenv = nil;
}


//
// InstallConsole
//
int InstallConsole(void)
{
}

//
// RemoveConsole
//
int RemoveConsole(void)
{
}

//
// WriteCharsToConsole
//
int WriteCharsToConsole(void)
{
	jclass		class_System;
	jfieldID	id_err;
	jobject		object_err;
	jclass		class_PrintStream;
	jmethodID	id_println;
	jmethodID	id_flush;
	jstring		string			= nil;
	int			messageLength	= 0;
	char		*message		= nil;
	
	
	// _FILE
	//debugstr((char*)stderr->buffer);
	
	if (gJNIenv != nil)
	{
		if (stderr->buffer_len > 0)
		{
			messageLength = stderr->buffer_len;
			message = (char*)stderr->buffer;
		}
		else if (stdout->buffer_len > 0)
		{
			messageLength = stdout->buffer_len;
			message = (char*)stdout->buffer;
		}
		
		if (messageLength < BUFSIZ)
		{
			strncpy(gMessage, message, messageLength);
			gMessage[messageLength] = '\0';
		}
		else
		{
			strncpy(gMessage, message, BUFSIZ);
			gMessage[BUFSIZ-1] = '\0';
		}
		
		string = (*gJNIenv)->NewStringUTF(gJNIenv, gMessage);
		if (string != nil)
		{
			class_System		= (*gJNIenv)->FindClass(gJNIenv, "java/lang/System");
			id_err				= (*gJNIenv)->GetStaticFieldID(gJNIenv, class_System, "err", "Ljava/io/PrintStream;");
			object_err			= (*gJNIenv)->GetStaticObjectField(gJNIenv, class_System, id_err);
			class_PrintStream	= (*gJNIenv)->GetObjectClass(gJNIenv, object_err);
			id_println			= (*gJNIenv)->GetMethodID(gJNIenv, class_PrintStream, "print", "(Ljava/lang/String;)V");
			
			(*gJNIenv)->CallVoidMethod(gJNIenv, object_err, id_println, string);
			
	    	(*gJNIenv)->DeleteLocalRef(gJNIenv, string);
	    	(*gJNIenv)->DeleteLocalRef(gJNIenv, class_PrintStream);
	    	(*gJNIenv)->DeleteLocalRef(gJNIenv, object_err);
	    	(*gJNIenv)->DeleteLocalRef(gJNIenv, class_System);
	    }
	    else
	    {
			//debugstr("string is null");
	    }
	}
	else
	{
		//debugstr("gJNIenv is null");
	}
}

//
// ReadCharsFromConsole
//
int ReadCharsFromConsole(void)
{
}