00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031 #ifndef __TOOLS_H__
00032 #define __TOOLS_H__
00033
00034 #include "common.h"
00035 #include "rexp.h"
00036 #include "io_handler.h"
00037 #include "cds_objects.h"
00038 #include <sys/time.h>
00039
00040 #ifdef HAVE_MAGIC
00041
00042 extern "C" {
00043 #include <magic.h>
00044 }
00045
00046 #endif
00047
00053 zmm::Ref<zmm::Array<zmm::StringBase> > split_string(zmm::String str, char sep,
00054 bool empty = false);
00055
00059 zmm::Ref<zmm::Array<zmm::StringBase> > split_path(zmm::String str);
00060
00062 zmm::String trim_string(zmm::String str);
00063
00069 bool check_path(zmm::String path, bool needDir = false);
00070
00083 time_t check_path_ex(zmm::String path, bool needDir = false, bool existenceUnneeded = false, off_t *filesize = NULL);
00084
00090 bool is_executable(zmm::String path, int *err = NULL);
00091
00095 zmm::String find_in_path(zmm::String exec);
00096
00103 bool string_ok(zmm::String str);
00104
00105 bool string_ok(zmm::Ref<zmm::StringBuffer> str);
00106
00111 void string_ok_ex(zmm::String str);
00112
00118 zmm::String http_redirect_to(zmm::String ip, zmm::String port, zmm::String page = _(""));
00119
00124 zmm::String hex_encode(void *data, int len);
00125
00129 zmm::String hex_decode_string(zmm::String encoded);
00130
00131
00134 zmm::String generate_random_id();
00135
00137 zmm::String hex_md5(void *data, int length);
00138
00140 zmm::String hex_string_md5(zmm::String str);
00141
00145 zmm::String url_escape(zmm::String str);
00146
00148 zmm::String url_unescape(zmm::String str);
00149
00153 zmm::String mime_types_to_CSV(zmm::Ref<zmm::Array<zmm::StringBase> > mimeTypes);
00154
00158 zmm::String mt_strerror(int mt_errno);
00159
00161 zmm::String read_text_file(zmm::String path);
00162
00164 void write_text_file(zmm::String path, zmm::String contents);
00165
00169 void copy_file(zmm::String from, zmm::String to);
00170
00171 typedef int (*COMPARATOR) (void *, void *);
00172 typedef void * COMPARABLE;
00173
00174 int StringBaseComparatorAsc(void *, void *);
00175
00176 void quicksort(COMPARABLE *arr, int size, COMPARATOR comparator);
00177
00184 zmm::String renderProtocolInfo(zmm::String mimetype, zmm::String protocol = _(PROTOCOL), zmm::String extend = nil);
00185
00188 zmm::String getMTFromProtocolInfo(zmm::String protocol);
00189
00194 zmm::String getProtocol(zmm::String protocolInfo);
00195
00196
00199 zmm::String secondsToHMS(int seconds);
00200
00202 int HMSToSeconds(zmm::String time);
00203
00204 #ifdef HAVE_MAGIC
00206 zmm::String get_mime_type(magic_set *ms, zmm::Ref<RExp> reMimetype, zmm::String file);
00208 zmm::String get_mime_type_from_buffer(magic_set *ms, zmm::Ref<RExp> reMimetype,
00209 void *buffer, size_t length);
00210
00211 #endif // HAVE_MAGIC
00212
00213 #ifdef SOPCAST
00215 int find_local_port(unsigned short range_min,
00216 unsigned short range_max);
00217 #endif
00219 zmm::String get_jpeg_resolution(zmm::Ref<IOHandler> ioh);
00220
00222 void set_jpeg_resolution_resource(zmm::Ref<CdsItem> item, int res_num);
00223
00225 bool check_resolution(zmm::String resolution, int *x = NULL, int *y = NULL);
00226
00227 zmm::String escape(zmm::String string, char escape_char, char to_escape);
00228
00233 zmm::String unescape(zmm::String string, char escape);
00234
00235
00241
00242
00243
00250 zmm::String unescape_amp(zmm::String string);
00251
00256 zmm::String fallbackString(zmm::String first, zmm::String fallback);
00257
00261 unsigned int stringHash(zmm::String str);
00262
00263 zmm::String intArrayToCSV(int *array, int size);
00264
00265
00266
00267 void getTimespecNow(struct timespec *ts);
00268
00269 long getDeltaMillis(struct timespec *first);
00270 long getDeltaMillis(struct timespec *first, struct timespec *second);
00271
00272 void getTimespecAfterMillis(long delta, struct timespec *ret, struct timespec *start = NULL);
00273 int compareTimespecs(struct timespec *a, struct timespec *b);
00274
00276
00277
00278
00279
00280
00284 zmm::String normalizePath(zmm::String path);
00285
00289 zmm::String interfaceToIP(zmm::String interface);
00290
00293 bool validateYesNo(zmm::String value);
00294
00295
00303 zmm::Ref<zmm::Array<zmm::StringBase> > parseCommandLine(zmm::String line, zmm::String in, zmm::String out);
00304
00310 zmm::String tempName(zmm::String leadPath, char *tmpl);
00311
00313 bool isTheora(zmm::String ogg_filename);
00314
00318 zmm::String get_last_path(zmm::String location);
00319
00322 ssize_t getValidUTF8CutPosition(zmm::String str, size_t cutpos);
00323
00324 #ifdef EXTEND_PROTOCOLINFO
00325 zmm::String getDLNAtransferHeader(zmm::String mimeType, zmm::String header);
00326 #endif
00327
00328 #ifndef HAVE_FFMPEG
00333 zmm::String getAVIFourCC(zmm::String avi_filename);
00334 #endif
00335
00336 #ifdef TOMBDEBUG
00337
00338 struct profiling_t
00339 {
00340 struct timespec sum;
00341 struct timespec last_start;
00342 bool running;
00343 pthread_t thread;
00344 };
00345
00346 #define PROFILING_T_INIT {{0,0},{0,0},false, pthread_self()}
00347
00348 void profiling_start(struct profiling_t *data);
00349 void profiling_end(struct profiling_t *data);
00350 void profiling_print(struct profiling_t *data);
00351
00352 #define PROF_INIT(var) profiling_t var = PROFILING_T_INIT
00353 #define PROF_START(var) profiling_start(var)
00354 #define PROF_END(var) profiling_end(var)
00355 #define PROF_PRINT(var) profiling_print(var)
00356
00357 #else
00358
00359 #define profiling_t int
00360 #define PROF_INIT(var) profiling_t var
00361 #define PROF_START(var)
00362 #define PROF_END(var)
00363 #define PROF_PRINT(var)
00364
00365
00366
00367
00368
00369
00370
00371 #endif
00372
00373 #endif // __TOOLS_H__