src/share/vm/runtime/os.cpp

changeset 2751
677234770800
parent 2669
fc416c2556ec
child 2997
bf6481e5f96d
     1.1 --- a/src/share/vm/runtime/os.cpp	Mon Mar 28 12:48:08 2011 +0200
     1.2 +++ b/src/share/vm/runtime/os.cpp	Wed Mar 30 19:38:07 2011 +0400
     1.3 @@ -1291,3 +1291,41 @@
     1.4    }
     1.5    return result;
     1.6  }
     1.7 +
     1.8 +// Read file line by line, if line is longer than bsize,
     1.9 +// skip rest of line.
    1.10 +int os::get_line_chars(int fd, char* buf, const size_t bsize){
    1.11 +  size_t sz, i = 0;
    1.12 +
    1.13 +  // read until EOF, EOL or buf is full
    1.14 +  while ((sz = (int) read(fd, &buf[i], 1)) == 1 && i < (bsize-1) && buf[i] != '\n') {
    1.15 +     ++i;
    1.16 +  }
    1.17 +
    1.18 +  if (buf[i] == '\n') {
    1.19 +    // EOL reached so ignore EOL character and return
    1.20 +
    1.21 +    buf[i] = 0;
    1.22 +    return (int) i;
    1.23 +  }
    1.24 +
    1.25 +  buf[i+1] = 0;
    1.26 +
    1.27 +  if (sz != 1) {
    1.28 +    // EOF reached. if we read chars before EOF return them and
    1.29 +    // return EOF on next call otherwise return EOF
    1.30 +
    1.31 +    return (i == 0) ? -1 : (int) i;
    1.32 +  }
    1.33 +
    1.34 +  // line is longer than size of buf, skip to EOL
    1.35 +  int ch;
    1.36 +  while (read(fd, &ch, 1) == 1 && ch != '\n') {
    1.37 +    // Do nothing
    1.38 +  }
    1.39 +
    1.40 +  // return initial part of line that fits in buf.
    1.41 +  // If we reached EOF, it will be returned on next call.
    1.42 +
    1.43 +  return (int) i;
    1.44 +}

mercurial