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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*
* utils.c
* Copyright (C) 2019 David García Goñi <dagargo@gmail.com>
*
* This file is part of Elektroid.
*
* Elektroid 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 3 of the License, or
* (at your option) any later version.
*
* Elektroid 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 Elektroid. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#if !defined(__linux__)
#include <dirent.h>
#endif
#include <errno.h>
#include "utils.h"
#define DEBUG_SHORT_HEX_LEN 64
#define DEBUG_FULL_HEX_THRES 5
gint debug_level;
static guint
get_max_message_length (guint msg_len)
{
guint len;
if (debug_level >= DEBUG_FULL_HEX_THRES)
{
len = msg_len;
}
else
{
len = msg_len > DEBUG_SHORT_HEX_LEN ? DEBUG_SHORT_HEX_LEN : msg_len;
}
return len;
}
gchar *
debug_get_hex_data (gint level, guint8 *data, guint len)
{
gint i;
guint8 *b;
guint size;
guint bytes_shown;
guint extra;
gchar *str;
gchar *next;
if (level >= DEBUG_FULL_HEX_THRES)
{
bytes_shown = len;
extra = 0;
}
else
{
if (len > DEBUG_SHORT_HEX_LEN)
{
bytes_shown = DEBUG_SHORT_HEX_LEN;
extra = 3;
}
else
{
bytes_shown = len;
extra = 0;
}
}
size = bytes_shown * 3 + extra;
if (!size)
{
return NULL;
}
str = g_malloc (sizeof (char) * size);
b = data;
next = str;
sprintf (next, "%02x", *b);
next += 2;
b++;
i = 1;
while (i < get_max_message_length (len))
{
sprintf (next, " %02x", *b);
next += 3;
b++;
i++;
}
if (level < DEBUG_FULL_HEX_THRES && len > DEBUG_SHORT_HEX_LEN)
{
sprintf (next, "...");
next += 3;
}
return str;
}
gchar *
debug_get_hex_msg (const GByteArray *msg)
{
return debug_get_hex_data (debug_level, msg->data, msg->len);
}
void
filename_remove_ext (char *name)
{
gint namelen = strlen (name);
gchar *dot = &name[namelen - 1];
gint i = namelen - 1;
while (i > 0)
{
if (*dot == '.')
{
*dot = 0;
break;
}
dot--;
i--;
}
}
const gchar *
filename_get_ext (const gchar *name)
{
int namelen = strlen (name);
const gchar *ext = &name[namelen], *p = name;
for (guint i = 0; i < namelen; i++, p++)
{
if (*p == '.')
{
i++;
p++;
ext = p;
}
}
return ext;
}
//The returned value is owned by the caller.
//As this is used from the code, rel_dir uses '/' always and needs to be converted.
gchar *
get_user_dir (const char *rel_dir)
{
const gchar *home = g_get_home_dir ();
if (rel_dir)
{
gchar *rel_dir_conv = path_translate (PATH_SYSTEM, rel_dir);
gchar *dir = path_chain (PATH_SYSTEM, home, rel_dir_conv);
g_free (rel_dir_conv);
return dir;
}
else
{
return strdup (home);
}
}
char *
get_system_startup_path (const gchar *local_dir)
{
DIR *dir;
gchar *startup_path = NULL;
if (local_dir)
{
dir = opendir (local_dir);
if (dir)
{
startup_path = strdup (local_dir);
}
else
{
error_print ("Unable to open dir '%s'", local_dir);
}
closedir (dir);
}
if (!startup_path)
{
startup_path = get_user_dir (NULL);
}
debug_print (1, "Using '%s' as local dir...", startup_path);
return startup_path;
}
void
free_msg (gpointer msg)
{
g_byte_array_free ((GByteArray *) msg, TRUE);
}
gint
file_load (const char *path, struct idata *idata, struct job_control *control)
{
FILE *f;
size_t size;
gint res;
GByteArray *array;
f = fopen (path, "rb");
if (!f)
{
return -errno;
}
res = 0;
if (fseek (f, 0, SEEK_END))
{
error_print ("Unexpected value");
res = -errno;
goto end;
}
size = ftell (f);
rewind (f);
array = g_byte_array_sized_new (size);
array->len = size;
if (fread (array->data, 1, size, f) == size)
{
gchar *name = g_path_get_basename (path);
filename_remove_ext (name);
idata_init (idata, array, strdup (name), NULL);
g_free (name);
debug_print (1, "%zu B read", size);
}
else
{
error_print ("Error while reading from file %s", path);
g_byte_array_free (array, TRUE);
res = -errno;
}
end:
fclose (f);
return res;
}
gint
file_save_data (const gchar *path, const guint8 *data, ssize_t len)
{
gint res;
size_t bytes;
FILE *file;
file = fopen (path, "wb");
if (!file)
{
return -errno;
}
debug_print (1, "Saving file %s...", path);
res = 0;
bytes = fwrite (data, 1, len, file);
if (bytes == len)
{
debug_print (1, "%zu B written", bytes);
}
else
{
error_print ("Error while writing to file %s", path);
res = -EIO;
}
fclose (file);
return res;
}
gint
file_save (const gchar *path, struct idata *idata,
struct job_control *control)
{
return file_save_data (path, idata->content->data, idata->content->len);
}
gchar *
get_human_size (gint64 size, gboolean with_space)
{
gchar *label = g_malloc (LABEL_MAX);
gchar *space = with_space ? " " : "";
if (size < 0)
{
*label = 0;
}
else if (size < KI)
{
snprintf (label, LABEL_MAX, "%" PRId64 "%sB", size, space);
}
else if (size < MI)
{
snprintf (label, LABEL_MAX, "%.4g%sKiB", size / (double) KI, space);
}
else if (size < GI)
{
snprintf (label, LABEL_MAX, "%.4g%sMiB", size / (double) MI, space);
}
else
{
snprintf (label, LABEL_MAX, "%.4g%sGiB", size / (double) GI, space);
}
return label;
}
static inline void
job_control_set_progress_value (struct job_control *control, gdouble p)
{
if (control->parts)
{
if (control->part == control->parts)
{
control->progress = 1.0;
}
else
{
control->progress =
(control->part / (double) control->parts) +
(p / (double) control->parts);
}
}
else
{
control->progress = 0.0;
}
}
void
job_control_set_sample_progress_no_sync (struct job_control *control,
gdouble p, gpointer data)
{
job_control_set_progress_value (control, p);
if (control->callback)
{
control->callback (control);
}
}
void
job_control_set_progress (struct job_control *control, gdouble p)
{
g_mutex_lock (&control->mutex);
job_control_set_progress_value (control, p);
g_mutex_unlock (&control->mutex);
if (control->callback)
{
control->callback (control);
}
}
gboolean
file_matches_extensions (const gchar *name, const gchar **extensions)
{
const gchar *extension;
const gchar **e = extensions;
if (!e)
{
return TRUE;
}
extension = filename_get_ext (name);
if (!*extension)
{
return FALSE;
}
while (*e)
{
if (!strcasecmp (extension, *e))
{
return TRUE;
}
e++;
}
return FALSE;
}
static inline const gchar *
path_get_separator (enum path_type type)
{
const gchar *sep;
if (type == PATH_SYSTEM)
{
#if defined(__MINGW32__) | defined(__MINGW64__)
sep = "\\";
#else
sep = "/";
#endif
}
else
{
sep = "/";
}
return sep;
}
gchar *
path_chain (enum path_type type, const gchar *parent, const gchar *child)
{
const gchar *sep = path_get_separator (type);
return g_build_path (sep, parent, child, NULL);
}
//Translate from internal path to system path.
gchar *
path_translate (enum path_type type, const gchar *input)
{
gchar *output, *o;
const gchar *i;
const gchar *sep = path_get_separator (type);
if (!strcmp (sep, "/"))
{
return strdup (input);
}
output = g_malloc (strlen (input) * 2 + 1); //Worst case scenario
i = input;
o = output;
while (*i)
{
if (*i == '/')
{
*o = 0;
strcat (o, sep);
o += strlen (sep);
}
else
{
*o = *i;
o++;
}
i++;
}
*o = 0;
return output;
}
//These two functions are needed as g_filename_to_uri and g_uri_to_filename
//depend on the local system and therefore can not be used for BE_TYPE_MIDI
//under MSYS2.
gchar *
path_filename_from_uri (enum path_type type, gchar *uri)
{
if (type == PATH_SYSTEM)
{
return g_filename_from_uri (uri, NULL, NULL);
}
const gchar *filename = &uri[7]; //Skip "file://".
return g_uri_unescape_string (filename, ":/");
}
gchar *
path_filename_to_uri (enum path_type type, gchar *filename)
{
if (type == PATH_SYSTEM)
{
return g_filename_to_uri (filename, NULL, NULL);
}
gchar *uri = g_strconcat ("file://", filename, NULL);
gchar *escaped_uri = g_uri_escape_string (uri, ":/", FALSE);
g_free (uri);
return escaped_uri;
}
void
gslist_fill (GSList **list, ...)
{
gpointer v;
va_list argptr;
*list = NULL;
va_start (argptr, list);
while ((v = va_arg (argptr, gpointer)))
{
*list = g_slist_append (*list, v);
}
va_end (argptr);
}
void
idata_init (struct idata *idata, GByteArray *content, gchar *name, void *info)
{
idata->content = content;
idata->name = name;
idata->info = info;
}
void
idata_free (struct idata *idata)
{
if (idata->content)
{
g_byte_array_free (idata->content, TRUE);
idata->content = NULL;
}
g_free (idata->name);
idata->name = NULL;
g_free (idata->info);
idata->info = NULL;
}
GByteArray *
idata_steal (struct idata *idata)
{
g_free (idata->name);
g_free (idata->info);
return idata->content;
}
gboolean
job_control_get_active_lock (struct job_control *control)
{
gboolean active;
g_mutex_lock (&control->mutex);
active = control->active;
g_mutex_unlock (&control->mutex);
return active;
}
void
job_control_set_active_lock (struct job_control *control, gboolean active)
{
g_mutex_lock (&control->mutex);
control->active = active;
g_mutex_unlock (&control->mutex);
}
void
job_control_reset (struct job_control *control, gint parts)
{
control->parts = parts;
control->part = 0;
job_control_set_progress (control, 0.0);
}
|