VALUE turtle_parser_parse_file( int argc, VALUE * argv, VALUE self ) { VALUE source_file; VALUE working_document = Qnil; pParserComplect complect; pANTLR3_UINT8 file; int alloc; if ( argc == 0 || ( argc > 0 && NIL_P(argv[0]) ) ) { rb_raise( rb_eArgError, "file name required" ); } source_file = argv[ 0 ]; if( argc > 1 ) working_document = argv[ 1 ]; if ( NIL_P(working_document) ) { working_document = rb_class_new_instance( 0, NULL, rb_path2class( "Wongi::RDF::Document" ) ); } Data_Get_Struct( self, ParserComplect, complect ); file = (pANTLR3_UINT8) StringValueCStr( source_file ); if( !open_file_stream(complect, file) ) { return working_document; } alloc = fill_parser_complect( complect ); if ( alloc ) { VALUE argv[ ] = { working_document }; VALUE collector = rb_class_new_instance( 1, argv, cCollector ); complect->parser->document( complect->parser, collector ); } release_complect( complect ); return working_document; }
/* ** Transfer a local file to a remote machine. */ int send_a_file( const char *local, const char *remote, int perm ) { int remote_fd, local_fd; char buf[ CHUNK_SIZE + 50 ]; int nbytes, bytes_to_go; size_t len; double start, finish; dprintf( D_ALWAYS, "Entering send_a_file( %s, %s, 0%o )\n", local,remote,perm); /* Start timing the transfer */ start = get_time(); /* Open the local file */ if( (local_fd=safe_open_wrapper(local,O_RDONLY,0)) < 0 ) { dprintf( D_FULLDEBUG, "Failed to open \"%s\" locally, errno = %d\n", local, errno); return -1; } /* Find length of the file */ len = lseek( local_fd, 0, 2 ); lseek( local_fd, 0, 0 ); /* Open the remote file */ remote_fd = open_file_stream( remote, O_WRONLY | O_CREAT | O_TRUNC, &len ); if( remote_fd < 0 ) { dprintf( D_ALWAYS, "Failed to open \"%s\" remotely, errno = %d\n", remote, errno ); (void)close(local_fd); return -1; } /* transfer the data */ dprintf( D_ALWAYS, "File length is %lu\n", (unsigned long)len ); for(bytes_to_go = len; bytes_to_go; bytes_to_go -= nbytes ) { nbytes = MIN( CHUNK_SIZE, bytes_to_go ); nbytes = read( local_fd, buf, nbytes ); if( nbytes < 0 ) { dprintf( D_ALWAYS, "Can't read fd %d, errno = %d\n", local_fd, errno ); (void)close( local_fd ); (void)close( remote_fd ); return -1; } if( write(remote_fd,buf,nbytes) != nbytes ) { dprintf( D_ALWAYS, "Can't write fd %d, errno = %d\n", remote_fd, errno ); (void)close( local_fd ); (void)close( remote_fd ); return -1; } } /* Clean up */ (void)close( local_fd ); (void)close( remote_fd ); /* report */ finish = get_time(); dprintf( D_ALWAYS,"Send_file() transferred %lu bytes, %d bytes/second\n", (unsigned long)len, (int)(len/(finish-start)) ); return len; }
/* ** Transfer a remote file to the local machine. */ int get_file( const char *remote, const char *local, int mode ) { int remote_fd, local_fd; char buf[ CHUNK_SIZE + 50]; int nbytes, bytes_to_go; size_t len; double start, finish; dprintf( D_ALWAYS, "Entering get_file( %s, %s, 0%o )\n", remote,local,mode); start = get_time(); /* open the remote file */ remote_fd = open_file_stream( remote, O_RDONLY, &len ); if( remote_fd < 0 ) { dprintf( D_ALWAYS, "Failed to open \"%s\" remotely, errno = %d\n", remote, errno ); return -1; } /* open the local file */ if( (local_fd=safe_open_wrapper(local,O_WRONLY|O_CREAT|O_TRUNC,mode)) < 0 ) { dprintf( D_FULLDEBUG, "Failed to open \"%s\" locally, errno = %d\n", local, errno); (void)close(remote_fd); return -1; } /* transfer the data */ for(bytes_to_go = len; bytes_to_go; bytes_to_go -= nbytes ) { nbytes = MIN( CHUNK_SIZE, bytes_to_go ); nbytes = read( remote_fd, buf, nbytes ); if( nbytes <= 0 ) { dprintf( D_ALWAYS, "Can't read fd %d, errno = %d\n", remote_fd, errno ); (void)close( local_fd ); (void)close( remote_fd ); return -1; } if( write(local_fd,buf,nbytes) != nbytes ) { dprintf( D_ALWAYS, "Can't write fd %d, errno = %d\n", local_fd, errno ); (void)close( local_fd ); (void)close( remote_fd ); return -1; } } /* clean up */ (void)close( local_fd ); (void)close( remote_fd ); finish = get_time(); dprintf( D_ALWAYS,"Get_file() transferred %lu bytes, %d bytes/second\n", (unsigned long) len, (int)(len/(finish-start)) ); return len; }