* Bring back rbuild build to be used until bug 6372 is fixed.
[reactos.git] / tools / rbuild / filesupportcode.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22
23 using std::string;
24
25 /* static */ void
26 FileSupportCode::WriteIfChanged ( char* outbuf,
27 const string& filename,
28 bool ignoreError )
29 {
30 FILE* out;
31 unsigned int end;
32 char* cmpbuf;
33 unsigned int stat;
34
35 out = fopen ( filename.c_str (), "rb" );
36 if ( out == NULL )
37 {
38 out = fopen ( filename.c_str (), "wb" );
39 if ( out == NULL )
40 {
41 if ( ignoreError )
42 return;
43 throw AccessDeniedException ( filename );
44 }
45 fputs ( outbuf, out );
46 fclose ( out );
47 return;
48 }
49
50 fseek ( out, 0, SEEK_END );
51 end = ftell ( out );
52 cmpbuf = (char*) malloc ( end );
53 if ( cmpbuf == NULL )
54 {
55 fclose ( out );
56 throw OutOfMemoryException ();
57 }
58
59 fseek ( out, 0, SEEK_SET );
60 stat = fread ( cmpbuf, 1, end, out );
61 if ( stat != end )
62 {
63 free ( cmpbuf );
64 fclose ( out );
65 throw AccessDeniedException ( filename );
66 }
67 if ( end == strlen ( outbuf ) && memcmp ( cmpbuf, outbuf, end ) == 0 )
68 {
69 free ( cmpbuf );
70 fclose ( out );
71 return;
72 }
73
74 free ( cmpbuf );
75 fclose ( out );
76 out = fopen ( filename.c_str (), "wb" );
77 if ( out == NULL )
78 {
79 throw AccessDeniedException ( filename );
80 }
81
82 stat = fwrite ( outbuf, 1, strlen ( outbuf ), out);
83 if ( strlen ( outbuf ) != stat )
84 {
85 fclose ( out );
86 throw AccessDeniedException ( filename );
87 }
88
89 fclose ( out );
90 }