char last = 0;
while (*p != '\0') {
if (*p == '<') {
if (strncmp(p, "<br />", 6) == 0) { p += 6; putchar('\n'); continue; }
if (strncmp(p, "<strong>", 8) == 0) { p += 8; continue; }
if (strncmp(p, "</strong>", 9) == 0) { p += 9; continue; }
if (strncmp(p, "<div>", 5) == 0) { p += 5; continue; }
if (strncmp(p, "</div>", 6) == 0) { p += 6; continue; }
if (strncmp(p, "<h3>", 4) == 0) { p += 4; putchar('\n'); continue; }
if (strncmp(p, "</h3>", 5) == 0) { p += 5; putchar('\n'); continue; }
(似たような処理がまだたくさん続くが面倒なので省略)
}
if (*p == ' ' || *p == '\r' || *p == '\n' || *p == '\t') {
p++;
if (last != ' ')
putchar(last = ' ');
} else
putchar(last = *p++);
}int strncmpSkip(const char **p, const char *s)
{
int l = strlen(s);
if (strncmp(*p, s, l) == 0) {
*p += l;
return 1;
}
return 0;
}char last = 0;
while (*p != '\0') {
if (*p == '<') {
if (strncmpSkip(&p, "<br />")) { putchar('\n'); continue; }
if (strncmpSkip(&p, "<strong>")) continue;
if (strncmpSkip(&p, "</strong>")) continue;
if (strncmpSkip(&p, "<div>")) continue;
if (strncmpSkip(&p, "</div>")) continue;
if (strncmpSkip(&p, "<h3>")) { putchar('\n'); continue; }
if (strncmpSkip(&p, "</h3>")) { putchar('\n'); continue; }
(似たような処理がまだたくさん続くが面倒なので省略)
}
if (*p == ' ' || *p == '\r' || *p == '\n' || *p == '\t') {
p++;
if (last != ' ')
putchar(last = ' ');
} else
putchar(last = *p++);
}typedef strcut AutoReleaseNode_ {
void *p, *next;
} AutoReleaseNode;
typedef struct AutoReleasePool_ {
void *oldPool;
AutoReleaseNode *node;
} AutoReleasePool;
AutoReleasePool *AutoReleasePool_defalut = 0;
AutoReleasePool *AutoReleasePool_open()
{
AutoReleasePool *w = malloc(sizeof (AutoReleasePool));
w->oldPool = AutoReleasePool_defalut; // デフォルトの値をバックアップしておく.
w->node = 0;
return w;
}
void AutoReleasePool_add(AutoReleasePool *w, void *p)
{
AutoReleaseNode *n = malloc(sizeof (AutoReleaseNode));
if (w == 0) w = AutoReleasePool_defalut; // 0ならデフォルト指定.
n->p = p;
n->next = w->node;
w->node = n;
}
void AutoReleasePool_remove(AutoReleasePool *w, void *p)
{
AutoReleaseNode *n;
if (w == 0) w = AutoReleasePool_defalut; // 0ならデフォルト指定.
for (n = w->node; n != 0; n = n->next) {
if (n->p == p) {
n->p = 0;
return;
}
}
fprintf(stderr, "AutoReleasePool_remove: remove error: p=%x\n", (int) p);
exit(EXIT_FAILURE);
}
void AutoReleasePool_close(AutoReleasePool *w)
{
AutoReleaseNode *n, *nn;
if (w != AutoReleasePool_defalut) {
fprintf(stderr, "AutoReleasePool_close: nesting error\n");
exit(EXIT_FAILURE);
}
for (n = w->node; n != 0; n = nn) {
nn = n->next;
if (n->p != 0)
free(n->p);
free(n);
}
AutoReleasePool_defalut = w->oldPool;
free(w);
}
char *tmpPrintf(const char *f, ...)
{
char buf[1024 * 1024];
va_list arg;
int l;
char *s;
va_start(arg, f);
l = vsnprintf(buf, sizeof buf, f, arg);
if (l < 0 || (int) (sizeof b) <= l) {
fprintf(stderr, "tmpPrintf: buffer error\n");
exit(EXIT_FAILURE);
}
va_end(arg);
s = malloc(l + 1);
memcpy(s, buf, l + 1);
AutoReleasePool_add(0, s);
return s;
}for (i = 0; i < 100; i++) {
FILE *fp = fopen(tmpPrintf("file%04d.txt", i), "rt");
...
fclose(fp);
}for (i = 0; i < 100; i++) {
char tmp[16];
sprintf(tmp, "file%04d.txt", i);
FILE *fp = fopen(tmp, "rt");
...
fclose(fp);
}char *strstr2(const char *s, const char *t, const char *u)
{
char *p = strstr(s, t);
if (p != 0) {
p = strstr(p + strlen(t), u);
if (p != 0)
p += strlen(u);
}
return p;
}p = HTMLの先頭;
for (;;) {
p = strstr2(p, "<a href=", ">");
if (p == 0) break;
q = strstr(p, "</a>");
if (q == 0) break;
printf("%.*s\n", q - p, p);
}| コメント | お名前 | NameLink | |