1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Michael Sevakis
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
32 #include "timefuncs.h"
34 #if CONFIG_CODEC == SWCODEC
35 int round_value_to_list32(unsigned long value
,
36 const unsigned long list
[],
40 unsigned long dmin
= ULONG_MAX
;
43 for (i
= 0; i
< count
; i
++)
53 if (signd
? ((long)list
[i
] < (long)value
) : (list
[i
] < value
))
54 diff
= value
- list
[i
];
56 diff
= list
[i
] - value
;
66 } /* round_value_to_list32 */
68 /* Number of bits set in src_mask should equal src_list length */
69 int make_list_from_caps32(unsigned long src_mask
,
70 const unsigned long *src_list
,
71 unsigned long caps_mask
,
72 unsigned long *caps_list
)
77 for (mask
= src_mask
, count
= 0, i
= 0;
81 unsigned long test_bit
;
82 mask
&= mask
- 1; /* Zero lowest bit set */
83 test_bit
= mask
^ src_mask
; /* Isolate the bit */
84 if (test_bit
& caps_mask
) /* Add item if caps has test bit set */
85 caps_list
[count
++] = src_list
? src_list
[i
] : (unsigned long)i
;
89 } /* make_list_from_caps32 */
90 #endif /* CONFIG_CODEC == SWCODEC */
92 /* Create a filename with a number part in a way that the number is 1
93 * higher than the highest numbered file matching the same pattern.
94 * It is allowed that buffer and path point to the same memory location,
95 * saving a strcpy(). Path must always be given without trailing slash.
96 * "num" can point to an int specifying the number to use or NULL or a value
97 * less than zero to number automatically. The final number used will also
98 * be returned in *num. If *num is >= 0 then *num will be incremented by
100 char *create_numbered_filename(char *buffer
, const char *path
,
101 const char *prefix
, const char *suffix
,
102 int numberlen
IF_CNFN_NUM_(, int *num
))
105 struct dirent
*entry
;
108 int prefixlen
= strlen(prefix
);
112 strncpy(buffer
, path
, MAX_PATH
);
114 pathlen
= strlen(buffer
);
117 if (num
&& *num
>= 0)
119 /* number specified */
125 /* automatic numbering */
128 dir
= opendir(pathlen
? buffer
: "/");
132 while ((entry
= readdir(dir
)))
136 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
137 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
140 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
141 if (curr_num
> max_num
)
150 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
151 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
164 /* Create a filename with a date+time part.
165 It is allowed that buffer and path point to the same memory location,
166 saving a strcpy(). Path must always be given without trailing slash.
167 unique_time as true makes the function wait until the current time has
169 char *create_datetime_filename(char *buffer
, const char *path
,
170 const char *prefix
, const char *suffix
,
173 struct tm
*tm
= get_time();
174 static struct tm last_tm
;
177 while (unique_time
&& !memcmp(get_time(), &last_tm
, sizeof (struct tm
)))
183 strncpy(buffer
, path
, MAX_PATH
);
185 pathlen
= strlen(buffer
);
186 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
187 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
188 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
189 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
193 #endif /* CONFIG_RTC */