aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_moves.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-03-25 15:17:00 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-03-25 15:17:00 +0100
commit0f4931c8de298b0f97aba1757c9538f48adb30c6 (patch)
tree4778096a091b980bc0f44fe3bfdfbcb65abcb6a0 /src/core/io_moves.h
parent9154476ba6bf400c1eb379f0a3aa6caae2d06d4d (diff)
downloadnissy-core-0f4931c8de298b0f97aba1757c9538f48adb30c6.tar.gz
nissy-core-0f4931c8de298b0f97aba1757c9538f48adb30c6.zip
Tiny bugfix in solution writing
Diffstat (limited to '')
-rw-r--r--src/core/io_moves.h23
1 files changed, 9 insertions, 14 deletions
diff --git a/src/core/io_moves.h b/src/core/io_moves.h
index 8c56a9f..b00682b 100644
--- a/src/core/io_moves.h
+++ b/src/core/io_moves.h
@@ -79,36 +79,31 @@ writemoves(
79 char buf[buf_size] 79 char buf[buf_size]
80) 80)
81{ 81{
82 size_t i, len, written; 82 size_t i, len, w;
83 const char *s; 83 const char *s;
84 char *b;
85 84
86 if (buf_size == 0) { 85 if (buf_size == 0) {
87 LOG("Error: cannot write moves to buffer of size 0.\n"); 86 LOG("Error: cannot write moves to buffer of size 0.\n");
88 return NISSY_ERROR_BUFFER_SIZE; 87 return NISSY_ERROR_BUFFER_SIZE;
89 } 88 }
90 89
91 for (i = 0, b = buf, written = 0; i < nmoves; i++, b++, written++) { 90 for (i = 0, w = 0; i < nmoves; i++, w++) {
92 s = movestr[m[i]]; 91 s = movestr[m[i]];
93 len = strlen(s); 92 len = strlen(s);
94 if (len + written >= buf_size) { 93 if (len + w >= buf_size) {
95 LOG("Error: the given buffer is too small for " 94 LOG("Error: the given buffer is too small for "
96 "writing the given moves.\n"); 95 "writing the given moves.\n");
97 goto writemoves_error; 96 goto writemoves_error;
98 } 97 }
99 memcpy(b, s, len); 98 memcpy(buf+w, s, len);
100 written += len; 99 w += len;
101 b += len; 100 buf[w] = ' ';
102 *b = ' ';
103 } 101 }
104 102
105 if (b == buf) 103 if (w > 0) w--; /* Remove last space */
106 written = 1; /* Nothing written, only NULL-terminator */ 104 buf[w] = '\0';
107 else
108 b--; /* Remove last space */
109 *b = '\0';
110 105
111 return (int64_t)written; 106 return (int64_t)w;
112 107
113writemoves_error: 108writemoves_error:
114 *buf = '\0'; 109 *buf = '\0';

Generated with cgit - Back to sebastiano.tronto.net