2
* Compiz configuration system library
6
* Copyright (C) 2012 Canonical Ltd.
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
* Sam Spilsbury <sam.spilsbury@canonical.com>
31
#include <ccs-object.h>
32
#include <ccs_text_file_interface.h>
33
#include <ccs_text_file.h>
35
typedef struct _CCSUnixTextFilePrivate CCSUnixTextFilePrivate;
37
struct _CCSUnixTextFilePrivate
43
freeAndFinalizeCCSTextFile (CCSTextFile *file,
44
CCSObjectAllocationInterface *ai)
46
ccsObjectFinalize (file);
47
(*ai->free_) (ai->allocator, file);
51
ccsUnixTextFileReadFromStart (CCSTextFile *textFile)
53
CCSUnixTextFilePrivate *priv = GET_PRIVATE (CCSUnixTextFilePrivate, textFile);
54
FILE *completedUpgrades = priv->unixFile;
60
fseek (completedUpgrades, 0, SEEK_END);
61
cuSize = ftell (completedUpgrades);
62
rewind (completedUpgrades);
64
cuBuffer = calloc (cuSize + 1, sizeof (char));
69
cuReadSize = fread (cuBuffer, 1, cuSize, completedUpgrades);
72
ccsWarning ("Couldn't read completed upgrades file!");
74
if (cuReadSize != cuSize)
80
cuBuffer[cuSize] = '\0';
86
ccsUnixTextFileAppendString (CCSTextFile *textFile, const char *str)
88
CCSUnixTextFilePrivate *priv = GET_PRIVATE (CCSUnixTextFilePrivate, textFile);
89
FILE *completedUpgrades = priv->unixFile;
91
fprintf (completedUpgrades, "%s\n", str);
96
ccsUnixFreeTextFile (CCSTextFile *textFile)
98
CCSUnixTextFilePrivate *priv = GET_PRIVATE (CCSUnixTextFilePrivate, textFile);
100
fclose (priv->unixFile);
101
priv->unixFile = NULL;
103
freeAndFinalizeCCSTextFile (textFile,
104
textFile->object.object_allocation);
107
CCSTextFileInterface ccsUnixTextFileInterface =
109
ccsUnixTextFileReadFromStart,
110
ccsUnixTextFileAppendString,
114
const char * CCS_UNIX_TEXT_FILE_OPEN_MODE_READONLY = "r";
115
const char * CCS_UNIX_TEXT_FILE_OPEN_MODE_READWRITE = "r+";
116
const char * CCS_UNIX_TEXT_FILE_OPEN_MODE_READWRITECREATE = "a+";
119
openUnixFile (CCSTextFile *textFile,
120
CCSObjectAllocationInterface *ai,
122
const char *openMode)
124
FILE *file = fopen (path, openMode);
128
ccsObjectFinalize (textFile);
129
(*ai->free_) (ai->allocator, textFile);
137
static CCSUnixTextFilePrivate *
138
allocateCCSUnixTextFilePrivate (CCSTextFile *file,
139
CCSObjectAllocationInterface *ai)
141
CCSUnixTextFilePrivate *priv = (*ai->calloc_) (ai->allocator, 1, sizeof (CCSUnixTextFilePrivate));
145
freeAndFinalizeCCSTextFile (file, ai);
153
allocateCCSTextFile (CCSObjectAllocationInterface *ai)
155
CCSTextFile *textFile = (*ai->calloc_) (ai->allocator, 1, sizeof (CCSTextFile));
160
ccsObjectInit (textFile, ai);
166
ccsUnixTextFileNew (const char *path,
167
CCSTextFileOpenMode openMode,
168
CCSObjectAllocationInterface *ai)
170
const char *fopenMode = NULL;
175
fopenMode = CCS_UNIX_TEXT_FILE_OPEN_MODE_READONLY;
178
fopenMode = CCS_UNIX_TEXT_FILE_OPEN_MODE_READWRITE;
180
case ReadWriteCreate:
181
fopenMode = CCS_UNIX_TEXT_FILE_OPEN_MODE_READWRITECREATE;
185
CCSTextFile *textFile = allocateCCSTextFile (ai);
190
CCSUnixTextFilePrivate *priv = allocateCCSUnixTextFilePrivate (textFile, ai);
195
ccsObjectSetPrivate (textFile, (CCSPrivate *) priv);
197
FILE *unixFile = openUnixFile (textFile,
205
priv->unixFile = unixFile;
207
ccsObjectAddInterface (textFile, (const CCSInterface *) &ccsUnixTextFileInterface,
208
GET_INTERFACE_TYPE (CCSTextFileInterface));
209
ccsObjectRef (textFile);