source: git/resource/feFopen.cc @ a59c630

spielwiese
Last change on this file since a59c630 was 46395ad, checked in by Hans Schoenemann <hannes@…>, 14 years ago
directory resources
  • Property mode set to 100644
File size: 3.1 KB
Line 
1
2/*****************************************************************
3 *
4 * File handling
5 *
6 *****************************************************************/
7
8FILE * feFopen(const char *path, const char *mode, char *where,
9               int useWerror, int path_only)
10{
11  char longpath[MAXPATHLEN];
12  if (path[0]=='~')
13  {
14    if (path[1] == DIR_SEP)
15    {
16      const char* home = getenv("HOME");
17#ifdef ix86_Win
18      if ((home==NULL)||(!access(home,X_OK)))
19        home = getenv("SINGHOME");
20#endif
21      if (home != NULL)
22      {
23        strcpy(longpath, home);
24        strcat(longpath, &(path[1]));
25        path = longpath;
26      }
27    }
28#if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
29    else
30    {
31      char* dir_sep;
32      struct passwd *pw_entry;
33      strcpy (longpath, path);
34      dir_sep = strchr(longpath, DIR_SEP);
35      *dir_sep = '\0';
36      pw_entry = getpwnam(&longpath[1]);
37      if (pw_entry != NULL)
38      {
39        strcpy(longpath, pw_entry->pw_dir);
40        dir_sep = strchr((char *)path, DIR_SEP);
41        strcat(longpath, dir_sep);
42        path = longpath;
43      }
44    }
45#endif
46  }
47  FILE * f=NULL;
48  if (! path_only)
49  {
50    struct stat statbuf;
51    if ((stat(path,&statbuf)==0)
52    && (S_ISREG(statbuf.st_mode)))
53      f = myfopen(path,mode);
54  }
55  if (where!=NULL) strcpy(where,path);
56  if ((*mode=='r') &&
57      (path[0]!=DIR_SEP) &&
58      ! (path[0] == '.' && path[1] == DIR_SEP) &&
59      (f==NULL))
60  {
61    char found = 0;
62    char* spath = feResource('s');
63    char *s;
64
65    if (where==NULL) s=(char *)omAlloc(250);
66    else             s=where;
67
68    if (spath!=NULL)
69    {
70      char *p,*q;
71      p = spath;
72      while( (q=strchr(p, fePathSep)) != NULL)
73      {
74        *q = '\0';
75        strcpy(s,p);
76        *q = fePathSep;
77        strcat(s, DIR_SEPP);
78        strcat(s, path);
79        if(!access(s, R_OK)) { found++; break; }
80        p = q+1;
81      }
82      if(!found)
83      {
84        strcpy(s,p);
85        strcat(s, DIR_SEPP);
86        strcat(s, path);
87      }
88      f=myfopen(s,mode);
89      if (f!=NULL)
90      {
91        if (where==NULL) omFree((ADDRESS)s);
92        return f;
93      }
94    }
95    else
96    {
97      if (where!=NULL) strcpy(s/*where*/,path);
98      f=myfopen(path,mode);
99    }
100    if (where==NULL) omFree((ADDRESS)s);
101  }
102  if ((f==NULL)&&(useWerror))
103    Werror("cannot open `%s`",path);
104  return f;
105}
106
107#ifdef ix86_Win
108// Make sure that mode contains binary option
109FILE* myfopen(const char *path, const char *mode)
110{
111  char mmode[4];
112  int i;
113  int done = 0;
114
115  for (i=0;;i++)
116  {
117    mmode[i] = mode[i];
118    if (mode[i] == '\0') break;
119    if (mode[i] == 'w') done = 1;
120    if (mode[i] == 'a') done = 1;
121    if (mode[i] == 'b') done = 1;
122  }
123
124  if (! done)
125  {
126    mmode[i] = 'b';
127    mmode[i+1] = '\0';
128  }
129  return fopen(path, mmode);
130}
131#endif
132// replace "\r\n" by " \n" and "\r" by "\n"
133
134size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
135{
136  size_t got = fread(ptr, size, nmemb, stream) * size;
137  size_t i;
138
139  for (i=0; i<got; i++)
140  {
141    if ( ((char*) ptr)[i] == '\r')
142    {
143      if (i+1 < got && ((char*) ptr)[i+1] == '\n')
144        ((char*) ptr)[i] = ' ';
145      else
146        ((char*) ptr)[i] = '\n';
147    }
148  }
149  return got;
150}
Note: See TracBrowser for help on using the repository browser.