summaryrefslogtreecommitdiff
path: root/plugins/shn/misc.c
blob: 759bb1d1627c9fa9f71fff5c95694cecb45e61ea (plain)
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
/*  misc.c - miscellaneous functions
 *  Copyright (C) 2000-2007  Jason Jordan <shnutils@freeshell.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * $Id: misc.c,v 1.14 2007/03/23 05:49:48 jason Exp $
 */

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "shorten.h"

void shn_snprintf(char *dest,int maxlen,char *formatstr, ...)
/* acts like snprintf, but makes 100% sure the string is NULL-terminated */
{
  va_list args;

  va_start(args,formatstr);

  shn_vsnprintf(dest,maxlen,formatstr,args);

  dest[maxlen-1] = 0;

  va_end(args);
}

int shn_filename_contains_a_dot(const char *filename)
{
	char *slash,*dot;

	dot = strrchr(filename,'.');
	if (!dot)
		return 0;

	slash = strrchr(filename,'/');
	if (!slash)
		return 1;

	if (slash < dot)
		return 1;
	else
		return 0;
}

char *shn_get_base_filename(const char *filename)
{
	const char *b,*e,*p;
	char *base;

	b = strrchr(filename,'/');

	if (b)
		b++;
	else
		b = filename;

	e = strrchr(filename,'.');

	if (e < b)
		e = filename + strlen(filename);

	if (NULL == (base = malloc((e - b + 1) * sizeof(char))))
	{
		shn_debug("Could not allocate memory for base filename");
		return NULL;
	}

	for (p=b;p<e;p++)
		*(base + (p - b)) = *p;

	*(base + (p - b)) = '\0';

	return base;
}

char *shn_get_base_directory(const char *filename)
{
	const char *e,*p;
	char *base;

	e = strrchr(filename,'/');

	if (!e)
		e = filename;

	if (NULL == (base = malloc((e - filename + 1) * sizeof(char))))
	{
		shn_debug("Could not allocate memory for base directory");
		return NULL;
	}

	for (p=filename;p<e;p++)
		*(base + (p - filename)) = *p;

	*(base + (p - filename)) = '\0';

	return base;
}

void shn_length_to_str(shn_file *info)
/* converts length of file to a string in m:ss or m:ss.ff format */
{
  ulong newlength,rem1,rem2,frames,ms;
  double tmp;

  if (PROB_NOT_CD(info->wave_header)) {
    newlength = (ulong)info->wave_header.exact_length;

    tmp = info->wave_header.exact_length - (double)((ulong)info->wave_header.exact_length);
    ms = (ulong)((tmp * 1000.0) + 0.5);

    if (1000 == ms) {
      ms = 0;
      newlength++;
    }

    shn_snprintf(info->wave_header.m_ss,16,"%lu:%02lu.%03lu",newlength/60,newlength%60,ms);
  }
  else {
    newlength = info->wave_header.length;

    rem1 = info->wave_header.data_size % CD_RATE;
    rem2 = rem1 % CD_BLOCK_SIZE;

    frames = rem1 / CD_BLOCK_SIZE;
    if (rem2 >= (CD_BLOCK_SIZE / 2))
      frames++;

    if (frames == CD_BLOCKS_PER_SEC) {
      frames = 0;
      newlength++;
    }

    shn_snprintf(info->wave_header.m_ss,16,"%lu:%02lu.%02lu",newlength/60,newlength%60,frames);
  }
}