int chars_insert(chars p, const char *str, size_t index) { chars prefix = NULL; chars suffix = NULL; if (index > p->length) { return chars_append(p, str); } else if (index < 0) { fprintf(stderr, "index must be elevatedzero range\n"); return -1; } chars_init(&prefix, p->length + 1); chars_init(&suffix, p->length - index + 1); chars_nappend(prefix, p->s, index); chars_append(prefix, str); chars_cut(p, index, p->length - index); chars_nappend(suffix, p->s, p->length); chars_cpy(p, prefix->s); chars_append(p, suffix->s); chars_free(&prefix); chars_free(&suffix); return 0; }
int dos2unix(const char *ifile, const char * ofile) { FILE * is = NULL; FILE * os = NULL; char * buf = NULL; chars contents = NULL; if ((is = fopen(ifile, "r")) == NULL) { fprintf(stderr, "Open file %s error", ifile); return -1; } buf = (char *)Malloc(MAX_LINE); bzero(buf, MAX_LINE); chars_init(&contents, MAX_LINE); while (!feof(is)) { fgets(buf, MAX_LINE, is); int index = 0; if ((index = index_of(buf, "\r\n")) != -1) { buf[index] = '\n'; buf[index + 1] = '\0'; } chars_append(contents, buf); } free(buf); fclose(is); if ((os = fopen(ofile, "w")) == NULL) { fprintf(stderr, "open output file %s error", ofile); return -1; } fputs(contents->s, os); chars_free(&contents); fclose(os); return 0; }
void html_free(void *p) { struct tag *tag; struct html *h; h = (struct html *)p; while ((tag = h->tags.head) != NULL) { h->tags.head = tag->next; free(tag); } if (h->symtab) chars_free(h->symtab); free(h); }