libgphoto2 photo camera library (libgphoto2) Internals  2.5.26
gphoto2-port-portability.c
Go to the documentation of this file.
1 
28 #include "config.h"
29 #include <stdio.h>
30 #include <gphoto2/gphoto2-port.h>
33 
34 /* Windows Portability
35  ------------------------------------------------------------------ */
36 #ifdef WIN32
37 
38 
39 void gp_port_win_convert_path (char *path) {
40 
41  int x;
42 
43  if (strchr(path, '\\'))
44  /* already converted */
45  return;
46 
47  //What was the purpose of this?
48  //copying the second character to the first place if path does not start with "."?
49 /* if (path[0] != '.') {
50  path[0] = path[1];
51  path[1] = ':';
52  path[2] = '\\';
53  }
54 */
55 
56  for (x=0; x<strlen(path); x++)
57  if (path[x] == '/')
58  path[x] = '\\';
59 }
60 
61 int gp_system_mkdir (const char *dirname) {
62 
63  if (_mkdir(dirname) < 0)
64  return (GP_ERROR);
65  return (GP_OK);
66 }
67 
68 int gp_system_rmdir (const char *dirname) {
69 
70  if (_rmdir(dirname) < 0)
71  return (GP_ERROR);
72  return (GP_OK);
73 }
74 
75 
76 gp_system_dir gp_system_opendir (const char *dirname) {
77 
78  GPPORTWINDIR *d;
79  DWORD dr;
80  int x;
81  printf("blah2\n");
82  d = (GPPORTWINDIR*)malloc(sizeof(GPPORTWINDIR));
83  d->handle = INVALID_HANDLE_VALUE;
84  d->got_first = 0;
85  strcpy(d->dir, dirname);
86  d->drive_count = 0;
87  d->drive_index = 0;
88 
89  dr = GetLogicalDrives();
90 
91  for (x=0; x<32; x++) {
92  if ((dr >> x) & 0x0001) {
93  sprintf(d->drive[d->drive_count], "%c", 'A' + x);
94  d->drive_count += 1;
95  }
96  }
97 
98  return (d);
99 }
100 
101 gp_system_dirent gp_system_readdir (gp_system_dir d) {
102 
103  char dirn[1024];
104 
105  if (strcmp(d->dir, "/")==0) {
106  if (d->drive_index == d->drive_count)
107  return (NULL);
108  strcpy(d->search.cFileName, d->drive[d->drive_index]);
109  d->drive_index += 1;
110  return (&(d->search));
111  }
112 
113 
114  /* Append the wildcard */
115 
116  strcpy(dirn, d->dir);
117  gp_port_win_convert_path(dirn);
118 
119  if (dirn[strlen(dirn)-1] != '\\')
120  strcat(dirn, "\\");
121  strcat(dirn, "*");
122 
123 
124  if (d->handle == INVALID_HANDLE_VALUE) {
125  d->handle = FindFirstFile(dirn, &(d->search));
126  if (d->handle == INVALID_HANDLE_VALUE)
127  return NULL;
128  } else {
129  if (!FindNextFile(d->handle, &(d->search)))
130  return NULL;
131  }
132 
133  return (&(d->search));
134 }
135 
136 const char *gp_system_filename (gp_system_dirent de) {
137 
138  return (de->cFileName);
139 }
140 
141 int gp_system_closedir (gp_system_dir d) {
142  FindClose(d->handle);
143  free(d);
144  return (1);
145 }
146 
147 int gp_system_is_file (const char *filename) {
148 
149  struct stat st;
150 
151  gp_port_win_convert_path(filename);
152 
153  if (stat(filename, &st)!=0)
154  return 0;
155  return (st.st_mode & _S_IFREG);
156 }
157 
158 int gp_system_is_dir (const char *dirname) {
159 
160  struct stat st;
161 
162  if (strlen(dirname) <= 3)
163  return 1;
164 
165  gp_port_win_convert_path(dirname);
166 
167  if (stat(dirname, &st)!=0)
168  return 0;
169  return (st.st_mode & _S_IFDIR);
170 }
171 
172 
173 #else
174 
183 int gp_system_mkdir (const char *dirname) {
184  if (mkdir(dirname, 0777)<0)
185  return (GP_ERROR);
186  return (GP_OK);
187 }
188 
197 int gp_system_rmdir (const char *dirname) {
198 
199  if (rmdir (dirname) < 0)
200  return (GP_ERROR);
201 
202  return (GP_OK);
203 }
204 
214 gp_system_dir gp_system_opendir (const char *dirname) {
215  return (opendir(dirname));
216 }
217 
228 gp_system_dirent gp_system_readdir (gp_system_dir d) {
229  return (readdir(d));
230 }
231 
240 const char *gp_system_filename (gp_system_dirent de) {
241  return (de->d_name);
242 }
243 
252 int gp_system_closedir (gp_system_dir dir) {
253  closedir(dir);
254  return (GP_OK);
255 }
256 
266 int gp_system_is_file (const char *filename) {
267  struct stat st;
268 
269  if (stat(filename, &st)!=0)
270  return 0;
271  return (!S_ISDIR(st.st_mode));
272 }
273 
283 int gp_system_is_dir (const char *dirname) {
284  struct stat st;
285 
286  if (stat(dirname, &st)!=0)
287  return 0;
288  return (S_ISDIR(st.st_mode));
289 }
290 #endif
int gp_system_is_file(const char *filename)
check if passed filename is a file
const char * gp_system_filename(gp_system_dirent de)
retrieve UNIX filename out of a directory entry
int gp_system_closedir(gp_system_dir dir)
closedir UNIX functionality
gp_system_dir gp_system_opendir(const char *dirname)
opendir UNIX functionality
int gp_system_mkdir(const char *dirname)
mkdir UNIX functionality
gp_system_dirent gp_system_readdir(gp_system_dir d)
readdir UNIX functionality
int gp_system_is_dir(const char *dirname)
check if passed filename is a directory
int gp_system_rmdir(const char *dirname)
rmdir UNIX functionality
#define GP_OK
Everything is OK.
#define GP_ERROR
Generic Error.