1 /*
2  * Copyright (C) 2006-2021 Registro.br. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 1. Redistribution of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY REGISTRO.BR ``AS IS AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIE OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16  * EVENT SHALL REGISTRO.BR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
21  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 /* $Id: */
26 
27 #include "config.h"
28 
29 #include <cstdio>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 
33 #include "FileUtil.H"
34 #include "StrUtil.H"
35 #include "IoException.H"
36 
37 LIBEPP_NICBR_NS_BEGIN
38 
read_file(const string & filename)39 string FileUtil::read_file(const string &filename)
40 {
41 	const int buf_sz = 16383;
42 	FILE* input_file;
43 	int read_bytes = 0;
44 	int flag = 0;
45 	string file_content = "";
46 	char buffer[buf_sz+1];
47 
48 	struct stat buf;
49 	if (stat(filename.c_str(), &buf) < 0 || !S_ISREG(buf.st_mode))
50 		throw IoException(IoException::OPEN_ERR,
51 		                  StrUtil::to_string("Error opening file: [%s]",
52 		                                     filename.c_str()));
53 
54 	input_file = fopen(filename.c_str(), "r");
55 	if (input_file == NULL)
56 		throw IoException(IoException::OPEN_ERR,
57 		                  StrUtil::to_string("Error opening file: [%s]",
58 		                                     filename.c_str()));
59 
60 	while (1) {
61 		read_bytes = fread(buffer,1,buf_sz,input_file);
62 		flag = ferror(input_file);
63 		if (flag)
64 			throw IoException(IoException::READ_ERR,
65 			                  StrUtil::to_string("Error reading file: [%s]",
66 			                                     filename.c_str()));
67 
68 		if (!read_bytes)
69 			break;
70 		buffer[read_bytes] = '\0';
71 		file_content += buffer;
72 		if (read_bytes != buf_sz)
73 			break;
74 	}
75 
76 	flag = fclose(input_file);
77 	if (flag)
78 		throw IoException(IoException::CLOSE_ERR,
79 		                  StrUtil::to_string("Error closing file: [%s]",
80 		                                     filename.c_str()));
81 
82 	return file_content;
83 }
84 
85 LIBEPP_NICBR_NS_END
86