Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c6994d41a3
Fetching contributors…

Cannot retrieve contributors at this time

file 101 lines (86 sloc) 2.809 kb
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
// Folve - A fuse filesystem that convolves audio files on-the-fly.
//
// Copyright (C) 2012 Henner Zeller <h.zeller@acm.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 3 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, see <http://www.gnu.org/licenses/>.

#include "util.h"

#include <assert.h>
#include <stdio.h>
#include <sys/time.h>
#include <syslog.h>

#include <cstdarg>

double folve::CurrentTime() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec / 1e6;
}

static void vAppendf(std::string *str, const char *format, va_list ap) {
  const size_t orig_len = str->length();
  const size_t space = 1024; // there should be better ways to do this...
  str->resize(orig_len + space);
  int written = vsnprintf((char*)str->data() + orig_len, space, format, ap);
  str->resize(orig_len + written);
}

void folve::Appendf(std::string *str, const char *format, ...) {
  va_list ap;
  va_start(ap, format);
  vAppendf(str, format, ap);
  va_end(ap);
}

std::string folve::StringPrintf(const char *format, ...) {
  va_list ap;
  va_start(ap, format);
  std::string result;
  vAppendf(&result, format, ap);
  va_end(ap);
  return result;
}

static bool global_debug_log = false;
void folve::EnableDebugLog(bool b) {
  if (b != global_debug_log) {
    syslog(LOG_INFO, "Switch debug mode %s.", b ? "on" : "off");
    global_debug_log = b;
  }
}

bool folve::IsDebugLogEnabled() {
  return global_debug_log;
}

void folve::DLogf(const char *format, ...) {
  if (!global_debug_log) return;
  va_list ap;
  va_start(ap, format);
  vsyslog(LOG_DEBUG, format, ap);
  va_end(ap);
}

bool folve::HasSuffix(const std::string &str, const std::string &suffix) {
  if (str.length() < suffix.length()) return false;
  return str.compare(str.length() - suffix.length(),
                     suffix.length(), suffix) == 0;
}

static void *CallRun(void *tobject) {
  reinterpret_cast<folve::Thread*>(tobject)->Run();
  return NULL;
}

folve::Thread::Thread() : started_(false) {}
folve::Thread::~Thread() { WaitFinished(); }

void folve::Thread::Start() {
  assert(!started_);
  pthread_create(&thread_, NULL, &CallRun, this);
  started_ = true;
}

void folve::Thread::WaitFinished() {
  if (!started_) return;
  pthread_join(thread_, NULL);
  started_ = false;
}
Something went wrong with that request. Please try again.