File: files.cpp

package info (click to toggle)
openssn 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,340 kB
  • sloc: cpp: 10,973; makefile: 80
file content (87 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "files.h"

static char *data_dir = NULL;

// Find a file's full path based on the name of a file
char *Find_Data_File(char *filename)
{
   char *location;
   FILE *my_file;

   location = (char *) calloc( strlen(filename) + 64, sizeof(char) );
   if (! location)
      return filename;

   // maybe we already know the directory and don't have to search
   if (data_dir) 
   {
      sprintf(location, "%s/%s", data_dir, filename);
      return location;
   }

   sprintf(location, "%s/%s", DATADIR, filename);
   my_file = fopen(location, "r");
   if (my_file)
   {
     fclose(my_file);
     data_dir = DATADIR;
     return location;
   }

   /*
   This is the old way of guessing one location at a time. Now we
   can use the DATADIR definition to make sure we check the correct
   place.
   
   sprintf(location, "/usr/local/share/games/openssn/%s", filename);
   my_file = fopen(location, "r");
   if (my_file)
   {
       fclose(my_file);
       data_dir = "/usr/local/share/games/openssn";
       return location;
   }

   sprintf(location, "/usr/share/games/openssn/%s", filename);
   my_file = fopen(location, "r");
   if (my_file)
   {
      fclose(my_file);
      data_dir = "/usr/share/games/openssn";
      return location;
   }
   */

   // last chance, try OPTDIR
   sprintf(location, "%s/%s", OPTDIR, filename);
   my_file = fopen(location, "r");
   if (my_file)
   {
      fclose(my_file);
      data_dir = OPTDIR;
      return location;
   }
   data_dir = ".";  
   return filename;
}


SDL_Surface *Load_Image(char *filename)
{
   char *file_name;
   SDL_Surface *my_image = NULL;

   file_name = Find_Data_File(filename);
   if (file_name)
      my_image = IMG_Load(file_name);
   if ( (file_name) && (file_name != filename) )
       free(file_name);
   return my_image;

}