~ubuntu-branches/ubuntu/trusty/hime/trusty

« back to all changes in this revision

Viewing changes to src/util.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#include "hime.h"
 
19
#include <errno.h>
 
20
 
 
21
#if !CLIENT_LIB || DEBUG
 
22
static FILE *out_fp;
 
23
#endif
 
24
 
 
25
void p_err(char *fmt,...)
 
26
{
 
27
  va_list args;
 
28
  char out[4096];
 
29
 
 
30
  va_start(args, fmt);
 
31
  vsprintf(out, fmt, args);
 
32
  va_end(args);
 
33
 
 
34
#if CLIENT_LIB
 
35
  fprintf(stderr, "%s\n", out);
 
36
#else
 
37
  if (getenv("NO_GTK_INIT"))
 
38
    fprintf(stderr, "%s\n", out);
 
39
  else {
 
40
    GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
 
41
                                     GTK_MESSAGE_ERROR,
 
42
                                     GTK_BUTTONS_CLOSE,
 
43
                                     "%s", out);
 
44
 
 
45
    gtk_dialog_run (GTK_DIALOG (dialog));
 
46
    gtk_widget_destroy (dialog);
 
47
  }
 
48
#endif
 
49
 
 
50
#if DEBUG && 1
 
51
  abort();
 
52
#else
 
53
  if (getenv("HIME_ERR_COREDUMP"))
 
54
    abort();
 
55
  exit(-1);
 
56
#endif
 
57
}
 
58
 
 
59
#if !CLIENT_LIB || DEBUG
 
60
static void init_out_fp()
 
61
{
 
62
  if (!out_fp) {
 
63
    if (getenv("HIME_DBG_TMP") || 0) {
 
64
      char fname[64];
 
65
      sprintf(fname, "/tmp/himedbg-%d-%d", getuid(), getpid());
 
66
      out_fp = fopen(fname, "w");
 
67
    }
 
68
 
 
69
    if (!out_fp)
 
70
      out_fp = stdout;
 
71
  }
 
72
}
 
73
#endif
 
74
 
 
75
#if !CLIENT_LIB
 
76
void dbg_time(char *fmt,...)
 
77
{
 
78
  va_list args;
 
79
  time_t t;
 
80
 
 
81
  init_out_fp();
 
82
 
 
83
  time(&t);
 
84
  struct tm *ltime = localtime(&t);
 
85
  dbg("%02d:%02d:%02d ", ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
 
86
 
 
87
  va_start(args, fmt);
 
88
  vfprintf(out_fp, fmt, args);
 
89
  fflush(out_fp);
 
90
  va_end(args);
 
91
}
 
92
#endif
 
93
 
 
94
#if DEBUG
 
95
void __hime_dbg_(char *fmt,...)
 
96
{
 
97
  va_list args;
 
98
 
 
99
  init_out_fp();
 
100
 
 
101
  va_start(args, fmt);
 
102
  vfprintf(out_fp, fmt, args);
 
103
  fflush(out_fp);
 
104
  va_end(args);
 
105
}
 
106
#endif
 
107
 
 
108
char *sys_err_strA()
 
109
{
 
110
  return (char *)strerror(errno);
 
111
}
 
112
 
 
113
void *zmalloc(int n)
 
114
{
 
115
  void *p =  malloc(n);
 
116
  bzero(p, n);
 
117
  return p;
 
118
}
 
119
#if !HIME_IME
 
120
 
 
121
void *memdup(void *p, int n)
 
122
{
 
123
  if (!p || !n)
 
124
    return NULL;
 
125
  void *q;
 
126
  q = malloc(n);
 
127
  memcpy(q, p, n);
 
128
  return q;
 
129
}
 
130
 
 
131
// can handle eol with \n \r \n\r \r\n
 
132
char *myfgets(char *buf, int bufN, FILE *fp)
 
133
{
 
134
        char *out = buf;
 
135
//      int rN = 0;
 
136
        while (!feof(fp) && out - buf < bufN) {
 
137
                char a, b;
 
138
                a = 0;
 
139
                if (fread(&a, 1, 1, fp) != 1)
 
140
                        break;
 
141
                if (a =='\n') {
 
142
                        b = 0;
 
143
                        if (fread(&b, 1, 1, fp)==1)
 
144
                                if (b!='\r')
 
145
                                        fseek(fp, -1, SEEK_CUR);
 
146
                        break;
 
147
                } else
 
148
                if (a =='\r') {
 
149
                        b = 0;
 
150
                        if (fread(&b, 1, 1, fp)==1)
 
151
                                if (b!='\n')
 
152
                                        fseek(fp, -1, SEEK_CUR);
 
153
                        break;
 
154
                }
 
155
 
 
156
                *(out++) = a;
 
157
        }
 
158
 
 
159
        *out = 0;
 
160
        return buf;
 
161
}
 
162
#endif