summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--legacy/eet/AUTHORS2
-rw-r--r--legacy/eet/src/lib/Eet.h3
-rw-r--r--legacy/eet/src/lib/eet_lib.c116
3 files changed, 93 insertions, 28 deletions
diff --git a/legacy/eet/AUTHORS b/legacy/eet/AUTHORS
index 7bf31c1234..54496f5a12 100644
--- a/legacy/eet/AUTHORS
+++ b/legacy/eet/AUTHORS
@@ -1,2 +1,2 @@
1The Rasterman (Carsten Haitzler) <raster@rasterman.com> 1The Rasterman (Carsten Haitzler) <raster@rasterman.com>
2 2David Goodlad <dgoodlad@gmail.com>
diff --git a/legacy/eet/src/lib/Eet.h b/legacy/eet/src/lib/Eet.h
index bfce73e375..88761a29eb 100644
--- a/legacy/eet/src/lib/Eet.h
+++ b/legacy/eet/src/lib/Eet.h
@@ -33,7 +33,8 @@ extern "C" {
33 enum _Eet_File_Mode 33 enum _Eet_File_Mode
34 { 34 {
35 EET_FILE_MODE_READ, 35 EET_FILE_MODE_READ,
36 EET_FILE_MODE_WRITE 36 EET_FILE_MODE_WRITE,
37 EET_FILE_MODE_RW
37 }; 38 };
38 39
39 typedef enum _Eet_File_Mode Eet_File_Mode; 40 typedef enum _Eet_File_Mode Eet_File_Mode;
diff --git a/legacy/eet/src/lib/eet_lib.c b/legacy/eet/src/lib/eet_lib.c
index 5e43527487..a078a1e1ad 100644
--- a/legacy/eet/src/lib/eet_lib.c
+++ b/legacy/eet/src/lib/eet_lib.c
@@ -217,7 +217,7 @@ eet_flush(Eet_File *ef)
217 return; 217 return;
218 if (!ef->header) return; 218 if (!ef->header) return;
219 if (!ef->header->directory) return; 219 if (!ef->header->directory) return;
220 if (ef->mode != EET_FILE_MODE_WRITE) return; 220 if ((ef->mode != EET_FILE_MODE_WRITE) && (ef->mode != EET_FILE_MODE_RW)) return;
221 if (!ef->writes_pending) return; 221 if (!ef->writes_pending) return;
222 222
223 /* calculate total size in bytes of directory block */ 223 /* calculate total size in bytes of directory block */
@@ -352,7 +352,7 @@ eet_open(char *file, Eet_File_Mode mode)
352 ef->mode = mode; 352 ef->mode = mode;
353 353
354 /* try open the file based on mode */ 354 /* try open the file based on mode */
355 if (ef->mode == EET_FILE_MODE_READ) 355 if ((ef->mode == EET_FILE_MODE_READ) || (ef->mode == EET_FILE_MODE_RW))
356 ef->fp = fopen(ef->path, "r"); 356 ef->fp = fopen(ef->path, "r");
357 else if (ef->mode == EET_FILE_MODE_WRITE) 357 else if (ef->mode == EET_FILE_MODE_WRITE)
358 { 358 {
@@ -373,8 +373,8 @@ eet_open(char *file, Eet_File_Mode mode)
373 return NULL; 373 return NULL;
374 } 374 }
375 375
376 /* if we opened for read */ 376 /* if we opened for read or read-write */
377 if (mode == EET_FILE_MODE_READ) 377 if ((mode == EET_FILE_MODE_READ) || (mode == EET_FILE_MODE_RW))
378 { 378 {
379 unsigned char buf[12]; 379 unsigned char buf[12];
380 unsigned char *dyn_buf, *p; 380 unsigned char *dyn_buf, *p;
@@ -472,6 +472,7 @@ eet_open(char *file, Eet_File_Mode mode)
472 int hash; 472 int hash;
473 Eet_File_Node *node; 473 Eet_File_Node *node;
474 int node_size; 474 int node_size;
475 void *data = NULL;
475 476
476 /* out directory block is inconsistent - we have oveerun our */ 477 /* out directory block is inconsistent - we have oveerun our */
477 /* dynamic block buffer before we finished scanning dir entries */ 478 /* dynamic block buffer before we finished scanning dir entries */
@@ -551,8 +552,37 @@ eet_open(char *file, Eet_File_Mode mode)
551 ef->header->directory->hash[hash].node[node_size].compression = flags; 552 ef->header->directory->hash[hash].node[node_size].compression = flags;
552 ef->header->directory->hash[hash].node[node_size].size = size; 553 ef->header->directory->hash[hash].node[node_size].size = size;
553 ef->header->directory->hash[hash].node[node_size].data_size = data_size; 554 ef->header->directory->hash[hash].node[node_size].data_size = data_size;
554 /* currently we have no data loaded */ 555
555 ef->header->directory->hash[hash].node[node_size].data = NULL; 556 /* read-only mode, so currently we have no data loaded */
557 if (mode == EET_FILE_MODE_READ)
558 {
559 ef->header->directory->hash[hash].node[node_size].data = NULL;
560 }
561 /* read-write mode - read everything into ram */
562 else
563 {
564 data = malloc(size);
565 if (data)
566 {
567 if (fseek(ef->fp, ef->header->directory->hash[hash].node[node_size].offset, SEEK_SET) < 0)
568 {
569 free(data);
570 data = NULL;
571 /* XXX die gracefully somehow */
572 break;
573 }
574 if (fread(data, size, 1, ef->fp) != 1)
575 {
576 free(data);
577 data = NULL;
578 /* XXX die gracefully somehow */
579 break;
580 }
581 }
582
583 ef->header->directory->hash[hash].node[node_size].data = data;
584 }
585
556 /* increment number of nodes */ 586 /* increment number of nodes */
557 ef->header->directory->hash[hash].size++; 587 ef->header->directory->hash[hash].size++;
558 /* advance */ 588 /* advance */
@@ -561,10 +591,19 @@ eet_open(char *file, Eet_File_Mode mode)
561 /* done - free dynamic buffer */ 591 /* done - free dynamic buffer */
562 free(dyn_buf); 592 free(dyn_buf);
563 } 593 }
594
595 /* we need to delete the original file in read-write mode and re-open for writing */
596 if (ef->mode == EET_FILE_MODE_RW)
597 {
598 fclose(ef->fp);
599 unlink(ef->real_path);
600 ef->fp = fopen(ef->path, "w");
601 }
602
564 /* add to cache */ 603 /* add to cache */
565 if (ef->mode == EET_FILE_MODE_READ) 604 if (ef->mode == EET_FILE_MODE_READ)
566 eet_cache_add(ef, &eet_readers, &eet_readers_num); 605 eet_cache_add(ef, &eet_readers, &eet_readers_num);
567 else if (ef->mode == EET_FILE_MODE_WRITE) 606 else if ((ef->mode == EET_FILE_MODE_WRITE) || (ef->mode == EET_FILE_MODE_RW))
568 eet_cache_add(ef, &eet_writers, &eet_writers_num); 607 eet_cache_add(ef, &eet_writers, &eet_writers_num);
569 return ef; 608 return ef;
570} 609}
@@ -766,11 +805,14 @@ eet_write(Eet_File *ef, char *name, void *data, int size, int compress)
766 Eet_File_Node *node; 805 Eet_File_Node *node;
767 char *name2; 806 char *name2;
768 void *data2; 807 void *data2;
808 int exists_already = 0;
769 809
770 /* check to see its' an eet file pointer */ 810 /* check to see its' an eet file pointer */
771 if ((!ef) || (ef->magic != EET_MAGIC_FILE) 811 if ((!ef) || (ef->magic != EET_MAGIC_FILE)
772 || (!name) || (!data) || (size <= 0) || 812 || (!name) || (!data) || (size <= 0) ||
773 (ef->mode != EET_FILE_MODE_WRITE)) 813 ((ef->mode != EET_FILE_MODE_WRITE) &&
814 (ef->mode != EET_FILE_MODE_RW)))
815
774 return 0; 816 return 0;
775 817
776 if (!ef->header) 818 if (!ef->header)
@@ -837,26 +879,48 @@ eet_write(Eet_File *ef, char *name, void *data, int size, int compress)
837 } 879 }
838 if (!compress) 880 if (!compress)
839 memcpy(data2, data, size); 881 memcpy(data2, data, size);
840 /* increase hash bucket size */ 882
841 node = realloc(ef->header->directory->hash[hash].node, 883 /* Does this node already exist? */
842 (node_size + 1) * sizeof(Eet_File_Node)); 884 if (ef->mode = EET_FILE_MODE_RW)
843 if (!node)
844 { 885 {
845 free(name2); 886 int i;
846 free(data2); 887 for (i = 0; i < node_size; i++)
847 return 0; 888 {
889 /* if it matches */
890 if (eet_string_match(ef->header->directory->hash[hash].node[i].name, name))
891 {
892 free(ef->header->directory->hash[hash].node[i].data);
893 ef->header->directory->hash[hash].node[i].compression = compress;
894 ef->header->directory->hash[hash].node[i].size = data_size;
895 ef->header->directory->hash[hash].node[i].data_size = size;
896 ef->header->directory->hash[hash].node[i].data = data2;
897 exists_already = 1;
898 }
899 }
848 } 900 }
849 /* resized node list set up */ 901 if (!exists_already)
850 ef->header->directory->hash[hash].node = node; 902 {
851 /* new node at end */ 903 /* increase hash bucket size */
852 ef->header->directory->hash[hash].node[node_size].name = name2; 904 node = realloc(ef->header->directory->hash[hash].node,
853 ef->header->directory->hash[hash].node[node_size].offset = 0; 905 (node_size + 1) * sizeof(Eet_File_Node));
854 ef->header->directory->hash[hash].node[node_size].compression = compress; 906 if (!node)
855 ef->header->directory->hash[hash].node[node_size].size = data_size; 907 {
856 ef->header->directory->hash[hash].node[node_size].data_size = size; 908 free(name2);
857 ef->header->directory->hash[hash].node[node_size].data = data2; 909 free(data2);
858 ef->header->directory->hash[hash].size++; 910 return 0;
859 911 }
912 /* resized node list set up */
913 ef->header->directory->hash[hash].node = node;
914 /* new node at end */
915 ef->header->directory->hash[hash].node[node_size].name = name2;
916 ef->header->directory->hash[hash].node[node_size].offset = 0;
917 ef->header->directory->hash[hash].node[node_size].compression = compress;
918 ef->header->directory->hash[hash].node[node_size].size = data_size;
919 ef->header->directory->hash[hash].node[node_size].data_size = size;
920 ef->header->directory->hash[hash].node[node_size].data = data2;
921 ef->header->directory->hash[hash].size++;
922 }
923
860 /* flags that writes are pending */ 924 /* flags that writes are pending */
861 ef->writes_pending = 1; 925 ef->writes_pending = 1;
862 /* update access time */ 926 /* update access time */