Lely core libraries 2.3.4
io.c File Reference

This file is part of the I/O library; it contains the implementation of the the initialization and finalization functions. More...

#include "io.h"
#include <lely/util/errnum.h>
#include <assert.h>
#include "handle.h"
Include dependency graph for io.c:

Go to the source code of this file.

Functions

int lely_io_init (void)
 Initializes the I/O library and makes the I/O functions available for use. More...
 
void lely_io_fini (void)
 Finalizes the I/O library and terminates the availability of the I/O functions. More...
 
int io_close (io_handle_t handle)
 Closes an I/O device. More...
 
int io_get_type (io_handle_t handle)
 Returns the type of an I/O device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE, IO_TYPE_SERIAL or IO_TYPE_SOCK), or -1 on error. More...
 
HANDLE io_get_fd (io_handle_t handle)
 Returns the native file descriptor of an I/O device.
 
int io_get_flags (io_handle_t handle)
 Obtains the flags of an I/O device. More...
 
int io_set_flags (io_handle_t handle, int flags)
 Sets the flags of an I/O device. More...
 
ssize_t io_read (io_handle_t handle, void *buf, size_t nbytes)
 Performs a read operation. More...
 
ssize_t io_write (io_handle_t handle, const void *buf, size_t nbytes)
 Performs a write operation. More...
 
int io_flush (io_handle_t handle)
 Flushes the write buffer of a an I/O device. More...
 

Detailed Description

This file is part of the I/O library; it contains the implementation of the the initialization and finalization functions.

See also
lely/io/io.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file io.c.

Function Documentation

◆ lely_io_init()

int lely_io_init ( void  )

Initializes the I/O library and makes the I/O functions available for use.

On Windows this function invokes WSAStartup(). This function is not thread safe, but can be invoked multiple times, as long as it is matched by an equal number of calls to lely_io_fini().

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 37 of file io.c.

◆ lely_io_fini()

void lely_io_fini ( void  )

Finalizes the I/O library and terminates the availability of the I/O functions.

On Windows this function invokes WSACleanup(). Note that this function MUST be invoked once for each corresponding call to lely_io_init(). Only the last invocation will finalize the library.

Definition at line 63 of file io.c.

◆ io_close()

int io_close ( io_handle_t  handle)

Closes an I/O device.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 78 of file io.c.

◆ io_get_type()

int io_get_type ( io_handle_t  handle)

Returns the type of an I/O device (one of IO_TYPE_CAN, IO_TYPE_FILE, IO_TYPE_PIPE, IO_TYPE_SERIAL or IO_TYPE_SOCK), or -1 on error.

In the latter case, the error number can be obtained with get_errc().

Definition at line 91 of file io.c.

◆ io_get_flags()

int io_get_flags ( io_handle_t  handle)

Obtains the flags of an I/O device.

Returns
the active flags (any combination of IO_FLAG_NO_CLOSE, IO_FLAG_NONBLOCK and IO_FLAG_LOOPBACK), or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_set_flags()

Definition at line 114 of file io.c.

◆ io_set_flags()

int io_set_flags ( io_handle_t  handle,
int  flags 
)

Sets the flags of an I/O device.

Parameters
handlea valid I/O device handle.
flagsthe I/O device flags (any combination of IO_FLAG_NO_CLOSE, IO_FLAG_NONBLOCK and IO_FLAG_LOOPBACK).
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
io_get_flags()

Definition at line 128 of file io.c.

◆ io_read()

ssize_t io_read ( io_handle_t  handle,
void *  buf,
size_t  nbytes 
)

Performs a read operation.

For regular files, this function updates the file pointer on success.

Parameters
handlea valid I/O device handle.
bufa pointer to the destination buffer.
nbytesthe number of bytes to read.
Returns
the number of bytes read on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 154 of file io.c.

◆ io_write()

ssize_t io_write ( io_handle_t  handle,
const void *  buf,
size_t  nbytes 
)

Performs a write operation.

For regular files, this function updates the file pointer on success.

Parameters
handlea valid I/O device handle.
bufa pointer to the source buffer.
nbytesthe number of bytes to write.
Returns
the number of bytes written on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 171 of file io.c.

◆ io_flush()

int io_flush ( io_handle_t  handle)

Flushes the write buffer of a an I/O device.

This function waits until all buffered data has been written.

Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().

Definition at line 188 of file io.c.