Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
2 changes: 1 addition & 1 deletion include/00ways/cds.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define __00ways_cds_h

#include <stdint.h>
typedef struct _IO_FILE FILE;
#include <stdio.h>
typedef struct cdsmap cdsmap;

cdsmap* cdsMapFile(const char* filename);
Expand Down
28 changes: 25 additions & 3 deletions src/cds.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include <stdlib.h>
#include <string.h>
#include <stddef.h>


#if !defined(max) || !defined(min)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#endif

static const char magicNumber[2] = {
0xCD,
0x50
Expand Down Expand Up @@ -42,14 +49,18 @@ static int fwriteSized(FILE* stream, enum cdsSize size, uint64_t* data) {
return 1;
return 0;
case BYTE_8:
#ifdef _WIN32
printf("writing bytes: 8(%02llX)\n", d.u64);
#else
printf("writing bytes: 8(%02lX)\n", d.u64);
#endif
if (fwrite(&(d.u64), sizeof(uint64_t), 1, stream) < 1)
return 1;
return 0;
}
return 0;
}
static int freadSized(uint64_t* data, enum cdsSize size, FILE* stream) {
static size_t freadSized(uint64_t* data, enum cdsSize size, FILE* stream) {
return fread(data, size, 1, stream);
}
static int skipSized(FILE* stream, enum cdsSize size) {
Expand All @@ -61,8 +72,6 @@ static int skipSized(FILE* stream, enum cdsSize size) {
}
return 0;
}
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
struct cdsRecord {
char* filename;
uint64_t length;
Expand Down Expand Up @@ -125,8 +134,13 @@ cdsmap* cdsMapFile(const char* filename) {
map->recordCount = recordCount;
fclose(fptr);
printf("map %s:\n", map->filename);
#ifdef _WIN32
printf("Origin: %lld\n", map->origin);
printf("Records: %lld\n", map->recordCount);
#else
printf("Origin: %ld\n", map->origin);
printf("Records: %ld\n", map->recordCount);
#endif
return map;
}
cdsmap* cdsMapCreate(const char* filename, int recordcount) {
Expand Down Expand Up @@ -215,7 +229,11 @@ int cdsMapWriteFileHeader(cdsmap* map, FILE** pfptr) {
printf("Unable to write file\n");
return 1;
}
#ifdef _WIN32
printf("[write] recordCount: %lld\n", map->recordCount);
#else
printf("[write] recordCount: %ld\n", map->recordCount);
#endif
enum cdsSize size;
if (map->recordCount > UINT32_MAX) size = BYTE_8;
else if (map->recordCount > UINT16_MAX) size = BYTE_4;
Expand Down Expand Up @@ -253,7 +271,11 @@ char* cdsMapToString(cdsmap* map) {
char* buffer = calloc(map->recordCount * (1 + 16 + 1 + 8 + 3 + 8), sizeof(char));
uint64_t offset = 0;
while (cursor != NULL) {
#ifdef _WIN32
sprintf(buffer, "%s\n%s %I64d + %I64d", buffer, cursor->filename, offset, cursor->length);
#else
sprintf(buffer, "%s\n%s %ld + %ld", buffer, cursor->filename, offset, cursor->length);
#endif
offset += cursor->length;
cursor = cursor->next;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,60 @@
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
/* This code is public domain -- Will Hartung 4/9/09 */
size_t getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;

if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;

c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}

*p++ = '\0';
*lineptr = bufptr;
*n = size;

return p - bufptr - 1;
}
#endif
static enum {
STATE_NULL = 0,
STATE_READ = 1,
Expand Down
Loading