summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/adrians-java/Old_robot/c_file.java
blob: 5882adbe202c72aed78391b2aeb19738ab440dcb (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
/*
 *  Low-level C file interface via JNI
 *
 *  Adrian Bowyer
 *  2 October 2005
 *
 */

// Teeny class to hold the JNI calls to some C functions that access
// low-level file setup, reading and writing

class c_c_file
{
    public native int c_open(String file_name);              // Open file, returning a descriptor
    public native void c_fcntl(int fd, int wait);            // Wait (or not) for input
    public native int c_write(int fd, String str, int len);  // Write len bytes of str (number actually sent is returned)
    public native String c_read(int fd);                     // Read into string
    public native int c_n_read();                            // Number of bytes read by c_read()

    static 
    {
        System.loadLibrary("c_c_file");	// load the .so file with the native fns.
    }
}

// Wrapper class for c_c_file; this retains the static instance 
// of it and the file descriptor, so the rest of the software doesn't
// need to care about them.

class c_file
{
    private static int fd;                    // C file descriptor of the file
    private static c_c_file ccf;              // Instance of the C JNI calls.

    // The constructor opens the file

    public c_file(String file_name)
    {
	ccf = new c_c_file();
	fd = ccf.c_open(file_name);
	if(fd == -1)
	    System.err.println("Unable to open file:" + file_name);	    
    }

    // Wait 1 (or not 0) for input

    public void fcntl(int wait)
    {
	ccf.c_fcntl(fd, wait);
    }

    // Write len bytes of str (number actually sent is returned)

    public int write(String str, int len)
    {
	return ccf.c_write(fd, str, len);
    }

    // Read into string

    public String read()
    {
	return ccf.c_read(fd);
    }

    // Number of bytes read by last call to c_read()
    // (Nasty.  But simple...)

    public int n_read()
    {
	return ccf.c_n_read();
    }
}