File: io.c

package info (click to toggle)
premake 3.7-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,148 kB
  • sloc: ansic: 20,490; cs: 15,185; makefile: 332
file content (203 lines) | stat: -rw-r--r-- 3,598 bytes parent folder | download
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**********************************************************************
 * Premake - io.c
 * File and directory I/O routines.
 *
 * Copyright (c) 2002-2005 Jason Perkins and the Premake project
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License in the file LICENSE.txt for details.
 **********************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#include "io.h"
#include "path.h"
#include "platform.h"

static char buffer[8192];
static FILE* file;


int io_chdir(const char* path)
{
	if (strlen(path) == 0)
		path = ".";
	return platform_chdir(path);
}

int io_closefile()
{
	fclose(file);
	return 1;
}


int io_copyfile(const char* src, const char* dest)
{
	return platform_copyfile(src, dest);
}


int io_direxists(const char* path)
{
	struct stat buf;
	if (stat(path, &buf) == 0)
	{
		if (buf.st_mode & S_IFDIR)
			return 1;
		else
			return 0;
	}
	else
	{
		return 0;
	}
}


int io_fileexists(const char* path)
{
	struct stat buf;
	if (stat(path, &buf) == 0)
	{
		if (buf.st_mode & S_IFDIR)
			return 0;
		else
			return 1;
	}
	else
	{
		return 0;
	}
}


const char* io_findlib(const char* name)
{
	if (platform_findlib(name, buffer, 8192))
		return buffer;
	else
		return NULL;
}


const char* io_getcwd()
{
	platform_getcwd(buffer, 8192);
	return buffer;
}


int io_mask_close(MaskHandle data)
{
	return platform_mask_close(data);
}


const char* io_mask_getname(MaskHandle data)
{
	return platform_mask_getname(data);
}


int io_mask_getnext(MaskHandle data)
{
	return platform_mask_getnext(data);
}


int io_mask_isfile(MaskHandle data)
{
	return platform_mask_isfile(data);
}


MaskHandle io_mask_open(const char* mask)
{
	return platform_mask_open(mask);
}


int io_mkdir(const char* path)
{
	/* Remember the current directory */
	char cwd[8192];
	platform_getcwd(cwd, 8192);

	/* Split the path and check each part in turn */
	strcpy(buffer, path);
	path = buffer;

	while (path != NULL)
	{
		char* ptr = strchr(path, '/');
		if (ptr != NULL)
			*ptr = '\0';

		platform_mkdir(path);
		platform_chdir(path);

		path = (ptr != NULL) ? ptr + 1 : NULL;
	}

	/* Restore the original working directory */
	platform_chdir(cwd);
	return 1;
}


int io_openfile(const char* path)
{
	/* Make sure that all parts of the path exist */
	io_mkdir(path_getdir(path));

	/* Now I can open the file */
	file = fopen(path, "w");
	if (file == NULL)
	{
		printf("** Unable to open file '%s' for writing\n", path);
		return 0;
	}
	else
	{
		return 1;
	}
}


void io_print(const char* format, ...)
{
	va_list args;
	va_start(args, format);
	vfprintf(file, format, args);
	va_end(args);
}


int io_remove(const char* path)
{
	return platform_remove(path);
}


int io_rmdir(const char* path, const char* dir)
{
	strcpy(buffer, path);
	if (strlen(buffer) > 0) 
		strcat(buffer, "/");
	strcat(buffer, dir);
	path_translateInPlace(buffer, NULL);
	return platform_rmdir(buffer);
}