Subversion Repositories Games.Chess Giants

Rev

Rev 18 | Rev 116 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 81
Line 1205... Line 1205...
1205
   obj_hashbucket_t *hash;
1205
   obj_hashbucket_t *hash;
1206
   obj_face_t *f;
1206
   obj_face_t *f;
1207
   vertex_t *vertices; // mallocated
1207
   vertex_t *vertices; // mallocated
1208
   unsigned long *indices; // mallocated
1208
   unsigned long *indices; // mallocated
1209
   vertex_t *current_vertex;
1209
   vertex_t *current_vertex;
1210
   struct _stat fileinfo;
1210
   unsigned long file_size;
1211
   char *filedata; // mallocated
1211
   char *filedata; // mallocated
1212
   char *fileptr;
1212
   char *fileptr;
1213
   int vertex_index;
1213
   int vertex_index;
1214
   int array_index;
1214
   int array_index;
1215
   void *ptr_to;
1215
   void *ptr_to;
1216
   FILE *fp;
1216
   FILE *fp;
1217
 
1217
 
1218
   // open the mesh file and read it as a whole
1218
   // open the mesh file and read it as a whole
1219
   _wstat (objfile_pathname, &fileinfo);
-
 
1220
   _wfopen_s (&fp, objfile_pathname, L"rb");
1219
   _wfopen_s (&fp, objfile_pathname, L"rb");
1221
   if (fp == NULL)
1220
   if (fp == NULL)
1222
      return (false); // bomb out on error
1221
      return (false); // bomb out on error
-
 
1222
   fseek (fp, 0, SEEK_END); // seek at end of file...
-
 
1223
   file_size = ftell (fp); // ...read file length...
-
 
1224
   fseek (fp, 0, SEEK_SET); // and rewind
1223
   filedata = (char *) SAFE_malloc (fileinfo.st_size, sizeof (char), false); // mallocate space for data
1225
   filedata = (char *) SAFE_malloc (file_size, sizeof (char), false); // mallocate space for data
1224
   fread (filedata, fileinfo.st_size, 1, fp); // read file as a whole
1226
   fread (filedata, file_size, 1, fp); // read file as a whole
1225
   fclose (fp); // file is read, close it
1227
   fclose (fp); // file is read, close it
1226
 
1228
 
1227
   // allocate space for an arbitrary amount of vertices, texture coordinates, normals and faces
1229
   // allocate space for an arbitrary amount of vertices, texture coordinates, normals and faces
1228
   memset (&obj, 0, sizeof (obj));
1230
   memset (&obj, 0, sizeof (obj));
1229
   obj.v_maxcount = 10000; obj.vs = (vector_t *) SAFE_malloc (obj.v_maxcount, sizeof (vector_t), false);
1231
   obj.v_maxcount = 10000; obj.vs = (vector_t *) SAFE_malloc (obj.v_maxcount, sizeof (vector_t), false);
Line 1815... Line 1817...
1815
static unsigned long HashFile (const wchar_t *file_pathname)
1817
static unsigned long HashFile (const wchar_t *file_pathname)
1816
{
1818
{
1817
   // super fast file content pseudo-hash function
1819
   // super fast file content pseudo-hash function
1818
 
1820
 
1819
   unsigned short value;
1821
   unsigned short value;
1820
   struct _stat fileinfo;
1822
   unsigned long file_size;
1821
   FILE *fp;
1823
   FILE *fp;
1822
 
-
 
1823
   // first, get info about the file
-
 
1824
   if (_wstat (file_pathname, &fileinfo) != 0)
-
 
1825
      return ((unsigned long) time (NULL)); // if file can't be open, return a random number
-
 
1826
 
1824
 
1827
   // open the file
1825
   // open the file
1828
   _wfopen_s (&fp, file_pathname, L"rb");
1826
   _wfopen_s (&fp, file_pathname, L"rb");
1829
   if (fp == NULL)
1827
   if (fp == NULL)
1830
      return ((unsigned long) time (NULL)); // if file can't be open, return a random number
1828
      return ((unsigned long) time (NULL)); // if file can't be open, return a random number
-
 
1829
 
-
 
1830
   // seek at end of file, read file size, and rewind
-
 
1831
   fseek (fp, 0, SEEK_END);
-
 
1832
   file_size = ftell (fp);
-
 
1833
   fseek (fp, 0, SEEK_SET);
1831
 
1834
 
1832
   // seek at 2/3 of file size (if file is small enough, return only its content)
1835
   // seek at 2/3 of file size (if file is small enough, return only its content)
1833
   if (fileinfo.st_size >= 4)
1836
   if (file_size >= 4)
1834
   {
1837
   {
1835
      fseek (fp, fileinfo.st_size * 2 / 3, SEEK_SET); // seek at 2/3 of file
1838
      fseek (fp, file_size * 2 / 3, SEEK_SET); // seek at 2/3 of file
1836
      fread (&value, 2, 1, fp); // and read a word here
1839
      fread (&value, 2, 1, fp); // and read a word here
1837
   }
1840
   }
1838
   else if (fileinfo.st_size >= 2)
1841
   else if (file_size >= 2)
1839
      fread (&value, 2, 1, fp);
1842
      fread (&value, 2, 1, fp);
1840
   else if (fileinfo.st_size == 1)
1843
   else if (file_size == 1)
1841
      value = fgetc (fp);
1844
      value = fgetc (fp);
1842
   else
1845
   else
1843
      value = 0;
1846
      value = 0;
1844
 
1847
 
1845
   // finished, close the file
1848
   // finished, close the file
1846
   fclose (fp);
1849
   fclose (fp);
1847
 
1850
 
1848
   // and return a hash composed of the 16 lower bits of the file size and the value we read
1851
   // and return a hash composed of the 16 lower bits of the file size and the value we read
1849
   return ((unsigned long) (fileinfo.st_size << 16) | (unsigned long) value);
1852
   return ((unsigned long) (file_size << 16) | (unsigned long) value);
1850
}
1853
}
1851
 
1854
 
1852
 
1855
 
1853
static void ResolveWildcard (wchar_t *file_pathname, wchar_t *extensions_separated_by_bars)
1856
static void ResolveWildcard (wchar_t *file_pathname, wchar_t *extensions_separated_by_bars)
1854
{
1857
{
1855
   // this function resolves a pathname ending with .* by testing with various possible
1858
   // this function resolves a pathname ending with .* by testing with various possible
1856
   // file extensions until one of the files formed that way is found to exist.
1859
   // file extensions until one of the files formed that way is found to exist.
1857
 
1860
 
1858
   static wchar_t extension_list[256]; // needs to be modifiable for strtok()
1861
   static wchar_t extension_list[256]; // needs to be modifiable for strtok()
1859
   wchar_t *current_extension;
1862
   wchar_t *current_extension;
1860
   wchar_t *wcstok_context;
1863
   wchar_t *wcstok_context;
1861
   struct _stat fileinfo;
-
 
1862
   int length;
1864
   int length;
1863
 
1865
 
1864
   wcscpy_s (extension_list, WCHAR_SIZEOF (extension_list), extensions_separated_by_bars);
1866
   wcscpy_s (extension_list, WCHAR_SIZEOF (extension_list), extensions_separated_by_bars);
1865
   length = wcslen (file_pathname); // get pathname length
1867
   length = wcslen (file_pathname); // get pathname length
1866
 
1868
 
Line 1872... Line 1874...
1872
   current_extension = wcstok_s (extension_list, L"|", &wcstok_context);
1874
   current_extension = wcstok_s (extension_list, L"|", &wcstok_context);
1873
   while (current_extension != NULL)
1875
   while (current_extension != NULL)
1874
   {
1876
   {
1875
      if (*current_extension == L'.')
1877
      if (*current_extension == L'.')
1876
         current_extension++; // if current extension starts with a dot, skip it
1878
         current_extension++; // if current extension starts with a dot, skip it
-
 
1879
 
1877
      wcscpy_s (&file_pathname[length - 1], wcslen (current_extension) + 1, current_extension);
1880
      wcscpy_s (&file_pathname[length - 1], wcslen (current_extension) + 1, current_extension);
1878
      if (_wstat (file_pathname, &fileinfo) == 0)
1881
      if (_waccess (file_pathname, 0) == 0)
1879
         return; // found a file with this extension
1882
         return; // found a file with this extension
1880
      current_extension = wcstok_s (NULL, L"|", &wcstok_context);
1883
      current_extension = wcstok_s (NULL, L"|", &wcstok_context);
1881
   }
1884
   }
1882
 
1885
 
1883
   wcscpy_s (&file_pathname[length - 1], 2, L"*");
1886
   wcscpy_s (&file_pathname[length - 1], 2, L"*");