~ubuntu-branches/ubuntu/saucy/apvlv/saucy

« back to all changes in this revision

Viewing changes to src/ApvlvUtil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-06-01 22:18:49 UTC
  • mfrom: (10.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110601221849-niibd5p5i7avemy4
Tags: 0.1.1-1.1
* Non-maintainer upload.
* Add support for poppler >= 0.15. Patch by Pino Toscano. (Closes: #627609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include "ApvlvUtil.hpp"
30
30
 
31
 
#include <stdlib.h>
 
31
#include <gtk/gtk.h>
 
32
 
32
33
#ifndef WIN32
33
34
#include <sys/wait.h>
34
35
#endif
35
 
#include <gtk/gtk.h>
36
 
 
37
36
#ifdef WIN32
38
37
# include <windows.h>
39
38
#endif
43
42
using namespace std;
44
43
 
45
44
namespace apvlv
46
 
{
 
45
  {
47
46
 
48
47
#ifdef WIN32
49
48
  string helppdf = "~\\Startup.pdf";
50
49
  string iniexam = "~\\apvlvrc.example";
51
 
  string iconreg = "~\\reg.png";
52
 
  string icondir = "~\\dir.png";
53
 
  string iconpdf = "~\\pdf.png";
 
50
  string iconreg = "~\\icons\\reg.png";
 
51
  string icondir = "~\\icons\\dir.png";
 
52
  string iconpdf = "~\\icons\\pdf.png";
54
53
  string inifile = "~\\_apvlvrc";
55
54
  string sessionfile = "~\\_apvlvinfo";
56
55
#else
57
56
  string helppdf = string (DOCDIR) + "/Startup.pdf";
58
57
  string iniexam = string (DOCDIR) + "/apvlvrc.example";
59
 
  string iconreg = string (DOCDIR) + "/reg.png";
60
 
  string icondir = string (DOCDIR) + "/dir.png";
61
 
  string iconpdf = string (DOCDIR) + "/pdf.png";
 
58
  string iconreg = string (DOCDIR) + "/icons/reg.png";
 
59
  string icondir = string (DOCDIR) + "/icons/dir.png";
 
60
  string iconpdf = string (DOCDIR) + "/icons/pdf.png";
62
61
  string inifile = "~/.apvlvrc";
63
62
  string sessionfile = "~/.apvlvinfo";
64
63
#endif
71
70
 
72
71
    if (g_path_is_absolute (path))
73
72
      {
74
 
        return g_strdup (path);
 
73
        return g_strdup (path);
75
74
      }
76
75
 
77
76
    if (*path == '~' && *(path + 1) == PATH_SEP_C)
78
77
      {
79
 
        const gchar *home;
 
78
        const gchar *home;
80
79
 
81
80
#ifdef WIN32
82
 
        home = g_win32_get_package_installation_directory_of_module (NULL);
 
81
        home = g_win32_get_package_installation_directory_of_module (NULL);
83
82
#else
84
 
        home = getenv ("HOME");
85
 
        if (home == NULL)
86
 
          {
87
 
            home = g_get_home_dir ();
88
 
          }
 
83
        home = getenv ("HOME");
 
84
        if (home == NULL)
 
85
          {
 
86
            home = g_get_home_dir ();
 
87
          }
89
88
#endif
90
89
 
91
 
        if (home != NULL)
92
 
          {
93
 
            g_snprintf (abpath, sizeof abpath, "%s%s", home, ++path);
94
 
          }
95
 
        else
96
 
          {
97
 
            debug ("Can't find home directory, use current");
98
 
            g_snprintf (abpath, sizeof abpath, "%s", path + 2);
99
 
          }
 
90
        if (home != NULL)
 
91
          {
 
92
            g_snprintf (abpath, sizeof abpath, "%s%s", home, ++path);
 
93
          }
 
94
        else
 
95
          {
 
96
            debug ("Can't find home directory, use current");
 
97
            g_snprintf (abpath, sizeof abpath, "%s", path + 2);
 
98
          }
100
99
      }
101
100
    else
102
101
      {
103
 
        const gchar *pwd;
 
102
        const gchar *pwd;
104
103
 
105
 
        pwd = g_get_current_dir ();
106
 
        if (pwd != NULL)
107
 
          {
108
 
            g_snprintf (abpath, sizeof abpath, "%s/%s", pwd, path);
109
 
          }
110
 
        else
111
 
          {
112
 
            debug ("Can't find current directory, use current");
113
 
            g_snprintf (abpath, sizeof abpath, "%s", path);
114
 
          }
 
104
        pwd = g_get_current_dir ();
 
105
        if (pwd != NULL)
 
106
          {
 
107
            g_snprintf (abpath, sizeof abpath, "%s/%s", pwd, path);
 
108
          }
 
109
        else
 
110
          {
 
111
            debug ("Can't find current directory, use current");
 
112
            g_snprintf (abpath, sizeof abpath, "%s", path);
 
113
          }
115
114
      }
116
115
 
117
116
    return g_strdup (abpath);
118
117
  }
119
118
 
120
119
  gboolean walkdir (const char *name, gboolean (*cb) (const char *, void *),
121
 
                    void *usrp)
 
120
                    void *usrp)
122
121
  {
123
122
    GDir *dir = g_dir_open (name, 0, NULL);
124
123
    if (dir == NULL)
125
124
      {
126
 
        debug ("Open dir: %s failed", name);
127
 
        return FALSE;
 
125
        debug ("Open dir: %s failed", name);
 
126
        return FALSE;
128
127
      }
129
128
 
130
129
    const gchar *token;
131
130
    while ((token = g_dir_read_name (dir)) != NULL)
132
131
      {
133
 
        gchar *subname = g_strjoin (PATH_SEP_S, name, token, NULL);
134
 
        if (subname == NULL)
135
 
          {
136
 
            continue;
137
 
          }
 
132
        gchar *subname = g_strjoin (PATH_SEP_S, name, token, NULL);
 
133
        if (subname == NULL)
 
134
          {
 
135
            continue;
 
136
          }
138
137
 
139
 
        if (g_file_test (subname, G_FILE_TEST_IS_REGULAR) == TRUE)
140
 
          {
141
 
            if (cb (subname, usrp) == FALSE)
142
 
              {
143
 
                return FALSE;
144
 
              }
145
 
          }
146
 
        else if (g_file_test (subname, G_FILE_TEST_IS_DIR) == TRUE)
147
 
          {
148
 
            if (walkdir (subname, cb, usrp) == FALSE)
149
 
              {
150
 
                return FALSE;
151
 
              }
152
 
          }
 
138
        if (g_file_test (subname, G_FILE_TEST_IS_REGULAR) == TRUE)
 
139
          {
 
140
            if (cb (subname, usrp) == FALSE)
 
141
              {
 
142
                return FALSE;
 
143
              }
 
144
          }
 
145
        else if (g_file_test (subname, G_FILE_TEST_IS_DIR) == TRUE)
 
146
          {
 
147
            if (walkdir (subname, cb, usrp) == FALSE)
 
148
              {
 
149
                return FALSE;
 
150
              }
 
151
          }
153
152
      }
154
153
 
155
154
    g_dir_close (dir);
168
167
    gboolean ret = g_file_get_contents (s, &content, NULL, NULL);
169
168
    if (ret == TRUE)
170
169
      {
171
 
        ret = g_file_set_contents (d, content, -1, NULL);
172
 
        g_free (content);
173
 
        ok = ret;
 
170
        ret = g_file_set_contents (d, content, -1, NULL);
 
171
        g_free (content);
 
172
        ok = ret;
174
173
      }
175
174
 
176
175
    g_free (s);
192
191
  }
193
192
 
194
193
  void
195
 
    logv (const char *level, const char *file, int line, const char *func,
196
 
          const char *ms, ...)
 
194
  logv (const char *level, const char *file, int line, const char *func,
 
195
        const char *ms, ...)
197
196
  {
198
197
    char p[0x1000], temp[0x100];
199
198
    va_list vap;
200
199
 
201
200
    g_snprintf (temp, sizeof temp, "[%s] %s: %d: %s(): ",
202
 
                level, file, line, func);
 
201
                level, file, line, func);
203
202
 
204
203
    va_start (vap, ms);
205
204
    vsnprintf (p, sizeof p, ms, vap);
220
219
    ret = -1;
221
220
    if (pid < 0)
222
221
      {
223
 
        errp ("Can't fork\n");
 
222
        errp ("Can't fork\n");
224
223
      }
225
224
    else if (pid == 0)
226
225
      {
227
 
        gchar **argv;
228
 
 
229
 
        while (!isalnum (*str))
230
 
          str++;
231
 
 
232
 
        argv = g_strsplit_set (str, " \t", 0);
233
 
        if (argv == NULL)
234
 
          {
235
 
            exit (1);
236
 
          }
237
 
 
238
 
        debug ("Exec path: (%s) argument [%d]\n", argv[0],
239
 
               g_strv_length (argv));
240
 
        ret = execvp (argv[0], argv);
241
 
        g_strfreev (argv);
242
 
        errp ("Exec error\n");
 
226
        gchar **argv;
 
227
 
 
228
        while (!isalnum (*str))
 
229
          str++;
 
230
 
 
231
        argv = g_strsplit_set (str, " \t", 0);
 
232
        if (argv == NULL)
 
233
          {
 
234
            exit (1);
 
235
          }
 
236
 
 
237
        debug ("Exec path: (%s) argument [%d]\n", argv[0],
 
238
               g_strv_length (argv));
 
239
        ret = execvp (argv[0], argv);
 
240
        g_strfreev (argv);
 
241
        errp ("Exec error\n");
243
242
      }
244
243
    else
245
244
      {
246
 
        ret = wait4 (pid, &status, 0, NULL);
 
245
        ret = wait4 (pid, &status, 0, NULL);
247
246
      }
248
247
 
249
248
    return ret;