comparison lib64gfx.c @ 567:b2b461829c61

Move utility function dmReadDataFile() to dmfile module instead.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 04 Jan 2013 16:48:15 +0200
parents cf39f51d4e74
children 5d158c4321bb
comparison
equal deleted inserted replaced
566:d400e32b62d9 567:b2b461829c61
819 819
820 return res; 820 return res;
821 } 821 }
822 822
823 823
824 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
825 {
826 FILE *f;
827 int res = DMERR_OK;
828 Uint8 *dataBuf = NULL;
829 size_t readSize, dataSize, dataRead, dataPos;
830
831 if (inFile != NULL)
832 f = inFile;
833 else
834 if (filename != NULL)
835 {
836 if ((f = fopen(filename, "rb")) == NULL)
837 {
838 dmError("Could not open '%s' for reading.\n", filename);
839 return DMERR_FOPEN;
840 }
841 }
842 else
843 {
844 dmError("NULL filename and stream pointers.\n");
845 return DMERR_NULLPTR;
846 }
847
848 // Allocate initial data buffer
849 readSize = dataSize = BUF_SIZE_INITIAL;
850 if ((dataBuf = dmMalloc(dataSize)) == NULL)
851 {
852 dmError("Error allocating memory for data, %d bytes.\n", dataSize);
853 res = DMERR_MALLOC;
854 goto error;
855 }
856
857 dataPos = 0;
858 dataRead = 0;
859
860 while (!feof(f) && !ferror(f))
861 {
862 size_t read = fread(dataBuf + dataPos, 1, readSize, f);
863 dataPos += read;
864 dataRead += read;
865
866 if (dataRead >= dataSize)
867 {
868 readSize = BUF_SIZE_GROW;
869 dataSize += BUF_SIZE_GROW;
870 if ((dataBuf = dmRealloc(dataBuf, dataSize)) == NULL)
871 {
872 dmError("Error reallocating memory for data, %d bytes.\n", dataSize);
873 res = DMERR_MALLOC;
874 goto error;
875 }
876 }
877 else
878 break;
879 }
880
881 *pbufSize = dataRead;
882 *pbuf = dataBuf;
883
884 error:
885 if (f != inFile)
886 fclose(f);
887
888 return res;
889 }
890
891
892 int dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len, 824 int dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len,
893 const size_t probeOffs, const size_t loadOffs, 825 const size_t probeOffs, const size_t loadOffs,
894 const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced) 826 const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced)
895 { 827 {
896 // Check for forced format 828 // Check for forced format