Skip to content

Instantly share code, notes, and snippets.

@mattn
Forked from cho45/c.c
Created February 4, 2009 00:09
Show Gist options
  • Save mattn/57840 to your computer and use it in GitHub Desktop.
Save mattn/57840 to your computer and use it in GitHub Desktop.
//#!gcc -g -O0 -Wall
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// class Entry {
typedef struct entry {
char* path;
char* title;
char* body;
struct stat* stat;
struct entry* next;
} Entry;
Entry* Entry_new (const char* path) {
Entry* self = malloc(sizeof(Entry));
// NOTE: need alloc more 1 byte.
// or you can use strdup().
//
//self->path = malloc(strlen(path) * sizeof(char));
self->path = malloc((strlen(path)+1) * sizeof(char));
strcpy(self->path, path);
self->stat = malloc(sizeof(struct stat));
stat(self->path, self->stat);
FILE* f = fopen(self->path, "r");
if (f != NULL) {
// 一括読みこみ
char* data = calloc(self->stat->st_size + 1, sizeof(char));
fread(data, sizeof(char), self->stat->st_size, f);
fclose(f);
char* body = strchr(data, 0x0a);
int title_length = (body == NULL) ? 0 : ((int)body - (int)data) / sizeof(char);
// data から 0x0a まで抜きだして title に
self->title = malloc(title_length + 1);
strncpy(self->title, data, title_length);
self->title[title_length] = 0;
// data の時点で null 末端なので strcpy
self->body = malloc(self->stat->st_size - title_length + 1);
strcpy(self->body, body);
free(data);
}
if (!self->title) self->title = calloc(1, sizeof(char));
if (!self->body) self->body = calloc(1, sizeof(char));
self->next = NULL;
return self;
}
void Entry_destroy (Entry* self) {
free(self->path);
free(self->title);
free(self->body);
free(self->stat);
free(self);
}
// } end of Entry class
// class Entries {
typedef Entry Entries;
Entry* Entries_collect_recursive (char* parent, Entry* current) {
DIR* dir;
// printf("downto %s\n", parent);
dir = opendir(parent);
if (dir == NULL) return NULL;
struct dirent* ent;
char* path;
while ((ent = readdir(dir)) != NULL) {
path = calloc(strlen(parent) + strlen(ent->d_name) + 2, sizeof(char));
sprintf(path, "%s/%s", parent, ent->d_name);
if (strcmp(ent->d_name, ".") == 0) continue;
if (strcmp(ent->d_name, "..") == 0) continue;
struct stat s;
stat(path, &s);
// printf("%s %s\n", path, S_ISDIR(s.st_mode) ? "(dir)" : "");
if (S_ISDIR(s.st_mode)) {
current = Entries_collect_recursive(path, current);
} else {
current->next = Entry_new(path);
current = current->next;
}
free(path);
}
closedir(dir);
return current;
}
Entries* Entries_new (char* root) {
Entries* self = malloc(sizeof(Entries));
Entries_collect_recursive(root, self);
return self;
}
void Entries_destroy (Entries* self) {
// free self and all Entry
Entry* entry = self->next;
Entry* temp;
while (entry) {
// printf("destroy: %s\n", entry->path);
temp = entry->next;
Entry_destroy(entry);
entry = temp;
}
free(self);
}
Entries* Entries_sort (Entries* self) {
return self;
}
// } end of Entries class
int main (int argc, char* argv[]) {
// Entry* entry = Entry_new("/home/cho45/blosxom-test-data/intro.txt");
// printf("%s\n", entry->path);
// printf("%s\n\n", entry->title);
// printf("%s\n", entry->body);
//
// Entry_destroy(entry);
Entries* entries = Entries_new("/home/cho45/blosxom-test-data");
Entry* entry = entries;
while ( (entry = entry->next) ) {
printf("%s\n", entry->path);
printf("%s\n", entry->title);
}
Entries_destroy(entries);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment