#include "kclib1.h"
#include <stdio.h>
#include <string.h>
int KSizPtr_addFile(KSizPtr *w, const char *path, const char *mod, int flg)
{
KSizPtr sp, *v = &sp;
int i;
FILE *fp = fopen(path, mod);
if (fp == 0) {
if (path == 0)
path = "(NULL)";
if ((flg & 1) != 0)
kerrorExit("KSizPtr_readFile: fopen error: '%s'", path);
return -1;
}
if ((flg & 4) != 0)
v = w;
KSizPtr_init(v, 256 * 1024);
for (;;) {
if (v->s >= v->s1)
KSizPtr_expand2(v);
i = fread(v->p + v->s, 1, v->s1 - v->s, fp);
if (i == 0) break;
v->s += i;
}
fclose(fp);
i = v->s;
if ((flg & 2) != 0) {
static char z16[16];
KSizPtr_addBytes(v, 16, z16);
}
if (w != v) {
KSizPtr_addBytes(w, v->s, v->p);
KSizPtr_free(v);
}
return i;
}#include <stdio.h>
#include "kclib1.h"
int var[256]; // 変数.
unsigned char *txt; // ソースコード.
int pc = 0; // プログラムカウンタ.
int getNumber(unsigned char c)
{
if ('0' <= c && c <= '9')
return c - '0'; // 1桁の定数.
return var[c]; // 1文字の変数名.
}
int main(int argc, const char **argv)
{
int i, pc0;
KAutoreleasePool_open();
if (argc < 2) // 引数が少ないのでエラー表示して終了.
kerrorExit("usage>%s program-file\n", argv[0]);
txt = kreadFileA(argv[1], "rt", NULL, 1 + 2);
for (;;) {
pc0 = pc;
if (txt[pc] == 0) // ファイル終端.
return 0;
if (txt[pc] == '\n' || txt[pc] == ' ' || txt[pc] == '\t' || txt[pc] == ';') { // 空行など.
pc++;
continue;
}
if (txt[pc + 1] == '=') { // 2文字目が"=".
i = txt[pc];
if (txt[pc + 3] == ';') { // 単純代入.
var[i] = getNumber(txt[pc + 2]);
} else if (txt[pc + 3] == '+' && txt[pc + 5] == ';') { // 加算.
var[i] = getNumber(txt[pc + 2]) + getNumber(txt[pc + 4]);
} else if (txt[pc + 3] == '-' && txt[pc + 5] == ';') { // 減算.
var[i] = getNumber(txt[pc + 2]) - getNumber(txt[pc + 4]);
} else
goto err;
} else if (txt[pc] == 'p' && txt[pc + 1] == 'r' && txt[pc + 5] == ' ' && txt[pc + 7] == ';') { // 最初の2文字しか調べてない(手抜き).
printf("%d\n", var[txt[pc + 6]]);
} else
goto err;
while (txt[pc] != ';')
pc++;
}
err:
kerrorExit("syntax error : %.10s\n", &txt[pc0]);
return 1; // dummy.
}#include "kclib1.h"
#include <string.h>
void *kreadFileA(const char *path, const char *mod, int *psiz, int flg)
{
KSizPtr sp;
int i = KSizPtr_addFile(&sp, path, mod, flg | 4), j;
void *p;
if (i < 0) return 0;
j = i;
if ((flg & 2) != 0)
j += 16;
p = KAutoreleasePool_alloc(0, j);
if (j > 0)
memcpy(p, sp.p, j);
KSizPtr_free(&sp);
if (psiz != 0)
*psiz = i;
return p;
}#include "kclib1.h"
#include <stdio.h>
int main(int argc, const char **argv)
{
KAutoreleasePool *ap = KAutoreleasePool_open();
const char *s = kreadFileA(argv[1], "rb", NULL, 1 + 2);
int i;
for (i = 1; ; i++) {
const char *t = ksgetsA(&s, 0);
if (*t == '\0') break;
printf("%08d: %s", i, t);
}
KAutoreleasePool_close(ap);
return 0;
}#include "kclib1.h"
#include <string.h>
void *ksgetsA(void *ps, int *psiz)
{
const char *s = *((const char **) ps);
char *t;
int i;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == '\n') {
i++;
break;
}
}
t = KAutoreleasePool_alloc(0, i + 1);
if (i > 0)
memcpy(t, s, i);
t[i] = '\0';
*((const char **) ps) = s + i;
if (psiz != 0)
*psiz = i;
return t;
}#include <string.h>
char *kcutCrLfM(int s, char *p)
{
if (s < 0) s = strlen(p);
if (s > 0 && p[s - 1] == '\n')
p[--s] = '\0';
if (s > 0 && p[s - 1] == '\r')
p[--s] = '\0';
return p;
}#include "kclib1.h"
#include <string.h>
void *kstrncpyA(int s, const void *p)
{
void *q;
if (s < 0) s = strlen(p);
q = KAutoreleasePool_alloc(0, s + 1);
if (s > 0)
memcpy(q, p, s);
((char *) q)[s] = '\0';
return q;
}| コメント | お名前 | NameLink | |