ni_nitdoc: quicksearch is now case sensitive
[nit.git] / c_src / stream_nit.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 *
5 * This file is free software, which comes along with NIT. This software is
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 * is kept unaltered, and a notification of the changes is added.
10 * You are allowed to redistribute it and sell it, alone or is a part of
11 * another product.
12 */
13
14 #include "stream_nit.h"
15
16 #include <poll.h>
17 #include <errno.h>
18
19 int stream_FDStream_FDStream_native_read_char_1(void *s, int fd) {
20 int result;
21 char buf;
22 ssize_t r = read(fd, &buf, 1);
23 if (r == 0)
24 result = -1;
25 else
26 result = buf;
27 return result;
28 }
29 #ifndef NONITCNI
30
31 /*
32 C implementation of stream::Object::intern_poll
33
34 Imported methods signatures:
35 bigint Array_length( Array recv ) for array::AbstractArrayRead::(abstract_collection::Collection::length)
36 nullable_Object Array__index( Array recv, bigint index ) for array::Array::(abstract_collection::SequenceRead::[])
37 int nullable_Object_is_a_bigint( nullable_Object value ) to check if a nullable Object is a Int
38 bigint nullable_Object_as_bigint( nullable_Object value ) to cast from nullable Object to Int
39 int Int_is_null( nullable_Int value ) to check if a nullable Int is a Int
40 bigint bigint_as_not_null( nullable_Int value ) to cast from nullable Int to Int
41 */
42 nullable_Int Object_intern_poll___impl( Object recv, Array in_fds, Array out_fds ) {
43 int in_len, out_len, total_len;
44 struct pollfd *c_fds;
45 sigset_t sigmask;
46 int i;
47 nullable_Object tmp_nit_obj;
48 int first_polled_fd = -1;
49 int result;
50
51 in_len = Array_length( in_fds );
52 out_len = Array_length( out_fds );
53 total_len = in_len + out_len;
54 c_fds = malloc( sizeof(struct pollfd) * total_len );
55
56 /* input streams */
57 for ( i=0; i<in_len; i ++ ) {
58 int fd;
59 tmp_nit_obj = Array__index( in_fds, i );
60 fd = nullable_Object_as_Int( tmp_nit_obj );
61
62 c_fds[i].fd = fd;
63 c_fds[i].events = POLLIN;
64 }
65
66 /* output streams */
67 for ( i=0; i<out_len; i ++ ) {
68 int fd;
69 tmp_nit_obj = Array__index( out_fds, i );
70 fd = nullable_Object_as_Int( tmp_nit_obj );
71
72 c_fds[i].fd = fd;
73 c_fds[i].events = POLLOUT;
74 }
75
76 /* poll all fds, unlimited timeout */
77 result = poll( c_fds, total_len, -1 );
78
79 if ( result > 0 ) {
80 /* analyse results */
81 for ( i=0; i<total_len; i++ )
82 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
83 c_fds[i].revents & POLLHUP ) /* closed */
84 {
85 first_polled_fd = c_fds[i].fd;
86 break;
87 }
88
89 return Int_as_nullable( first_polled_fd );
90 }
91 else if ( result < 0 )
92 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
93
94 return null_Int();
95 }
96 #endif