Skip to content

Commit faf0ba5

Browse files
committed
fix bug and improve performance
1 parent 451fbe8 commit faf0ba5

File tree

8 files changed

+43
-38
lines changed

8 files changed

+43
-38
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ tag:
7373
echo workspace not clean; \
7474
exit 1; \
7575
fi; \
76+
if grep dev version; then \
77+
echo this is a dev version; \
78+
exit 1; \
79+
fi; \
80+
printf 'tag: '; \
81+
cat version; \
7682
git tag "v$$(cat version)"
7783

7884
arch:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ See the man page `hardv(1)` for detail about how to implement a mod.
171171
Installation
172172
------------
173173

174-
- Chose a version from
174+
- Choose a version from
175175
the [release page](https://github.com/dongyx/hardv/releases);
176176
The latest is always recommended unless otherwise noted in the
177177
release note.
@@ -185,7 +185,7 @@ release note.
185185
$ sudo make install
186186
~~~
187187

188-
`make install` copies file to `/usr/local` by default.
188+
`make install` copies files to `/usr/local` by default.
189189
The man page `hardv(1)` is shipped with the installation.
190190

191191
Input Format

ctab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ WERR: fclose(fp);
8383
static char *backup(char *src)
8484
{
8585
static char dst[PATHSZ];
86-
char *ret, buf[BUFSIZ];
86+
char *ret, buf[8192];
8787
int fd[2], n;
8888

8989
ret = NULL;

learn.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static int sety(struct card *card, time_t now);
3232
static int setn(struct card *card, time_t now);
3333
static void preset(struct card *card, time_t now,
3434
time_t *prev, time_t *next, time_t *diff);
35-
char *indent(char *s, char *buf, int n);
35+
int pindent(char *s);
3636

3737
int learn(char *filename, int now, struct learnopt *opt)
3838
{
@@ -146,16 +146,15 @@ static int exemod(struct card *card, time_t now)
146146

147147
static int recall(struct card *card, time_t now)
148148
{
149-
char in[BUFSIZ], ques[VALSZ], answ[VALSZ], buf[VALSZ];
149+
char in[BUFSIZ], ques[VALSZ], answ[VALSZ];
150150

151151
if (learnopt->any)
152152
putchar('\n');
153-
indent(normval(getques(card), buf, VALSZ), ques, VALSZ);
154-
indent(normval(getansw(card), buf, VALSZ), answ, VALSZ);
153+
normval(getques(card), ques, VALSZ);
154+
normval(getansw(card), answ, VALSZ);
155155
puts("Q:\n");
156-
printf("%s\n", ques);
157-
if (ques[strlen(ques) - 1] != '\n')
158-
putchar('\n');
156+
pindent(ques);
157+
puts("\n");
159158
fflush(stdout);
160159
CHECK:
161160
fputs("Press <ENTER> to check the answer.\n", stdout);
@@ -169,9 +168,8 @@ static int recall(struct card *card, time_t now)
169168
if (strcmp(in, "\n"))
170169
goto CHECK;
171170
puts("A:\n");
172-
printf("%s\n", answ);
173-
if (answ[strlen(answ) - 1] != '\n')
174-
putchar('\n');
171+
pindent(answ);
172+
puts("\n");
175173
fflush(stdout);
176174
QUERY:
177175
fputs("Do you recall? (y/n/s)\n", stdout);
@@ -245,18 +243,14 @@ static void preset(struct card *card, time_t now,
245243
*diff = DAY;
246244
}
247245

248-
char *indent(char *s, char *buf, int n)
246+
int pindent(char *s)
249247
{
250-
char *sp, *bp;
248+
char *sp;
251249

252-
for (sp = s, bp = buf; *sp && bp < &buf[n]; sp++) {
250+
for (sp = s; *sp ; sp++) {
253251
if (sp == s || sp[-1] == '\n')
254-
*bp++ = '\t';
255-
if (bp < &buf[n])
256-
*bp++ = *sp;
252+
putchar('\t');
253+
putchar(*sp);
257254
}
258-
if (bp >= &buf[n])
259-
return NULL;
260-
*bp = '\0';
261-
return buf;
255+
return sp - s;
262256
}

legacy_v1.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
#include <unistd.h>
88
#include <fcntl.h>
99
#include "apperr.h"
10-
#include "applim.h"
1110
#include "siglock.h"
1211
#include "legacy_v1.h"
12+
#define PATHSZ 1024
13+
#define NLINE 32767
14+
#define LINESZ 1024
15+
#define NCARD 32767
16+
#define NFIELD 8
17+
#define KEYSZ 8
18+
#define VALSZ 1024
1319
#define MOD "MOD"
1420
#define QUES "Q"
1521
#define ANSW "A"
@@ -18,6 +24,16 @@
1824
#define TIMEFMT "%Y-%m-%d %H:%M:%S"
1925
#define TIMEFMT_P "%d-%d-%d %d:%d:%d %c%2d%2d"
2026

27+
struct field_v1 {
28+
char key[KEYSZ];
29+
char val[VALSZ];
30+
};
31+
32+
struct card_v1 {
33+
int nfield, leadnewl, trainewl;
34+
struct field_v1 field[NFIELD];
35+
};
36+
2137
static char *getfield_v1(struct card_v1 *card, char *key);
2238
static int gettime_v1(struct card_v1 *card, char *key, time_t *tp);
2339
static int settime_v1(struct card_v1 *card, char *key, time_t t);

legacy_v1.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
#ifndef CARD_V1_H
22
#define CARD_V1_H
33

4-
#include "applim.h"
5-
6-
struct field_v1 {
7-
char key[KEYSZ];
8-
char val[VALSZ];
9-
};
10-
11-
struct card_v1 {
12-
int nfield, leadnewl, trainewl;
13-
struct field_v1 field[NFIELD];
14-
};
15-
164
int conv_1to2(char *fname);
175

186
#endif

parse.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int readcard(FILE *fp, struct card *card, int *nline, int maxnl)
6060
}
6161
if (line[0] == '%') {
6262
/* end of card */
63-
strncpy(card->sep, line, sizeof card->sep);
63+
strncpy(card->sep, line, LINESZ);
6464
break;
6565
} else if (line[0] == '\t' || line[0] == '\n') {
6666
/* successive value line */
@@ -117,6 +117,7 @@ int readcard(FILE *fp, struct card *card, int *nline, int maxnl)
117117
#undef INCNLINE
118118
#undef CHK_FIELD
119119
#undef CHK_VALSZ
120+
#undef BACK_LINENO
120121
}
121122

122123
int writecard(FILE *fp, struct card *card)

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.0
1+
2.0.1

0 commit comments

Comments
 (0)