Robust I/O function code of the RIO package

Source: Internet
Author: User
Tags types of functions

[Php]
The following is about

The following describes [php]
# Include <stdio. h>
# Include <string. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <fcntl. h>
# Include <sys/stat. h>
 
# Define MAXLINE 1024
 
/* Unbuffer input/output function */
 
Ssize_t rio_readn (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
 
While (nleft> 0 ){
If (nread = read (fd, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {
Return-1;/* error */
}
} Else if (nread = 0 ){
Break;/* EOF */
} Else {/* read content */
Nleft-= nread;
Bufp + = nread;
}
}
Return (n-nleft );
}
 
Ssize_t rio_writen (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nwritten;
Char * bufp = usrbuf;
 
While (nwritten = write (fd, bufp, nleft) <= 0 ){
If (errno = EINTR ){
Nwritten = 0;
} Else {
Return-1;
}
Nleft-= nwritten;
Bufp + = nwritten;
}
Return n;
}
/*************************************** ***************************************/
# Define RIO_BUFSIZE 8192
Typedef struct {
Int rio_fd;/* To operate the file descriptor */
Int rio_cnt;/* unread bytes in internal buf */
Char * rio_bufptr;/* next unread byte int internal buf */
Char rio_buf [RIO_BUFSIZE];/* internal buf */
} Rio_t;
Void rio_readinitb (rio_t * rp, int fd)
{
Rp-> rio_fd = fd;
Rp-> rio_cnt = 0;
Rp-> rio_bufptr = rp-> rio_buf;
}
Static ssize_t rio_read (rio_t * rp, char * usrbuf, size_t n)
{
Int cnt;
While (rp-> rio_cnt <= 0) {/* Read the file content if buf is empty */
Rp-> rio_cnt = read (rp-> rio_fd, rp-> rio_buf, sizeof (rp-> rio_buf ));
If (rp-> rio_cnt <0 ){
If (errno! = EINTR ){
Return-1;
}
} Else if (rp-> rio_cnt = 0) {/* EOF */
Return 0;
} Else {/* reset buf ptr */
Rp-> rio_bufptr = rp-> rio_buf;
}
}
/* When n <rp-> rio_cnt, need copy some times */
Cnt = n;
If (rp-> rio_cnt <n) {/* one time copy end */
Cnt = rp-> rio_cnt;
}
Memcpy (usrbuf, rp-> rio_bufptr, cnt );
Rp-> rio_bufptr + = cnt;
Rp-> rio_cnt-= cnt;
Return cnt;
}
Ssize_t rio_readlineb (rio_t * rp, void * usrbuf, size_t maxlen)
{
Int n, rc;
Char c, * bufp = usrbuf;
For (n = 1; n <maxlen; n ++ ){
If (rc = rio_read (rp, & c, 1) = 1 ){
* Bufp ++ = c;
If (c = '\ n '){
Break;
}
} Else if (rc = 0 ){
If (n = 1) {/* EOF no data read */
Return 0;
} Else {/* EOF some data read */
Break;
}
} Else {/* ERROR */
Return-1;
}
}
* Bufp = 0;/* string end sign: '\ 0 '*/
Return n;
}
 
Ssize_t rio_readnb (rio_t * rp, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;
 
While (nleft> 0 ){
If (nread = rio_read (rp, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {/* errno set by read ()*/
Return-1;
}
} Else if (nread = 0) {/* EOF */
Break;
}
Nleft-= nread;
Bufp + = nread;
}
Return (n-nleft);/* return> = 0 */
}
 
Int main ()
{
Int n;
Rio_t rio;
Char buf [MAXLINE];
 
Int fd = open ("1.txt", O_RDONLY, 755 );
If (fd <= 0 ){
Printf ("error \ n ");
}
Rio_readinitb (& rio, fd );
While (n = rio_readlineb (& rio, buf, MAXLINE ))! = 0 ){
Rio_writen (1, buf, n );
}
Close (fd );
Return 0;
}

# Include <stdio. h>
# Include <string. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <fcntl. h>
# Include <sys/stat. h>

# Define MAXLINE 1024

/* Unbuffer input/output function */

Ssize_t rio_readn (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;

While (nleft> 0 ){
If (nread = read (fd, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {
Return-1;/* error */
}
} Else if (nread = 0 ){
Break;/* EOF */
} Else {/* read content */
Nleft-= nread;
Bufp + = nread;
}
}
Return (n-nleft );
}

Ssize_t rio_writen (int fd, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nwritten;
Char * bufp = usrbuf;

While (nwritten = write (fd, bufp, nleft) <= 0 ){
If (errno = EINTR ){
Nwritten = 0;
} Else {
Return-1;
}
Nleft-= nwritten;
Bufp + = nwritten;
}
Return n;
}
/*************************************** ***************************************/
# Define RIO_BUFSIZE 8192
Typedef struct {
Int rio_fd;/* To operate the file descriptor */
Int rio_cnt;/* unread bytes in internal buf */
Char * rio_bufptr;/* next unread byte int internal buf */
Char rio_buf [RIO_BUFSIZE];/* internal buf */
} Rio_t;
Void rio_readinitb (rio_t * rp, int fd)
{
Rp-> rio_fd = fd;
Rp-> rio_cnt = 0;
Rp-> rio_bufptr = rp-> rio_buf;
}
Static ssize_t rio_read (rio_t * rp, char * usrbuf, size_t n)
{
Int cnt;
While (rp-> rio_cnt <= 0) {/* Read the file content if buf is empty */
Rp-> rio_cnt = read (rp-> rio_fd, rp-> rio_buf, sizeof (rp-> rio_buf ));
If (rp-> rio_cnt <0 ){
If (errno! = EINTR ){
Return-1;
}
} Else if (rp-> rio_cnt = 0) {/* EOF */
Return 0;
} Else {/* reset buf ptr */
Rp-> rio_bufptr = rp-> rio_buf;
}
}
/* When n <rp-> rio_cnt, need copy some times */
Cnt = n;
If (rp-> rio_cnt <n) {/* one time copy end */
Cnt = rp-> rio_cnt;
}
Memcpy (usrbuf, rp-> rio_bufptr, cnt );
Rp-> rio_bufptr + = cnt;
Rp-> rio_cnt-= cnt;
Return cnt;
}
Ssize_t rio_readlineb (rio_t * rp, void * usrbuf, size_t maxlen)
{
Int n, rc;
Char c, * bufp = usrbuf;
For (n = 1; n <maxlen; n ++ ){
If (rc = rio_read (rp, & c, 1) = 1 ){
* Bufp ++ = c;
If (c = '\ n '){
Break;
}
} Else if (rc = 0 ){
If (n = 1) {/* EOF no data read */
Return 0;
} Else {/* EOF some data read */
Break;
}
} Else {/* ERROR */
Return-1;
}
}
* Bufp = 0;/* string end sign: '\ 0 '*/
Return n;
}

Ssize_t rio_readnb (rio_t * rp, void * usrbuf, size_t n)
{
Size_t nleft = n;
Ssize_t nread;
Char * bufp = usrbuf;

While (nleft> 0 ){
If (nread = rio_read (rp, bufp, nleft) <0 ){
If (errno = EINTR) {/* interrupted by sig handler return */
Nread = 0;
} Else {/* errno set by read ()*/
Return-1;
}
} Else if (nread = 0) {/* EOF */
Break;
}
Nleft-= nread;
Bufp + = nread;
}
Return (n-nleft);/* return> = 0 */
}

Int main ()
{
Int n;
Rio_t rio;
Char buf [MAXLINE];

Int fd = open ("1.txt", O_RDONLY, 755 );
If (fd <= 0 ){
Printf ("error \ n ");
}
Rio_readinitb (& rio, fd );
While (n = rio_readlineb (& rio, buf, MAXLINE ))! = 0 ){
Rio_writen (1, buf, n );
}
Close (fd );
Return 0;
}


The above functional RIO package provides two different types of functions:

1. Input and Output functions without buffering

2. buffered Input Functions

This is mainly for file I/O, because the standard I/O itself is buffered. When we perform read/write operations on files, standard I/O functions are generally used preferentially, but in network programming, only file Io functions can be used. The above functions may be used here. If you are interested, you can learn more about the above mechanism. In network communication, this mechanism is still widely used!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.