File: pytype_basics.c

package info (click to toggle)
jppy 0.0.47-1.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,188 kB
  • ctags: 4,558
  • sloc: ansic: 51,314; python: 5,949; yacc: 1,303; makefile: 735; sh: 555; sed: 10
file content (248 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download
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
#include <Python.h>
#include "pytype_contacts.h"
#include "pytype_basics.h"

/* Here we're using ContactObject as if it were a parent class for all
   TodoObject, et. al. */

void SetBasicRecordObjectAttributeDefaults(PyObject *self, int (*packer)( )) {
  ((PyPiContact*)self)->saved_br.size = 0;
  ((PyPiContact*)self)->saved_br.buf = NULL;
  ((PyPiContact*)self)->deleted = 0;
  ((PyPiContact*)self)->modified = 0;
  ((PyPiContact*)self)->busy = 0;
  ((PyPiContact*)self)->secret = 0;
  ((PyPiContact*)self)->archived = 0;
  ((PyPiContact*)self)->category = 0;
  ((PyPiContact*)self)->unique_id = 0;
  ((PyPiContact*)self)->unsaved_changes = 0;
  ((PyPiContact*)self)->packer = packer;
}

void SetSavedBrAndRTandUniqueIDandAttribs(PCRecType rt, unsigned int unique_id, 
						 unsigned char attrib, int size, 
						 void *buf, PyObject *self) {
  ((PyPiContact*)self)->saved_br.size = size;
  ((PyPiContact*)self)->saved_br.attrib = attrib;
  ((PyPiContact*)self)->saved_br.rt = ((PyPiContact*)self)->rt = rt;
  ((PyPiContact*)self)->saved_br.unique_id = ((PyPiContact*)self)->unique_id = unique_id;
  ((PyPiContact*)self)->saved_br.buf = malloc(size);
  memcpy(((PyPiContact*)self)->saved_br.buf,buf,size);

  ((PyPiContact*)self)->deleted = (attrib & 0x80) ? 1 : 0;
  ((PyPiContact*)self)->modified = (attrib & 0x40) ? 1 : 0;
  ((PyPiContact*)self)->busy = (attrib & 0x20) ? 1 : 0;
  ((PyPiContact*)self)->secret = (attrib & 0x10) ? 1 : 0;

  /* http://cvs.coldsync.org/cgi-bin/viewcvs.cgi/coldsync/libpdb/
     pdb.c?rev=1.44&content-type=text/vnd.viewcvs-markup 
     The archived bit is only set, if deleted is set. Otherwise,
     the bit is used as part of the category */

  if (((PyPiContact*)self)->deleted) {
    ((PyPiContact*)self)->archived = (attrib & 0x08) ? 1 : 0;
    ((PyPiContact*)self)->category = 0;
  } else {
    ((PyPiContact*)self)->archived = 0;
    ((PyPiContact*)self)->category = (attrib & 0x0F);
    
  }
}

char *FriendlyNameForRecordType(PCRecType rt) {
  switch(rt) {
  case PALM_REC: 
    return "normal";
  case MODIFIED_PALM_REC:
    return "modified";
  case DELETED_PALM_REC:
    return "deleted";
  case NEW_PC_REC:
    return "new_pc";
  case DELETED_PC_REC:
    return "deleted_pc";
  case REPLACEMENT_PALM_REC:
    return "replacement_rec";
  case DELETED_DELETED_PALM_REC:
    return "deleted_palm";
  default:
    return "unknown";
  }
}

PyObject *Attribute_Repr(PyObject* self) {
  static PyObject *format = NULL;
  PyObject *args, *result;

  if (format == NULL) {
    format = PyString_FromString("d:%d m:%d b:%d s:%d a:%d cat:%d type:%s uid=%d");
    if (format == NULL)
      return NULL;
  }

  args = Py_BuildValue("iiiiiisi", 
		       ((PyPiContact*)self)->deleted,
		       ((PyPiContact*)self)->modified,
		       ((PyPiContact*)self)->busy,
		       ((PyPiContact*)self)->secret,
		       ((PyPiContact*)self)->archived,
		       ((PyPiContact*)self)->category,
		       FriendlyNameForRecordType(((PyPiContact*)self)->rt),
		       ((PyPiContact*)self)->unique_id);  
  
  if (args == NULL)
    return NULL;

  result = PyString_Format(format, args);
  Py_DECREF(args);
  return result;
}


PyObject * PyPi_Getbool(PyObject *self, void *closure) {
  void *addr = (void *)self + (size_t) closure;
  if (*(int *)(addr)) {
    Py_INCREF(Py_True);
    return Py_True;  
  } else {
    Py_INCREF(Py_False);
    return Py_False;  
  }
}

PyObject * PyPi_GetCategory(PyObject *self, void *closure) {
  void *addr = (void *)self + (size_t) closure;
  return PyInt_FromLong(*(long *)addr);
}

int PyPi_Setbool(PyObject *self, PyObject *value, void *closure) {
  void *addr = (void *)self + (size_t) closure;

  if (value == NULL) {
    PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
    return -1;
  }
  
  if (!(PyBool_Check(value) || PyInt_Check(value))) {
    PyErr_SetString(PyExc_TypeError, "The attribute value must be a boolean");
    return -1;
  }

  if (PyInt_Check(value) && PyInt_AsLong(value)) {
    *(int*)addr = 1;
  } else if (PyBool_Check(value) && (value == Py_True)) {
    *(int*)addr = 1;
  } else {
    *(int*)addr = 0;
  }

  return 0;
}

int PyPi_SetCategory(PyObject *self, PyObject *value, void *closure) {
  void *addr = (void *)self + (size_t) closure;
  int category;
  if (value == NULL) {
    PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
    return -1;
  }
  
  if (!PyInt_Check(value)) {
    PyErr_SetString(PyExc_TypeError, "The category must be an integer");
    return -1;
  }
  
  category = PyInt_AsLong(value);
  if ((category >= 0) && (category <= 15)) {
    *(int*)addr = category;
  } else {
    PyErr_SetString(PyExc_ValueError, "The category value must be 0 <= x <= 15");
    return -1;
  }

  return 0;
}


VObject *safeAddPropValue(VObject *o, const char *id, char *val) {
  VObject *vo = NULL;
  unsigned int charsetflag = 0;
  unsigned int qpflag = 0;
  unsigned int n = 0;

  if (val) {
    GString *val_copy = g_string_new(val);
    for (n=0; ; n++) {
      if (val[n] == 0)
	break;
      if ((unsigned char)(val[n]) > 127) {
	qpflag = 1;
	charsetflag = 1;
      };
      if ((unsigned char)(val[n]) == '\n')
	qpflag = 1;
    };
    if (qpflag) {
      for (n=0; n < val_copy->len ; n++) {
	if ((unsigned char)(val_copy->str[n]) == '\n') {
	  g_string_insert_c(val_copy, n, '\r');
	  n++;
	}
      };
    };
    vo = addPropValue(o, id, val_copy->str);
    g_string_free(val_copy,1);
    if (charsetflag) {
      if (isAPropertyOf(vo,"CHARSET") == NULL)
	addPropValue(vo,"CHARSET","cp1252");
    }
    if (qpflag) {
      if (isAPropertyOf(vo,VCEncodingProp) == NULL)
      addPropValue(vo,VCEncodingProp,VCQuotedPrintableProp);
    }
  }

  return vo;
}

// adds the properties to the original VObject
VObject *safeAddPropValuePart(VObject *o, const char *id, char *val) {
  VObject *vo = NULL;
  unsigned int charsetflag = 0;
  unsigned int qpflag = 0;
  unsigned int n = 0;

  if (val) {
    GString *val_copy = g_string_new(val);
    for (n=0; ; n++) {
      if (val[n] == 0)
	break;
      if ((unsigned char)(val[n]) > 127) {
	qpflag = 1;
	charsetflag = 1;
      };
      if ((unsigned char)(val[n]) == '\n')
	qpflag = 1;
    };
    if (qpflag) {
      for (n=0; n < val_copy->len ; n++) {
	if ((unsigned char)(val_copy->str[n]) == '\n') {
	  g_string_insert_c(val_copy, n, '\r');
	  n++;
	}
      };
    };
    vo = addPropValue(o, id, val_copy->str);
    g_string_free(val_copy,1);
    if (charsetflag) {
      if (isAPropertyOf(o,"CHARSET") == NULL)
	addPropValue(o,"CHARSET","cp1252");
    }
    if (qpflag) {
      if (isAPropertyOf(o,VCEncodingProp) == NULL)
      addPropValue(o,VCEncodingProp,VCQuotedPrintableProp);
    }
  }

  return vo;
}