File: plstdio.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (132 lines) | stat: -rw-r--r-- 3,419 bytes parent folder | download | duplicates (7)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* $Id: plstdio.c,v 1.3 2010/10/06 16:02:49 rice Exp $

   Standardized I/O handler for PLplot.

   Copyright (C) 2006  Jim Dishaw
   Copyright (C) 2006  Hazen Babcock

   This file is part of PLplot.

   PLplot is free software; you can redistribute it and/or modify
   it under the terms of the GNU Library General Public License as published
   by the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   PLplot is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with PLplot; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

*/

/* #define NEED_PLDEBUG */
#include "plplotP.h"

/* 
 * plio_write()
 *
 * Writes the contents of buf to stream.  Handles any I/O error conditions
 * so that the caller can "fire and forget."
 */
 
void 
plio_fwrite(void *buf, size_t size, size_t nmemb, FILE *stream)
{
/*  size_t bytes; */
  int iw;
  dbug_enter("plio_fwrite");

  /* Exit if there is nothing to write */
  if(size == 0 || nmemb == 0) return;

  /* Clear the error flag for this steam */
  clearerr(stream);

  /* bytes = */ iw = fwrite(buf, size, nmemb, stream);
  
  if(ferror(stream)) {
    /* Perhaps we can add a flag (global or per output stream)
       in order to decide if we should abort or warn.  I think
       I/O errors should generate an abort  */
    plabort("Error writing to file");
  }
}

/*
 * plio_read()
 *
 * Read from stream into buf.  Like plio_write(), this function will 
 * handle any I/O error conditions.
 */

void
plio_fread(void *buf, size_t size, size_t nmemb, FILE *stream)
{
/*  size_t bytes; */
  int ir;

  dbug_enter("plio_fread");

  /* If the buffer has a size of zero, we should complain */
  if(size == 0 || nmemb == 0) {
    plwarn("Zero length buffer size in plio_read, returning");    
    return;
  }

  /* Clear the error flag for this steam */
  clearerr(stream);

  /* bytes = */ ir = fread(buf, size, nmemb, stream);

  if(ferror(stream)) {
    /* The read resulted in an error */
    plabort("Error reading from file");
  }
}

/*
 * plio_fgets()
 *
 * Read from stream into buf.  This version of fgets is designed for the occasions
 * where the caller wants to ignore the return value.  
 *
 * NOTE: If one is reading from a file until an EOF condition, fgets() is better suited 
 * than this function, i.e.
 *
 *     while(fgets(buf, size, fp) != NULL) { ... do some stuff ... }
 *
 * rather than
 *
 *     while(!feof(fp)) { plio_fgets(buf, size, fp);  ... do some stuff ... }
 *
 * which would require checking for an empty buffer.
 */

void
plio_fgets(char *buf, int size, FILE *stream)
{
  /* size_t bytes; */                  /* pmr: unused */
  char *s;

  dbug_enter("plio_fgets");

  /* If the buffer has a size of zero, we should complain */
  if(size == 0) {
    plwarn("Zero length buffer size in plio_fgets, returning");    
    return;
  }

  /* Clear the error flag for this steam */
  clearerr(stream);

  s = fgets(buf, size, stream);

  if(s == NULL && ferror(stream)) {
    /* The read resulted in an error */
    plabort("Error reading from file");
  }
}