File: virtualdir.c

package info (click to toggle)
gtoaster 0.19991130-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,700 kB
  • ctags: 1,318
  • sloc: ansic: 12,739; sh: 353; makefile: 217; sed: 93
file content (225 lines) | stat: -rw-r--r-- 5,477 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Virtualdir is a collection of routines managing virtual directory entries.
 * It is used to display the contents of the last session in gnometoaster. */

#include <gtk/gtk.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "virtualdir.h"

/* uncomment for debugging */
/* #define DEBUG */

/* create a new dir */
virtualdir_dir *virtualdir_create(/* informations about the file/directory
				   * to be created */
				  char *name,
				  unsigned int mode,
				  unsigned long int filesize)
{
   virtualdir_dir *result;
   
   result=(virtualdir_dir*)malloc(sizeof(virtualdir_dir));
   /* directory is empty */
   result->entries=NULL;
   strcpy((char*)&result->name,name);
   result->mode=mode;
   result->filesize=filesize;
   
   return result;
};

/* find entry with name in directory dir */
virtualdir_dir *virtualdir_findentry(virtualdir_dir *dir,
				     gchar *name)
{
   virtualdir_dir *result=NULL;
   GList *current;
   
#ifdef DEBUG
   printf ("virtualdir_findentry: scanning for %s ... ",name);
#endif	

   for (current=dir->entries;current!=NULL;current=current->next)
     {
	if (!strcmp(name,
		    (gchar*)&(((virtualdir_dir*)current->data)->name)))
	  {
	     result=(virtualdir_dir*)current->data; 
	  };
     };
   
#ifdef DEBUG
   if (result!=NULL)
     printf ("found.\n");
   else
     printf ("not found.\n");
#endif
   return result;
};

/* find a subdirectory of dir root */
virtualdir_dir *virtualdir_finddir(virtualdir_dir *root,
				   gchar *path)
{
   gchar *subpath;
   gchar *rest;
   virtualdir_dir *entry;
   virtualdir_dir *result;

#ifdef DEBUG
   printf ("virtualdir_finddir: seeking directory %s\n",path);
#endif
   
   /* get the "leftmost" dir name of the directory tree */
   subpath=(gchar*)malloc(strlen(path)+1);
   if (path[0]=='/')
     strcpy(subpath,&path[1]);
   else
     strcpy(subpath,path);
   
   /* find the end of the "leftmost" dir name */
   rest=strchr(subpath,'/');
   /* if terminated by '/',terminate the string with 0. */
   if (rest!=NULL)
     {      
	*rest=0;
	rest++;
     }	
   /* position pointer to rest string to stringend in case
    * of no more slashes,position behind the last slash otherwise */
   else     
     rest=subpath+strlen(subpath);
#ifdef DEBUG
   printf ("virtualdir_finddir: yet to search: %s\n",
	   rest);
#endif
   
   if (strlen(subpath)>0)
     {
	/* seek the desired directory entry */   
	entry=virtualdir_findentry(root,
				   subpath);
	/* see if a directory with that name exists */
	if (entry!=NULL)
	  {
	     /* if so, check if we have any more subtrees to get in */
	     if (strlen(rest)>0)
	       {		  
		  result=virtualdir_finddir(entry,rest);
#ifdef DEBUG
		  if (result!=NULL)
		    printf ("virtualdir_finddir: got result from rec. call %s\n",			    
			    (char*)&result->name);
		  else
		    printf ("virtualdir_finddir: got invalid result from rec. call\n");
#endif
	       }
	     /* in case we don't,the entry just found is the "rightmost"
	      * dir of the tree,hence what we're searching for */
	     else	  
	       result=entry;
	  }
	/* if a directory with the desired name couldn't be found,
	 * return NULL */
	else
	  result=NULL;
     }
   else
     /* if no directory name was given,return pointer to root directory */
     result=root;

   
#ifdef DEBUG
   printf ("virtualdir_finddir: freeing temporary data\n");
#endif
   /* free our temporary string */
   free(subpath);
   
#ifdef DEBUG
   if (result!=NULL)
     printf ("virtualdir_finddir: found %s\n",(char*)&result->name);
#endif   
   /* and return what we have found */
   return result;
};

/* open a subdirectory of dir root for reading */
virtualdir_VDIR *virtualdir_open(virtualdir_dir *root,
				 gchar *path)
{
   virtualdir_VDIR *result;
   virtualdir_dir *dir;
   
   result=(virtualdir_VDIR*)malloc(sizeof(virtualdir_VDIR));
   dir=virtualdir_finddir(root,path);
   if (dir==NULL)
     {
	free(result);
	result=NULL;	
     }
   else
     *result=dir->entries;
   return result;
};

/* read a directory entry. this call's structur is similar to
 * the readdir() libc function.  it will return 
 * a pointer to the direntry struct or NULL if the end of the directory
 * has been reached */
virtualdir_dir *virtualdir_read(virtualdir_VDIR *vdir)
{
   virtualdir_dir *result;
   
   if (*vdir!=NULL)
     {	
	result=(virtualdir_dir*)((*vdir)->data);
	*vdir=(*vdir)->next;
     }
   else
     result=NULL;

   return result;
};

/* close a VDIR opened by virtualdir_open */
void virtualdir_close(virtualdir_VDIR *vdir)
{
   free(vdir);
};

/* add a direntry structure to the virtual directory
 * structure specified by root in subdir path,
 * client should be created with virtualdir_create() */
void virtualdir_addentry(virtualdir_dir *root,
			 virtualdir_dir *client,
			 char *path)
{
   virtualdir_dir *destdir;
   
   destdir=virtualdir_finddir(root,path);
   /* if desired destination could be found */   
   if (destdir!=NULL)
     {	
#ifdef DEBUG
	printf ("virtualdir_addentry: adding client to %s\n",(char*)&destdir->name);
#endif
	destdir->entries=g_list_append(destdir->entries,(gpointer)client);
     };
};

void virtualdir_deletestructure(virtualdir_dir *root)
{
   
   /* delete recursively */
   while (root->entries!=NULL)
     {
	virtualdir_deletestructure((virtualdir_dir*)(root->entries->data));
	root->entries=g_list_remove(root->entries,root->entries->data);
     };	
   free (root);
};