Subversion Repositories psp

[/] [trunk/] [neopop/] [System_PSP/] [psplib/] [fileio.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 jack
/** PSP helper library ***************************************/
2
/**                                                         **/
3
/**                        fileio.c                         **/
4
/**                                                         **/
5
/** This file contains file management routines             **/
6
/**                                                         **/
7
/** Copyright (C) Akop Karapetyan 2007                      **/
8
/**     You are not allowed to distribute this software     **/
9
/**     commercially. Please, notify me, if you make any    **/
10
/**     changes to this file.                               **/
11
/*************************************************************/
12
#include <pspkernel.h>
13
#include <psptypes.h>
14
#include <string.h>
15
#include <malloc.h>
16
#include <unistd.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
 
20
#include "fileio.h"
21
 
22
static int _pspFileIoCompareFilesByName(const void *s1, const void *s2);
23
static void _pspFileIoSortFileList(PspFileList *list);
24
 
25
// Returns a list of all files in a directory
26
// path: path containing the files
27
// filter: array of strings, each containing an extension. last element contains NULL ('\0')
28
 
29
PspFileList* pspFileIoGetFileList(const char *path, const char **filter)
30
{
31
  SceUID fd = sceIoDopen(path);
32
 
33
  if (fd < 0) return 0;
34
 
35
  SceIoDirent dir;
36
  memset(&dir, 0, sizeof(dir));
37
 
38
  PspFile *file = 0;
39
  PspFileList *list = (PspFileList*)malloc(sizeof(PspFileList));
40
  const char **pext;
41
  int loop;
42
 
43
  list->Count = 0;
44
  list->First = 0;
45
  list->Last = 0;
46
 
47
  while (sceIoDread(fd, &dir) > 0)
48
  {
49
    if (filter && !(dir.d_stat.st_attr & FIO_SO_IFDIR))
50
    {
51
      /* Loop through the list of allowed extensions and compare */
52
      for (pext = filter, loop = 1; *pext; pext++)
53
      {
54
        if (pspFileIoEndsWith(dir.d_name, *pext))
55
        {
56
          loop = 0;
57
          break;
58
        }
59
      }
60
 
61
      if (loop) continue;
62
    }
63
 
64
    // Create a new file entry
65
 
66
    file = (PspFile*)malloc(sizeof(PspFile));
67
    file->Name = strdup(dir.d_name);
68
    file->Next = 0;
69
    file->Attrs = (dir.d_stat.st_attr & FIO_SO_IFDIR) ? PSP_FILEIO_DIR : 0;
70
 
71
    list->Count++;
72
 
73
    // Update preceding element
74
 
75
    if (list->Last)
76
    {
77
      list->Last->Next = file;
78
      list->Last = file;
79
    }
80
    else
81
    {
82
      list->First = file;
83
      list->Last = file;
84
    }
85
  }
86
 
87
  sceIoDclose(fd);
88
 
89
  // Sort the files by name
90
 
91
  _pspFileIoSortFileList(list);
92
 
93
  return list;
94
}
95
 
96
static int _pspFileIoCompareFilesByName(const void *s1, const void *s2)
97
{
98
  PspFile *f1=*(PspFile**)s1, *f2=*(PspFile**)s2;
99
  if ((f1->Attrs&PSP_FILEIO_DIR)==(f2->Attrs&PSP_FILEIO_DIR))
100
    return strcasecmp(f1->Name, f2->Name);
101
  else if (f1->Attrs&PSP_FILEIO_DIR)
102
    return -1;
103
  else return 1;
104
}
105
 
106
static void _pspFileIoSortFileList(PspFileList *list)
107
{
108
  PspFile **files, *file;
109
  int i;
110
 
111
  if (list->Count < 1) return;
112
 
113
  /* Copy the file entries to an array */
114
  files = (PspFile**)malloc(sizeof(PspFile*) * list->Count);
115
  for (file = list->First, i = 0; file; file = file->Next, i++)
116
    files[i] = file;
117
 
118
  /* Sort the array */
119
  qsort((void*)files, list->Count, sizeof(PspFile*), _pspFileIoCompareFilesByName);
120
 
121
  /* Rearrange the file entries in the list */
122
  list->First = files[0];
123
  list->First->Next = NULL;
124
 
125
  for (i = 1; i < list->Count; i++)
126
    files[i - 1]->Next = files[i];
127
 
128
  list->Last = files[list->Count - 1];
129
  list->Last->Next = NULL;
130
 
131
  /* Free the temp. array */
132
  free(files);
133
}
134
 
135
void pspFileIoDestroyFileList(PspFileList* list)
136
{
137
  PspFile *file, *next;
138
 
139
  for (file = list->First; file; file = next)
140
  {
141
    next = file->Next;
142
    free(file->Name);
143
    free(file);
144
  }
145
 
146
  free(list);
147
}
148
 
149
void pspFileIoEnterDirectory(char **cur_path, char *dir)
150
{
151
  /* Same directory */
152
  if (strcmp(dir, ".") == 0)
153
    return;
154
 
155
  int len, pos;
156
  len = strlen(*cur_path);
157
  pos = len - 1;
158
 
159
  /* Ascend one level */
160
  if (strcmp(dir, "..") == 0)
161
  {
162
    for (; pos >= 0 && (*cur_path)[pos] == '/'; pos--);
163
    for (; pos >= 0 && (*cur_path)[pos] != '/'; pos--);
164
 
165
    if (pos >= 0)
166
      (*cur_path)[pos + 1] = '\0';
167
 
168
    return;
169
  }
170
 
171
  /* Descend one level */
172
  char *new_path = (char*)malloc(sizeof(char) * (strlen(dir) + len + 2)); // 2: '\0' + '/'
173
  sprintf(new_path, "%s%s/", *cur_path, dir);
174
 
175
  free(*cur_path);
176
  *cur_path = new_path;
177
}
178
 
179
char* pspFileIoGetParentDirectory(const char *path)
180
{
181
  char *pos = strrchr(path, '/');
182
 
183
  if (!pos) return NULL;
184
 
185
  char *parent = (char*)malloc(sizeof(char) * (pos - path + 2));
186
  strncpy(parent, path, pos - path + 1);
187
  parent[pos - path + 1] = '\0';
188
 
189
  return parent;
190
}
191
 
192
const char* pspFileIoGetFilename(const char *path)
193
{
194 106 jack
  char *pos;
195 145 jack
  if (!(pos = strrchr(path, '/')))
196
    return path;
197 9 jack
  return pos + 1;
198
}
199
 
200
int pspFileIoIsRootDirectory(const char *path)
201
{
202
  char *pos = strchr(path, '/');
203
  return !(pos && (*(pos + 1)));
204
}
205
 
206
const char* pspFileIoGetFileExtension(const char *path)
207
{
208
  const char *filename = strrchr(path, '/');
209
 
210
  filename = (filename) ? filename + 1 : path;
211
 
212
  const char *ext = strrchr(filename, '.');
213
 
214
  return (ext) ? ext + 1 : filename + strlen(filename);
215
}
216
 
217
int pspFileIoEndsWith(const char *filename, const char *ext)
218
{
219
  int fn_len, ext_len;
220
  const char *file_ext;
221
 
222
  fn_len = strlen(filename);
223
  ext_len = strlen(ext);
224
 
225
  /* Filename must be at least 2 chars longer (period + a char) */
226
  if (fn_len < ext_len + 2)
227
    return 0;
228
 
229
  file_ext = filename + (fn_len - ext_len);
230
  if (*(file_ext - 1) == '.' && strcasecmp(file_ext, ext) == 0)
231
    return 1;
232
 
233
  return 0;
234
}
235
 
236
int pspFileIoCheckIfExists(const char *path)
237
{
238
  SceIoStat stat;
239
  memset(&stat, 0, sizeof(stat));
240
 
241
  if (sceIoGetstat(path, &stat) < 0)
242
    return 0;
243
 
244
  return 1;
245
}
246
 
247
int pspFileIoDelete(const char *path)
248
{
249
  return sceIoRemove(path) >= 0;
250
}