V
Victor Nazarov
I've tried to implement some garbage collection library for see. Here is
an example of it's usage. Do you think that it is usefull at all and
deserve development?
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "virlib.h"
void fclose_p (void *p);
void print (fp);
char *readline (fp);
int
main (int argc, char **argv)
{
FILE *fp;
begin_func ();
fp = fopen ("style.txt", "r");
add_destructor (fp, fclose_p);
print (fp);
end_func ();
return 0;
}
void
fclose_p (void *p)
{
fclose (p);
}
void
print (fp)
{
char *s;
int n;
begin_func ();
n = 0;
while ((s = readline (fp)) != NULL)
fprintf ("%4d%s", n++, s);
end_func ();
}
char *
readline (fp)
{
char *s, *t, *p;
int alen, llen;
begin_func ();
alen = 10;
s = malloc (alen);
if (s == NULL)
fail ("allocating buffer to read");
add_destructor (s, free);
p = s;
for (; {
t = fgets (p, alen, fp);
if (t == NULL) {
end_func ();
return NULL;
}
llen = strlen (p);
if (p[llen - 1] == '\n') {
mark_result (s);
end_func ();
return s;
}
llen = p - s;
t = realloc (s, alen * 2);
if (t == NULL)
fail ("allocating buffer to read");
mark_global (s); /* No need to destruct s */
s = t;
add_destructor (s, free);
p = s + llen;
}
}
an example of it's usage. Do you think that it is usefull at all and
deserve development?
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "virlib.h"
void fclose_p (void *p);
void print (fp);
char *readline (fp);
int
main (int argc, char **argv)
{
FILE *fp;
begin_func ();
fp = fopen ("style.txt", "r");
add_destructor (fp, fclose_p);
print (fp);
end_func ();
return 0;
}
void
fclose_p (void *p)
{
fclose (p);
}
void
print (fp)
{
char *s;
int n;
begin_func ();
n = 0;
while ((s = readline (fp)) != NULL)
fprintf ("%4d%s", n++, s);
end_func ();
}
char *
readline (fp)
{
char *s, *t, *p;
int alen, llen;
begin_func ();
alen = 10;
s = malloc (alen);
if (s == NULL)
fail ("allocating buffer to read");
add_destructor (s, free);
p = s;
for (; {
t = fgets (p, alen, fp);
if (t == NULL) {
end_func ();
return NULL;
}
llen = strlen (p);
if (p[llen - 1] == '\n') {
mark_result (s);
end_func ();
return s;
}
llen = p - s;
t = realloc (s, alen * 2);
if (t == NULL)
fail ("allocating buffer to read");
mark_global (s); /* No need to destruct s */
s = t;
add_destructor (s, free);
p = s + llen;
}
}