/* */
This source file includes following definitions.
- open_input_file
- open_output_file
- get_descripter
- close_file
1 /*
2 * Copyright (c) 2006
3 * Tama Communications Corporation
4 *
5 * This file is part of GNU GLOBAL.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <stdio.h>
25 #ifdef STDC_HEADERS
26 #include <stdlib.h>
27 #endif
28
29 #include "checkalloc.h"
30 #include "die.h"
31 #include "fileop.h"
32 #include "makepath.h"
33 #include "strlimcpy.h"
34
35 /**
36 @file
37
38 File operation: usage
39
40 @par [WRITE]
41 @code
42 int compress = cflag ? 1 : 0;
43
44 FILEOP *fileop = open_output_file(path, compress);
45 FILE *op = get_descripter(fileop);
46
47 fputs("Hello", op);
48 ...
49 close_file(fileop);
50 @endcode
51
52 @par [READ]
53 @code
54 FILEOP *fileop = open_input_file(path);
55 FILE *ip = get_descripter(fileop);
56
57 fgets(buf, sizeof(buf), ip);
58 ...
59 close_file(fileop);
60 @endcode
61 */
62 /**
63 * open input file.
64 *
65 * @param[in] path path name
66 * @return file descripter
67 */
68 FILEOP *
69 open_input_file(const char *path)
70 {
71 FILE *fp = fopen(path, "r");
72 FILEOP *fileop;
73
74 if (fp == NULL)
75 die("cannot open file '%s'.", path);
76 fileop = check_calloc(sizeof(FILEOP), 1);
77 fileop->fp = fp;
78 strlimcpy(fileop->path, path, sizeof(fileop->path));
79 fileop->type = FILEOP_INPUT;
80 return fileop;
81 }
82 /**
83 * open output file
84 *
85 * @param[in] path path name
86 * @param[in] compress 0: normal, 1: compress
87 * @return file descripter
88 *
89 * @note Uses the @NAME{gzip} program to compress, which should already be on your system.
90 */
91 FILEOP *
92 open_output_file(const char *path, int compress)
93 {
94 FILEOP *fileop;
95 FILE *fp;
96 char command[MAXFILLEN];
97
98 if (compress) {
99 snprintf(command, sizeof(command), "gzip -c >\"%s\"", path);
100 fp = popen(command, "w");
101 if (fp == NULL)
102 die("cannot create pipe.");
103 } else {
104 fp = fopen(path, "w");
105 if (fp == NULL)
106 die("cannot create file '%s'.", path);
107 }
108 fileop = check_calloc(sizeof(FILEOP), 1);
109 strlimcpy(fileop->path, path, sizeof(fileop->path));
110 if (compress)
111 strlimcpy(fileop->command, command, sizeof(fileop->command));
112 fileop->type = FILEOP_OUTPUT;
113 if (compress)
114 fileop->type |= FILEOP_COMPRESS;
115 fileop->fp = fp;
116 return fileop;
117 }
118 /**
119 * get UNIX file descripter
120 *
121 * ( See open_input_file() and open_output_file() ).
122 */
123 FILE *
124 get_descripter(FILEOP *fileop)
125 {
126 return fileop->fp;
127 }
128 /**
129 * close_file: close file
130 *
131 * @param[in] fileop file descripter
132 */
133 void
134 close_file(FILEOP *fileop)
135 {
136 if (fileop->type & FILEOP_COMPRESS) {
137 if (pclose(fileop->fp) != 0)
138 die("terminated abnormally. '%s'", fileop->command);
139 } else
140 fclose(fileop->fp);
141 free(fileop);
142 }
/* */