comparison src/shnplug/misc.c @ 1305:51bf0e431e02

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