annotate src/shnplug/misc.c @ 1862:d9d4b258fbf1

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