1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
1 /* misc.c - miscellaneous functions
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
2 * Copyright (C) 2000-2007 Jason Jordan <shnutils@freeshell.org>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
3 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
7 * (at your option) any later version.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
8 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
9 * This program is distributed in the hope that it will be useful,
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
12 * GNU General Public License for more details.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
13 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
14 * You should have received a copy of the GNU General Public License
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
15 * along with this program; if not, write to the Free Software
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
17 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
18
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
19 /*
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
20 * $Id: misc.c,v 1.14 2007/03/23 05:49:48 jason Exp $
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
21 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
22
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
23 #include <stdlib.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24 #include <string.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25 #include "shorten.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 void shn_snprintf(char *dest,int maxlen,char *formatstr, ...)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28 /* acts like snprintf, but makes 100% sure the string is NULL-terminated */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 va_list args;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32 va_start(args,formatstr);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 shn_vsnprintf(dest,maxlen,formatstr,args);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36 dest[maxlen-1] = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38 va_end(args);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 int shn_filename_contains_a_dot(char *filename)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43 char *slash,*dot;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 dot = strrchr(filename,'.');
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 if (!dot)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 slash = strrchr(filename,'/');
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 if (!slash)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51 return 1;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53 if (slash < dot)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 return 1;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 char *shn_get_base_filename(char *filename)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 char *b,*e,*p,*base;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 b = strrchr(filename,'/');
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 if (b)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 b++;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 b = filename;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 e = strrchr(filename,'.');
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 if (e < b)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 e = filename + strlen(filename);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 if (NULL == (base = malloc((e - b + 1) * sizeof(char))))
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 shn_debug("Could not allocate memory for base filename");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 return NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 for (p=b;p<e;p++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82 *(base + (p - b)) = *p;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84 *(base + (p - b)) = '\0';
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 return base;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 char *shn_get_base_directory(char *filename)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 char *e,*p,*base;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 e = strrchr(filename,'/');
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95 if (!e)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 e = filename;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
98 if (NULL == (base = malloc((e - filename + 1) * sizeof(char))))
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 shn_debug("Could not allocate memory for base directory");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101 return NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104 for (p=filename;p<e;p++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 *(base + (p - filename)) = *p;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107 *(base + (p - filename)) = '\0';
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 return base;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
112 void shn_length_to_str(shn_file *info)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 /* converts length of file to a string in m:ss or m:ss.ff format */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115 ulong newlength,rem1,rem2,frames,ms;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 double tmp;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118 if (PROB_NOT_CD(info->wave_header)) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 newlength = (ulong)info->wave_header.exact_length;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
120
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
121 tmp = info->wave_header.exact_length - (double)((ulong)info->wave_header.exact_length);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
122 ms = (ulong)((tmp * 1000.0) + 0.5);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
123
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
124 if (1000 == ms) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
125 ms = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
126 newlength++;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
127 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
128
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
129 shn_snprintf(info->wave_header.m_ss,16,"%lu:%02lu.%03lu",newlength/60,newlength%60,ms);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
130 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
131 else {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
132 newlength = info->wave_header.length;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
133
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
134 rem1 = info->wave_header.data_size % CD_RATE;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
135 rem2 = rem1 % CD_BLOCK_SIZE;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
136
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
137 frames = rem1 / CD_BLOCK_SIZE;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
138 if (rem2 >= (CD_BLOCK_SIZE / 2))
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
139 frames++;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
140
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
141 if (frames == CD_BLOCKS_PER_SEC) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
142 frames = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
143 newlength++;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
144 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
145
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
146 shn_snprintf(info->wave_header.m_ss,16,"%lu:%02lu.%02lu",newlength/60,newlength%60,frames);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
147 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
148 }
|