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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* $Source: /cvsroot/cgoban1/cgoban1/src/sgfOut.c,v $
* $Revision: 1.3 $
* $Author: wmshub $
* $Date: 2002/05/31 23:40:54 $
*
* src/sgfOut.c, part of Complete Goban (game program)
* Copyright (C) 1995-1996 William Shubert.
* See "configure.h.in" for more copyright information.
*/
#include <ctype.h>
#include <wms.h>
#include <wms/rnd.h>
#include <wms/str.h>
#include "cgoban.h"
#include "sgf.h"
#include "msg.h"
#ifdef _SGFOUT_H_
LEVELIZATION ERROR
#endif
#include "sgfOut.h"
/**********************************************************************
* Forward Declarations
**********************************************************************/
static void printColor(Str *out, SgfElem *me);
static void printPoint(Str *out, Sgf *mc, SgfElem *me);
static bool writeNode(FILE *f, Sgf *mc, SgfElem *me, int *err,
int col, Str *tmp1, Str *tmp2);
static void printString(Str *out, SgfElem *me);
static SgfElem *printPoints(Str *out, Sgf *mc, SgfElem *me);
static SgfElem *printLabelPoints(Str *out, Sgf *mc, SgfElem *me);
/**********************************************************************
* Functions
**********************************************************************/
bool sgf_writeFile(Sgf *mc, const char *fname, int *err) {
FILE *f;
SgfElem *me;
Str nodeOut, tmp;
assert(MAGIC(mc));
f = fopen(fname, "w");
if (f == NULL) {
if (err)
*err = errno;
return(FALSE);
}
fprintf(f, "(;GM[1]FF[3]\n");
str_init(&nodeOut);
str_init(&tmp);
if (mc->top.childH == mc->top.childT) {
if (!writeNode(f, mc, mc->top.childH, err, 0, &nodeOut, &tmp)) {
fclose(f);
str_deinit(&nodeOut);
str_deinit(&tmp);
return(FALSE);
}
} else {
for (me = mc->top.childH; me; me = me->sibling) {
fprintf(f, "\n(");
if (!writeNode(f, mc, me, err, 0, &nodeOut, &tmp)) {
fclose(f);
str_deinit(&nodeOut);
str_deinit(&tmp);
return(FALSE);
}
fprintf(f, ")");
}
}
fprintf(f, "\n)\n");
fclose(f);
str_deinit(&nodeOut);
str_deinit(&tmp);
return(TRUE);
}
static bool writeNode(FILE *f, Sgf *mc, SgfElem *me, int *err,
int col, Str *nodeOut, Str *tmp) {
bool crNeeded; /* Flag; put this on its own line? */
while (me) {
crNeeded = FALSE;
switch(me->type) {
case sgfType_node:
str_copyChar(nodeOut, ';');
break;
case sgfType_unknown:
str_copyChars(nodeOut, str_chars(me->sVal));
break;
case sgfType_size:
str_print(nodeOut, "SZ[%d]", me->iVal);
break;
case sgfType_rules:
str_print(nodeOut, "RU[%s]", msg_ruleNames[(int)(me->iVal)]);
break;
case sgfType_handicap:
str_print(nodeOut, "HA[%d]", me->iVal);
break;
case sgfType_komi:
str_print(nodeOut, "KM[%g]", (double)me->iVal / 2.0);
break;
case sgfType_time:
str_print(nodeOut, "TM[%s]", str_chars(me->sVal));
break;
case sgfType_copyright:
str_copyChars(nodeOut, "CP");
printString(nodeOut, me);
break;
case sgfType_playerName:
crNeeded = TRUE;
str_copyChar(nodeOut, 'P');
printColor(nodeOut, me);
printString(nodeOut, me);
break;
case sgfType_title:
crNeeded = TRUE;
str_copyChars(nodeOut, "GN");
printString(nodeOut, me);
break;
case sgfType_playerRank:
str_clip(nodeOut, 0);
printColor(nodeOut, me);
str_catChar(nodeOut, 'R');
printString(nodeOut, me);
break;
case sgfType_event:
crNeeded = TRUE;
str_copyChars(nodeOut, "EV");
printString(nodeOut, me);
break;
case sgfType_source:
crNeeded = TRUE;
str_copyChars(nodeOut, "SO");
printString(nodeOut, me);
break;
case sgfType_gameComment:
str_copyChars(nodeOut, "GC");
printString(nodeOut, me);
break;
case sgfType_date:
crNeeded = TRUE;
str_copyChars(nodeOut, "DT");
printString(nodeOut, me);
break;
case sgfType_whoseMove:
assert(goStone_isStone(me->gVal));
str_print(nodeOut, "PL[%c]", (me->gVal == goStone_white) ? 'W' : 'B');
break;
case sgfType_move:
str_clip(nodeOut, 0);
printColor(nodeOut, me);
printPoint(nodeOut, mc, me);
break;
case sgfType_pass:
str_clip(nodeOut, 0);
printColor(nodeOut, me);
if (mc->longLoc)
str_catChars(nodeOut, "[]");
else
str_catChars(nodeOut, "[tt]");
break;
case sgfType_timeLeft:
str_clip(tmp, 0);
printColor(tmp, me);
str_print(nodeOut, "%sL[%d]", str_chars(tmp), me->iVal);
break;
case sgfType_stonesLeft:
str_copyChar(tmp, 'O');
printColor(tmp, me);
str_print(nodeOut, "%s[%d]", str_chars(tmp), me->iVal);
break;
case sgfType_setBoard:
str_copyChar(nodeOut, 'A');
printColor(nodeOut, me);
me = printPoints(nodeOut, mc, me);
break;
case sgfType_territory:
str_copyChar(nodeOut, 'T');
printColor(nodeOut, me);
me = printPoints(nodeOut, mc, me);
break;
case sgfType_triangle:
str_copyChars(nodeOut, "TR");
me = printPoints(nodeOut, mc, me);
break;
case sgfType_circle:
str_copyChars(nodeOut, "CR");
me = printPoints(nodeOut, mc, me);
break;
case sgfType_square:
str_copyChars(nodeOut, "SQ");
me = printPoints(nodeOut, mc, me);
break;
case sgfType_mark:
str_copyChars(nodeOut, "MA");
me = printPoints(nodeOut, mc, me);
break;
case sgfType_label:
str_copyChars(nodeOut, "LB");
me = printLabelPoints(nodeOut, mc, me);
break;
case sgfType_comment:
str_copyChars(nodeOut, "C");
printString(nodeOut, me);
break;
case sgfType_result:
crNeeded = TRUE;
str_copyChars(nodeOut, "RE");
printString(nodeOut, me);
break;
case sgfType_place:
str_copyChars(nodeOut, "PC");
printString(nodeOut, me);
break;
case sgfType_style:
str_copyChars(nodeOut, "SY");
printString(nodeOut, me);
break;
default:
fprintf(stderr, "BOGUS PROPERTY TO PRINT!\n");
for (;;);
break;
}
if (col && (crNeeded || (col + str_len(nodeOut) > 70))) {
fprintf(f, "\n");
col = 0;
}
col += str_len(nodeOut);
fprintf(f, "%s", str_chars(nodeOut));
if (crNeeded) {
fprintf(f, "\n");
col = 0;
}
if (me->childH == me->childT) {
me = me->childH;
} else {
for (me = me->childH; me; me = me->sibling) {
fprintf(f, "\n(");
if (!writeNode(f, mc, me, err, 1, nodeOut, tmp))
return(FALSE);
fprintf(f, ")\n");
col = 0;
}
}
}
return(TRUE);
}
static void printColor(Str *out, SgfElem *me) {
if (me->gVal == goStone_white)
str_catChar(out, 'W');
else if (me->gVal == goStone_black)
str_catChar(out, 'B');
else {
assert(me->gVal == goStone_empty);
str_catChar(out, 'E');
}
}
static void printPoint(Str *out, Sgf *mc, SgfElem *me) {
if (mc->longLoc) {
str_catChar(out, '[');
str_catChar(out, me->lVal[0]);
str_catChar(out, me->lVal[1]);
str_catChar(out, me->lVal[2]);
str_catChar(out, me->lVal[3]);
str_catChar(out, ']');
} else {
str_catChar(out, '[');
str_catChar(out, me->lVal[1]);
str_catChar(out, me->lVal[3]);
str_catChar(out, ']');
}
}
static SgfElem *printPoints(Str *out, Sgf *mc, SgfElem *me) {
printPoint(out, mc, me);
while ((me->childH != NULL) &&
(me->childH == me->childT) &&
(me->childH->type == me->type) &&
(me->childH->gVal == me->gVal)) {
me = me->childH;
printPoint(out, mc, me);
}
return(me);
}
static void printLabelPoint(Str *out, Sgf *mc, SgfElem *me) {
if (mc->longLoc) {
str_catChar(out, '[');
str_catChar(out, me->lVal[0]);
str_catChar(out, me->lVal[1]);
str_catChar(out, me->lVal[2]);
str_catChar(out, me->lVal[3]);
str_catChar(out, ':');
str_cat(out, me->sVal);
str_catChar(out, ']');
} else {
str_catChar(out, '[');
str_catChar(out, me->lVal[1]);
str_catChar(out, me->lVal[3]);
str_catChar(out, ':');
str_cat(out, me->sVal);
str_catChar(out, ']');
}
}
static SgfElem *printLabelPoints(Str *out, Sgf *mc, SgfElem *me) {
printLabelPoint(out, mc, me);
while (me->childH &&
(me->childH == me->childT) &&
(me->childH->type == me->type) &&
(me->childH->gVal == me->gVal)) {
me = me->childH;
printLabelPoint(out, mc, me);
}
return(me);
}
static void printString(Str *out, SgfElem *me) {
const char *str = str_chars(me->sVal);
str_catChar(out, '[');
while (*str) {
if ((*str == ']') || (*str == '[') || (*str == '\\'))
str_catChar(out, '\\');
str_catChar(out, *str);
++str;
}
str_catChar(out, ']');
}
|