Advertisement
aaaaaa123456789

Quick and dirty sample <stdio.h> implementation

Apr 18th, 2016
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    This code is hereby released to the public domain.
  3.    ~aaaaaa123456789, 2016-04-18
  4. */
  5.  
  6. // NOTE: this is a quick and dirty, untested, sample implementation! Do not use without testing and fixing bugs! It may not even compile!
  7.  
  8. typedef struct {
  9.   char * init_ptr;
  10.   unsigned pos;
  11.   unsigned len;
  12. } FILE;
  13.  
  14. // assuming that <stdlib.h> and <string.h> (or suitable equivalents) are included
  15.  
  16. FILE * fopen (const char * filename, const char * mode) {
  17.   // accepts filenames in the form 0x12345678:0xabcd where they are pointer and length
  18.   // doesn't give a shit about the mode
  19.   const char * sep = strchr(filename, ':');
  20.   if (!sep) return NULL;
  21.   if (sep == filename) return NULL;
  22.   unsigned long ptr, len;
  23.   char * p = malloc(sep - filename + 1);
  24.   strncpy(p, filename, sep - filename);
  25.   p[sep - filename] = 0;
  26.   char * perr;
  27.   ptr = strtoul(p, &perr, 0);
  28.   if (*perr) {
  29.     free(p);
  30.     return NULL;
  31.   }
  32.   free(p);
  33.   if (!*(++ sep)) return NULL;
  34.   len = strtoul(sep, &perr, 0);
  35.   if (*perr) return NULL;
  36.   FILE * result = malloc(sizeof(FILE));
  37.   *result = (FILE) {.init_ptr = (char *) ptr, .pos = 0, .len = len};
  38.   return result;
  39. }
  40.  
  41. size_t fread (void * buffer, size_t element_size, size_t element_count, FILE * fp) {
  42.   unsigned read_count;
  43.   if ((element_size * element_count) > (fp -> len - fp -> pos))
  44.     read_count = (fp -> len - fp -> pos) / element_size;
  45.   else
  46.     read_count = element_count;
  47.   memcpy(buffer, fp -> init_ptr + pos, read_count * element_size);
  48.   return read_count;
  49. }
  50.  
  51. size_t fwrite (const void * buffer, size_t element_size, size_t element_count, FILE * fp) {
  52.   unsigned write_count;
  53.   if ((element_size * element_count) > (fp -> len - fp -> pos))
  54.     write_count = (fp -> len - fp -> pos) / element_size;
  55.   else
  56.     write_count = element_count;
  57.   memcpy(fp -> init_ptr + pos, buffer, write_count * element_size);
  58.   return write_count;
  59. }
  60.  
  61. int feof (FILE * fp) {
  62.   return fp -> pos >= fp -> len;
  63. }
  64.  
  65. int ferror (FILE * fp) {
  66.   return 0; // no errors
  67. }
  68.  
  69. void clearerr (FILE * fp) {} // does nothing
  70.  
  71. void rewind (FILE * fp) {
  72.   fp -> pos = 0;
  73. }
  74.  
  75. int fseek (FILE * fp, long offset, int whence /* yes, this is the real name of the parameter */) {
  76.   switch (whence) {
  77.     case 1: case 2:
  78.       offset += (whence == 1) ? fp -> pos : fp -> len;
  79.       // fallthrough
  80.     case 0:
  81.       if (offset < 0) return -1;
  82.       if (offset > fp -> len) return -1;
  83.       fp -> pos = offset;
  84.       return 0;
  85.     default:
  86.       return -1;
  87.   }
  88. }
  89.  
  90. long ftell (FILE * fp) {
  91.   return fp -> pos;
  92. }
  93.  
  94. typedef unsigned fpos_t;
  95.  
  96. int fgetpos (FILE * fp, fpos_t * pos) {
  97.   *pos = fp -> pos;
  98.   return 0;
  99. }
  100.  
  101. int fsetpos (FILE * fp, fpos_t * pos) {
  102.   return fseek(fp, *pos, 0);
  103. }
Advertisement
RAW Paste Data Copied
Advertisement