File: cppFileUtils.h

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (28 lines) | stat: -rw-r--r-- 898 bytes parent folder | download | duplicates (4)
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
/* return number of bytes read, throw string on error */
#ifndef CPPFILEUTIL_H
#define CPPFILEUTIL_H
#include "stringutil.h"

template <class W>
u_int32_t fread_loop(FILE *theFile, u_int32_t elems2Read, W *bufp) {
  u_int32_t totalRead = fread(bufp, sizeof(W), elems2Read, theFile);
  bufp += totalRead; 
  while (elems2Read > totalRead) {
    u_int32_t numread = fread(bufp+numread, sizeof(W), elems2Read - totalRead, theFile); 
    if (!numread) {
      string errstring = string("Data read failed after ") +
    doubleToString(totalRead+numread) +
    "bytes: "; 
      int err = ferror(theFile);
      if (err) throw errstring + "error "+doubleToString(err);
      if (feof(theFile)) throw errstring + "unexpected EOF";
      else throw errstring + "zero items read, but no error or EOF (impossible!)";
    }
    bufp += numread; 
    totalRead += numread; 
  }
  return totalRead; 
}

#endif