[go: up one dir, main page]

File: immsutil.cc

package info (click to toggle)
imms 3.1.0~rc8-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,228 kB
  • ctags: 1,243
  • sloc: cpp: 7,839; ansic: 1,845; xml: 252; sh: 189; makefile: 140
file content (198 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download | duplicates (3)
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
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <stdlib.h>     // for (s)random
#include <signal.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>

#include <iostream>
#include <fstream>

#include "immsconf.h"
#include "immsutil.h"

using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios_base;

// Random
int imms_random(int max)
{
    int rand_num;
    static bool initialized = false;
#ifdef INITSTATE_USABLE
    static struct random_data rand_data;
    static char rand_state[256];
    if (!initialized)
    {
        rand_data.state = (int32_t*)rand_state;
        initstate_r(time(0), rand_state, sizeof(rand_state), &rand_data);
        initialized = true;
    }
    random_r(&rand_data, &rand_num);
#else
    if (!initialized)
    {
        srandom(time(0));
        initialized = true;
    }
    rand_num = random();
#endif
    double cof = rand_num / (RAND_MAX + 1.0);
    return (int)(max * cof);
}

uint64_t usec_diff(struct timeval &tv1, struct timeval &tv2)
{
    return (tv2.tv_sec - tv1.tv_sec) * 1000000
        + tv2.tv_usec - tv1.tv_usec;
}

StackTimer::StackTimer()
{
#ifdef DEBUG
    gettimeofday(&start, 0);
#endif
}

StackTimer::~StackTimer()
{
#ifdef DEBUG
    struct timeval end;
    gettimeofday(&end, 0);
    std::cout << usec_diff(start, end) / 1000 << " msecs elapsed" << std::endl;
#endif
}

string get_imms_root(const string &file)
{
    static string dotimms;
    if (dotimms == "")
    {
        char *immsroot = getenv("IMMSROOT");
        if (immsroot)
        {
            dotimms = immsroot;
            dotimms.append("/");
        }
        else
        {
            dotimms = getenv("HOME");
            dotimms.append("/.imms/");
        }
    }
    return dotimms + file;
}

StackLockFile::StackLockFile(const string &_name) : name(_name)
{
    while (1)
    {
        ifstream lockfile(name.c_str());
        int pid = 0;
        lockfile >> pid;
        if (!pid)
            break;
        if (kill(pid, 0))
            break;
        name = "";
        return;
    }

    ofstream lockfile(name.c_str(), ios_base::trunc | ios_base::out);
    lockfile << getpid() << std::endl;
    lockfile.close();
}

StackLockFile::~StackLockFile()
{
    if (name != "")
        unlink(name.c_str());
}

float rms_string_distance(const string &s1, const string &s2, int max)
{
    if (s1 == "" || s2 == "")
        return 0;

    int len = s1.length();
    if (len != (int)s2.length())
        return 0;
    len = std::min(len, max);
    float distance = 0;

    for (int i = 0; i < len; ++i)
        distance += pow(s1[i] - s2[i], 2);

    return sqrt(distance / len);
}

string path_normalize(const string &path)
{
    const char *start = path.c_str();
    while (isspace(*start))
        start++;
    if (access(start, R_OK))
        return start;
    char resolved[4096];
    realpath(start, resolved);
    return resolved;
}

int listdir(const string &dirname, vector<string> &files)
{
    files.clear();
    DIR *dir = opendir(dirname.c_str());
    if (!dir)
        return errno;
    struct dirent *de;
    while ((de = readdir(dir)))
        files.push_back(de->d_name);
    closedir(dir);
    return 0;
}

int socket_connect(const string &sockname)
{
    int fd = socket(PF_UNIX, SOCK_STREAM, 0);

    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strncpy(sun.sun_path, sockname.c_str(), sizeof(sun.sun_path));
    if (connect(fd, (sockaddr*)&sun, sizeof(sun)))
    {
        close(fd);
        return -1;
    }
    return fd;
}

bool file_exists(const string &filename) {
    struct stat buf;
    return !stat(filename.c_str(), &buf);
}