blocxx
SafeCStringIO.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
38 #include "blocxx/BLOCXX_config.h"
39 #include "blocxx/SafeCStringIO.hpp"
40 #include "blocxx/SafeCString.hpp"
41  /* to get OverflowException and RESULT_TRUNCATED */
42 #include "blocxx/String.hpp"
43 #include "blocxx/StringBuffer.hpp"
44 #include "blocxx/IOException.hpp"
45 #include <cstdio>
46 
47 namespace BLOCXX_NAMESPACE
48 {
49 namespace SafeCString
50 {
51 
52 char * fgets_trunc(char * dst, std::size_t dstsize, FILE * fp)
53 {
54  char * res = std::fgets(dst, dstsize, fp);
55  if (!res)
56  {
57  if (feof(fp))
58  {
59  return 0;
60  }
61  BLOCXX_THROW(blocxx::IOException, "read error");
62  }
63  return res;
64 }
65 
66 char * fgets_check(char * dst, std::size_t dstsize, FILE * fp)
67 {
68  std::size_t const end = dstsize - 1;
69  char savechar = dst[end];
70  dst[end] = ' '; // anything but '\0'
71  char * res = std::fgets(dst, dstsize, fp);
72  char endchar = dst[end];
73  dst[end] = savechar;
74  if (res)
75  {
76  if (endchar == '\0' && (end == 0 || dst[end - 1] != '\n'))
77  {
78  // No newline at end. Either the input line was truncated, or
79  // we read the last line of the file and it had no newline.
80  // We test for EOF to distinguish the two cases. Since the EOF
81  // marker doesn't get set until we actually try to read past EOF,
82  // we first peek one character ahead.
83  std::ungetc(std::fgetc(fp), fp);
84  if (!feof(fp))
85  {
87  OverflowException, "fgets overflow", RESULT_TRUNCATED);
88  }
89  }
90  return res;
91  }
92  else
93  {
94  if (feof(fp))
95  {
96  return 0;
97  }
98  BLOCXX_THROW(blocxx::IOException, "read error");
99  }
100 }
101 
102 String fget_string(FILE * fp, std::size_t const max_chars)
103 {
104  // This could perhaps be made more efficient by reading directly into a
105  // custom resizable character array.
106  std::size_t const BUFSIZE = 8 * 1024;
107  StringBuffer sb;
108  char buf[BUFSIZE];
109  while (!sb.endsWith('\n') && sb.length() <= max_chars &&
110  fgets_trunc(buf, fp))
111  {
112  sb.append(buf);
113  }
114  if (sb.length() > max_chars)
115  {
116  BLOCXX_THROW(StringConversionException, "input line too long");
117  }
118  return sb.releaseString();
119 }
120 
121 } // namespace SafeCString
122 } // namespace BLOCXX_NAMESPACE