File: list.c

package info (click to toggle)
lletters 0.1.95-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,152 kB
  • ctags: 502
  • sloc: sh: 9,985; ansic: 3,225; makefile: 230; sed: 93; perl: 50
file content (192 lines) | stat: -rw-r--r-- 3,647 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
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
#include "lletters.h"

/* prototype std lib functions */
extern int alphasort ();

/* local prototypes */
int file_select (struct direct *entry);

GSList *
lln_dir_list (GSList * file_list, gchar * directory_name, char letter)
{
  int count, i;
  struct direct **files;

  if (directory_name == NULL)
    {
/* Begin change JPV - 1999/03/06 */
      g_print (_("Error getting path\n"));
/* End change JPV - 1999/03/06 */
      return NULL;

    }

  count = scandir (directory_name, &files, file_select, alphasort);

  if (count <= 0)
    {
/* Begin change JPV - 1999/03/06 */
      g_print (_("No files in this directory\n"));
/* End change JPV - 1999/03/06 */
      return NULL;
    }

  for (i = 1; i < count + 1; ++i)
    {
      if (letter == lln_get_1st_letter (files[i - 1]->d_name))
	file_list = g_slist_append (file_list, 
				    g_strdup(files[i - 1]->d_name));
      free(files[i-1]);
    }
  free(files);

  return (file_list);
}
int
file_select (struct direct *entry)
{                               /* ignore . and .. entries */
    if ((strcmp (entry->d_name, ".") == 0) ||
	      (strcmp (entry->d_name, "..") == 0))
	      return (FALSE);
    else
	      return (TRUE);
}

char
lln_get_1st_letter (char *filename)
{
  char letter;

  letter = toupper (filename[0]);
  return (letter);
}

char *
lln_get_word (char *filename)
{
/* Parse the word out of the filename, and
 * format it to Proper Case */

  char *word;
  int len, i;
  word = g_strdup (filename);
  len = strlen (filename);

  word[0] = toupper (word[0]);
  for (i = 0; i < len; i++)
    {
      if (i != 0)
	word[i] = tolower (word[i]);
      if (word[i] == '.')
	word[i] = '\0';
      if (word[i] == '_' || word[i] == '-')
	word[i] = ' ';
    }
  return word;
}

char *
lln_get_number (char *filename)
{
/* Parse the number's caption out of the filename, and
 * format it to Proper Case. We start at the first letter, 
 * because the first character will be a digit. Number files 
 * should have the format 2Two_Apples.xpm. */

  GString *caption;
  char *tmp;
  int len, i;
  
  tmp = g_strdup(filename);
  len = strlen (filename);

  tmp[1] = toupper (tmp[1]);
  for (i = 1; i < len; i++)
    {
      if (i != 1)
	tmp[i] = tolower (tmp[i]);
      if (tmp[i] == '.')
	tmp[i] = '\0';
      if (tmp[i] == '_' || tmp[i] == '-')
	tmp[i] = ' ';
      tmp[i] = tmp[i];
    }
  
  caption = g_string_new(tmp);
  caption = g_string_erase(caption,0,1);
  
  return caption->str;
}

char *
lln_get_file (char letter)
{
  int i = 1, files, random_file;
  char *file;
  GSList *image_files, *tmp;
  gchar *imgdir;
  
  imgdir = g_strconcat (lln_image_dir, NULL);
  
  image_files = lln_dir_list (NULL, imgdir, letter);
  tmp = image_files;

  files = g_slist_length (tmp);
  random_file = lln_get_random ((double) files, letter);
  /*  g_print ("** DEBUG **: files = %d, random_file = %d\n", 
   *     files, random_file);
   */
  if (files == 0)
    {

/* Begin change JPV - 1999/03/06 */
      g_print (_("lln-message:\n"));
      g_print (_("No Matching image files for %c\n\n"), letter);
/* End change JPV - 1999/03/06 */

      return NULL;
    }
  while (i != random_file)
    {
      tmp = tmp->next;
      i++;
    }
  file = g_strdup (tmp->data);
  return file;


}

int 
lln_check_file (char *path)
{
  FILE *fd;
  fd = fopen (path, "r");
  if (fd == NULL) 
    return (FALSE);
  else
    return (TRUE);
}

int
lln_get_random (double max, char letter)
{
  int i;
  time_t t1;

  (void) time (&t1);
  /* use time in seconds to set seed */
  srand ((int) t1 + letter);

  i = 1 + (int) (max * rand () / (RAND_MAX + 1.0));
  //1+(rand() % max);

  return (i);
}