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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*** /
This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2009 Andrew Trevorrow and Tomas Rokicki.
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 for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Web site: http://sourceforge.net/projects/golly
Authors: rokicki@gmail.com andrew@trevorrow.com
/ ***/
#include "writepattern.h"
#include "lifealgo.h"
#include "util.h" // for *progress calls
#include <cstdio>
#include <cstdlib>
#include <cstring>
#ifdef __APPLE__
#define BUFFSIZE 4096 // 4K is best for Mac OS X
#else
#define BUFFSIZE 8192 // 8K is best for Windows and other platforms???
#endif
// globals for writing RLE files
static char outbuff[BUFFSIZE];
static size_t outpos; // current write position in outbuff
static unsigned int currsize; // current file size (for showing in progress dialog)
static bool badwrite; // fwrite failed?
// using buffered putchar instead of fputc is about 20% faster on Mac OS X
void putchar(char ch, FILE *f) {
if (badwrite) return;
if (outpos == BUFFSIZE) {
if (fwrite(outbuff, 1, outpos, f) != outpos) badwrite = true;
outpos = 0;
currsize += BUFFSIZE;
}
outbuff[outpos] = ch;
outpos++;
}
const int WRLE_NONE = -3 ;
const int WRLE_EOP = -2 ;
const int WRLE_NEWLINE = -1 ;
// output of RLE pattern data is channelled thru here to make it easier to
// ensure all lines have <= 70 characters
void AddRun(FILE *f,
int state, // in: state of cell to write
int multistate, // true if #cell states > 2
unsigned int &run, // in and out
unsigned int &linelen) // ditto
{
unsigned int i, numlen;
char numstr[32];
if ( run > 1 ) {
sprintf(numstr, "%u", run);
numlen = strlen(numstr);
} else {
numlen = 0; // no run count shown if 1
}
if ( linelen + numlen + 1 + multistate > 70 ) {
putchar('\n', f);
linelen = 0;
}
i = 0;
while (i < numlen) {
putchar(numstr[i], f);
i++;
}
if (multistate) {
if (state <= 0)
putchar(".$!"[-state], f) ;
else {
if (state > 24) {
int hi = (state - 25) / 24 ;
putchar((char)(hi + 'p'), f) ;
linelen++ ;
state -= (hi + 1) * 24 ;
}
putchar((char)('A' + state - 1), f) ;
}
} else
putchar("!$bo"[state+2], f) ;
linelen += numlen + 1;
run = 0; // reset run count
}
// write current pattern to file using extended RLE format
const char *writerle(FILE *f, char *comments, lifealgo &imp,
int top, int left, int bottom, int right,
bool xrle)
{
badwrite = false;
if (xrle) {
// write out #CXRLE line; note that the XRLE indicator is prefixed
// with #C so apps like Life32 and MCell will ignore the line
fprintf(f, "#CXRLE Pos=%d,%d", left, top);
if (imp.getGeneration() > bigint::zero)
fprintf(f, " Gen=%s", imp.getGeneration().tostring('\0'));
fputs("\n", f);
}
char *endcomms = NULL;
if (comments && comments[0]) {
// write given comment line(s) -- can't just do fputs(comments,f)
// because comments might include arbitrary text after the "!"
char *p = comments;
while (*p == '#') {
while (*p != '\n') p++;
p++;
}
if (p != comments) {
char savech = *p;
*p = '\0';
fputs(comments, f);
*p = savech;
}
// any comment lines not starting with # will be written after "!"
if (*p != '\0') endcomms = p;
}
if ( imp.isEmpty() || top > bottom || left > right ) {
// empty pattern
fprintf(f, "x = 0, y = 0, rule = %s\n!\n", imp.getrule());
} else {
// do header line
unsigned int wd = right - left + 1;
unsigned int ht = bottom - top + 1;
sprintf(outbuff, "x = %u, y = %u, rule = %s\n", wd, ht, imp.getrule());
outpos = strlen(outbuff);
// do RLE data
unsigned int linelen = 0;
unsigned int brun = 0;
unsigned int orun = 0;
unsigned int dollrun = 0;
int laststate = WRLE_NONE ;
int multistate = imp.NumCellStates() > 2 ;
int cx, cy;
// for showing accurate progress we need to add pattern height to pop count
// in case this is a huge pattern with many blank rows
double maxcount = imp.getPopulation().todouble() + ht;
double accumcount = 0;
int currcount = 0;
int v = 0 ;
for ( cy=top; cy<=bottom; cy++ ) {
// set lastchar to anything except 'o' or 'b'
laststate = WRLE_NONE ;
currcount++;
for ( cx=left; cx<=right; cx++ ) {
int skip = imp.nextcell(cx, cy, v);
if (skip + cx > right)
skip = -1; // pretend we found no more live cells
if (skip > 0) {
// have exactly "skip" dead cells here
if (laststate == 0) {
brun += skip;
} else {
if (orun > 0) {
// output current run of live cells
AddRun(f, laststate, multistate, orun, linelen);
}
laststate = 0 ;
brun = skip;
}
}
if (skip >= 0) {
// found next live cell in this row
cx += skip;
if (laststate == v) {
orun++;
} else {
if (dollrun > 0)
// output current run of $ chars
AddRun(f, WRLE_NEWLINE, multistate, dollrun, linelen);
if (brun > 0)
// output current run of dead cells
AddRun(f, 0, multistate, brun, linelen);
if (orun > 0)
AddRun(f, laststate, multistate, orun, linelen) ;
laststate = v ;
orun = 1;
}
currcount++;
} else {
cx = right + 1; // done this row
}
if (currcount > 1024) {
char msg[128];
accumcount += currcount;
currcount = 0;
sprintf(msg, "File size: %.2g MB", double(currsize) / 1048576.0);
if (lifeabortprogress(accumcount / maxcount, msg)) break;
}
}
// end of current row
if (isaborted()) break;
if (laststate == 0)
// forget dead cells at end of row
brun = 0;
else if (laststate >= 0)
// output current run of live cells
AddRun(f, laststate, multistate, orun, linelen);
dollrun++;
}
// terminate RLE data
dollrun = 1;
AddRun(f, WRLE_EOP, multistate, dollrun, linelen);
putchar('\n', f);
// flush outbuff
if (outpos > 0 && !badwrite && fwrite(outbuff, 1, outpos, f) != outpos)
badwrite = true;
}
if (endcomms) fputs(endcomms, f);
if (badwrite)
return "Failed to write output buffer!";
else
return 0;
}
const char *writelife105(FILE *f, char *comments, lifealgo &imp)
{
fputs("#Life 1.05\n", f);
fprintf(f, "#R %s\n", imp.getrule());
if (comments && comments[0]) {
// write given comment line(s)
fputs(comments, f);
}
return "Not yet implemented.";
}
const char *writemacrocell(FILE *f, char *comments, lifealgo &imp)
{
if (imp.hyperCapable())
return imp.writeNativeFormat(f, comments);
else
return "Not yet implemented.";
}
const char *writepattern(const char *filename, lifealgo &imp, pattern_format format,
int top, int left, int bottom, int right)
{
FILE *f;
// extract any comments if file exists so we can copy them to new file
char *commptr = NULL;
f = fopen(filename, "r");
if (f) {
fclose(f);
const char *err = readcomments(filename, &commptr);
if (err) {
if (commptr) free(commptr);
return err;
}
}
f = fopen(filename, "w");
if (f == 0) {
if (commptr) free(commptr);
return "Can't create pattern file!";
}
// skip past any old #CXRLE lines at start of existing XRLE file
char *comments = commptr;
if (comments) {
while (strncmp(comments, "#CXRLE", 6) == 0) {
while (*comments != '\n') comments++;
comments++;
}
}
currsize = 0;
lifebeginprogress("Writing pattern file");
const char *errmsg = NULL;
switch (format) {
case RLE_format:
errmsg = writerle(f, comments, imp, top, left, bottom, right, false);
break;
case XRLE_format:
errmsg = writerle(f, comments, imp, top, left, bottom, right, true);
break;
case L105_format:
// Life 1.05 format ignores given edges
errmsg = writelife105(f, comments, imp);
break;
case MC_format:
// macrocell format ignores given edges
errmsg = writemacrocell(f, comments, imp);
break;
default:
errmsg = "Unsupported pattern format!";
}
if (errmsg == NULL && ferror(f))
errmsg = "Error occurred writing file; maybe disk is full?";
lifeendprogress();
fclose(f);
if (commptr) free(commptr);
if (isaborted())
return "File contains truncated pattern.";
else
return errmsg;
}
|