- Improvements to fetching file system information on Windows.

- Fixes for reset and clearing to end of line for Windows console.
This commit is contained in:
Adam Murdoch
2012-09-15 17:52:51 +10:00
parent 17ab1f04ef
commit 30cc864813
2 changed files with 122 additions and 99 deletions

View File

@@ -3,6 +3,7 @@
#include "native.h"
#include "generic.h"
#include <windows.h>
#include <wchar.h>
/*
* Marks the given result as failed, using the current value of GetLastError()
@@ -55,36 +56,53 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getPid(JNIEn
*/
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
// TODO - don't use the stack
wchar_t volumeName[MAX_PATH+1];
wchar_t deviceName[MAX_PATH+1];
wchar_t pathNames[MAX_PATH+1];
wchar_t fsName[MAX_PATH+1];
wchar_t* volumeName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
jclass info_class = env->GetObjectClass(info);
jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");
HANDLE handle = FindFirstVolumeW(volumeName, MAX_PATH+1);
if (handle == INVALID_HANDLE_VALUE) {
printf("no volumes\r\n");
free(volumeName);
mark_failed_with_errno(env, "could not find first volume", result);
return;
}
wchar_t* deviceName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
wchar_t* pathNames = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
wchar_t* fsName = (wchar_t*)malloc(sizeof(wchar_t) * (MAX_PATH+1));
while(true) {
// Chop off the trailing '\'
size_t len = wcslen(volumeName);
if (len < 5) {
mark_failed_with_message(env, "volume name is too short", result);
break;
}
volumeName[len-1] = L'\0';
QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1);
// TODO - error
if (QueryDosDeviceW(&volumeName[4], deviceName, MAX_PATH+1) == 0) {
mark_failed_with_errno(env, "could not query dos device", result);
break;
}
volumeName[len-1] = L'\\';
DWORD used;
GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used);
// TODO - error
if (GetVolumePathNamesForVolumeNameW(volumeName, pathNames, MAX_PATH+1, &used) == 0) {
// TODO - try again if the buffer is too small
mark_failed_with_errno(env, "could not query volume paths", result);
break;
}
wchar_t* cur = pathNames;
if (cur[0] != L'\0') {
GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1);
// TODO - error
if(GetVolumeInformationW(cur, NULL, 0, NULL, NULL, NULL, fsName, MAX_PATH+1) == 0) {
if (GetLastError() != ERROR_NOT_READY) {
mark_failed_with_errno(env, "could not query volume information", result);
break;
}
wcscpy(fsName, L"unknown");
}
for (;cur[0] != L'\0'; cur += wcslen(cur) + 1) {
env->CallVoidMethod(info, method, env->NewString((jchar*)deviceName, wcslen(deviceName)),
env->NewString((jchar*)fsName, wcslen(fsName)), env->NewString((jchar*)cur, wcslen(cur)), JNI_FALSE);
@@ -93,11 +111,15 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileS
if (FindNextVolumeW(handle, volumeName, MAX_PATH) == 0) {
if (GetLastError() != ERROR_NO_MORE_FILES) {
// TODO - fail
mark_failed_with_errno(env, "could find next volume", result);
}
break;
}
}
free(volumeName);
free(deviceName);
free(pathNames);
free(fsName);
FindVolumeClose(handle);
}
@@ -196,8 +218,7 @@ Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_normal(JNI
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_reset(JNIEnv *env, jclass target, jobject result) {
SetConsoleTextAttribute(current_console, original_attributes);
if (!SetConsoleTextAttribute(current_console, current_attributes)) {
if (!SetConsoleTextAttribute(current_console, original_attributes)) {
mark_failed_with_errno(env, "could not set text attributes", result);
}
}
@@ -309,8 +330,9 @@ Java_net_rubygrapefruit_platform_internal_jni_WindowsConsoleFunctions_clearToEnd
mark_failed_with_errno(env, "could not get console buffer", result);
return;
}
for (int i = console_info.dwCursorPosition.X; i < console_info.dwSize.X; i++) {
WriteConsole(current_console, " ", 1, NULL, NULL);
DWORD count;
if (!FillConsoleOutputCharacterW(current_console, L' ', console_info.dwSize.X - console_info.dwCursorPosition.X, console_info.dwCursorPosition, &count)) {
mark_failed_with_errno(env, "could not clear console", result);
}
}

View File

@@ -1,82 +1,83 @@
package net.rubygrapefruit.platform;
public class Main {
public static void main(String[] args) {
System.out.println();
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
SystemInfo systemInfo = Native.get(SystemInfo.class);
System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture());
Process process = Native.get(Process.class);
System.out.println("* PID: " + process.getProcessId());
FileSystems fileSystems = Native.get(FileSystems.class);
System.out.println("* File systems: ");
for (FileSystem fileSystem : fileSystems.getFileSystems()) {
System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local"));
}
Terminals terminals = Native.get(Terminals.class);
boolean stdoutIsTerminal = terminals.isTerminal(Terminals.Output.Stdout);
boolean stderrIsTerminal = terminals.isTerminal(Terminals.Output.Stderr);
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
if (stdoutIsTerminal) {
Terminal terminal = terminals.getTerminal(Terminals.Output.Stdout);
TerminalSize terminalSize = terminal.getTerminalSize();
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
System.out.println("* text attributes: " + (terminal.supportsTextAttributes() ? "yes" : "no"));
System.out.println("* color: " + (terminal.supportsColor() ? "yes" : "no"));
System.out.println("* cursor motion: " + (terminal.supportsCursorMotion() ? "yes" : "no"));
System.out.println();
System.out.println("TEXT ATTRIBUTES");
System.out.print("[normal] ");
terminal.bold();
System.out.print("[bold]");
terminal.normal();
System.out.println(" [normal]");
System.out.println();
System.out.println("COLORS");
for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
terminal.bold();
System.out.print(String.format("[%s]", color.toString().toLowerCase()));
terminal.normal();
System.out.println();
}
System.out.println();
terminal.reset();
if (terminal.supportsCursorMotion()) {
System.out.println("CURSOR MOVEMENT");
System.out.println(" ");
System.out.println(" ");
System.out.print("[delete me]");
terminal.cursorLeft(11);
terminal.cursorUp(1);
terminal.cursorRight(10);
System.out.print("[4]");
terminal.cursorUp(1);
terminal.cursorLeft(3);
System.out.print("[2]");
terminal.cursorLeft(13);
System.out.print("[1]");
terminal.cursorLeft(3);
terminal.cursorDown(1);
System.out.print("[3]");
terminal.cursorDown(1);
terminal.cursorStartOfLine();
terminal.clearToEndOfLine();
terminal.foreground(Terminal.Color.Blue).bold();
System.out.println("done!");
System.out.println();
}
}
}
}
package net.rubygrapefruit.platform;
public class Main {
public static void main(String[] args) {
System.out.println();
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
SystemInfo systemInfo = Native.get(SystemInfo.class);
System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture());
Process process = Native.get(Process.class);
System.out.println("* PID: " + process.getProcessId());
FileSystems fileSystems = Native.get(FileSystems.class);
System.out.println("* File systems: ");
for (FileSystem fileSystem : fileSystems.getFileSystems()) {
System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local"));
}
Terminals terminals = Native.get(Terminals.class);
boolean stdoutIsTerminal = terminals.isTerminal(Terminals.Output.Stdout);
boolean stderrIsTerminal = terminals.isTerminal(Terminals.Output.Stderr);
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
if (stdoutIsTerminal) {
Terminal terminal = terminals.getTerminal(Terminals.Output.Stdout);
TerminalSize terminalSize = terminal.getTerminalSize();
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
System.out.println("* text attributes: " + (terminal.supportsTextAttributes() ? "yes" : "no"));
System.out.println("* color: " + (terminal.supportsColor() ? "yes" : "no"));
System.out.println("* cursor motion: " + (terminal.supportsCursorMotion() ? "yes" : "no"));
System.out.println();
System.out.println("TEXT ATTRIBUTES");
System.out.print("[normal] ");
terminal.bold();
System.out.print("[bold]");
terminal.normal();
System.out.println(" [normal]");
System.out.println();
System.out.println("COLORS");
for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
terminal.bold();
System.out.print(String.format("[%s]", color.toString().toLowerCase()));
terminal.normal();
System.out.println();
}
System.out.println();
terminal.reset();
if (terminal.supportsCursorMotion()) {
System.out.println("CURSOR MOVEMENT");
System.out.println(" ");
System.out.println(" ");
System.out.print("[delete me]");
terminal.cursorLeft(11);
terminal.cursorUp(1);
terminal.cursorRight(10);
System.out.print("[4]");
terminal.cursorUp(1);
terminal.cursorLeft(3);
System.out.print("[2]");
terminal.cursorLeft(13);
System.out.print("[1]");
terminal.cursorLeft(3);
terminal.cursorDown(1);
System.out.print("[3]");
terminal.cursorDown(1);
terminal.cursorStartOfLine();
terminal.foreground(Terminal.Color.Blue).bold();
System.out.print("done");
terminal.clearToEndOfLine();
System.out.println("!");
System.out.println();
}
}
}
}