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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* ratMsgList.c --
*
* This file contains code which handles message listing
*
* TkRat software and its included text is Copyright 1996-2004 by
* Martin Forssn
*
* The full text of the legal notice is contained in the file called
* COPYRIGHT, included with this distribution.
*/
#include "ratFolder.h"
/*
*----------------------------------------------------------------------
*
* RatParseList --
*
* Parse a list expression (almost like a printf format string)
*
* Results:
* A structure representing the parsed expression, or null if
* there is a syntax error in the format string.
*
* Side effects:
* None.
*
*
*----------------------------------------------------------------------
*/
ListExpression*
RatParseList(const char *format, char *error)
{
ListExpression *expPtr;
int i, w, index, bufLen, num;
char buf[1024];
for(i=num=0; '\0' != format[i]; i++) {
if ('%' == format[i] && format[i+1] && '%' != format[i+1]) {
while (format[++i]
&& ('-' == format[i] || isdigit((unsigned char)format[i])));
if (!strchr("scnNmrRbBdDSitMu", format[i])) {
if (error != NULL) {
*error = format[i];
}
return NULL;
}
num++;
}
}
expPtr = (ListExpression*)ckalloc(sizeof(ListExpression));
expPtr->preString = (char**)ckalloc(num*sizeof(char*));
expPtr->typeList =
(RatFolderInfoType*)ckalloc(num*sizeof(RatFolderInfoType));
expPtr->fieldWidth = (int*)ckalloc(num*sizeof(int));
expPtr->leftJust = (int*)ckalloc(num*sizeof(int));
for (i = index = bufLen = 0; format[i]; i++) {
if ('%' == format[i] && format[i+1]) {
if ('%' == format[++i]) {
buf[bufLen++] = format[i];
continue;
}
buf[bufLen] = '\0';
expPtr->preString[index] = cpystr(buf);
if ('-' == format[i]) {
expPtr->leftJust[index] = 1;
i++;
} else {
expPtr->leftJust[index] = 0;
}
w=0;
while (isdigit((unsigned char)format[i])) {
w = w*10+format[i++]-'0';
}
if (!format[i]) {
break;
}
expPtr->fieldWidth[index] = w;
switch(format[i]) {
case 's': expPtr->typeList[index++] = RAT_FOLDER_SUBJECT; break;
case 'c': expPtr->typeList[index++] = RAT_FOLDER_CANONSUBJECT;
break;
case 'n': expPtr->typeList[index++] = RAT_FOLDER_NAME; break;
case 'N': expPtr->typeList[index++] = RAT_FOLDER_ANAME; break;
case 'm': expPtr->typeList[index++] = RAT_FOLDER_MAIL; break;
case 'r': expPtr->typeList[index++] = RAT_FOLDER_NAME_RECIPIENT;
break;
case 'R': expPtr->typeList[index++] = RAT_FOLDER_MAIL_RECIPIENT;
break;
case 'b': expPtr->typeList[index++] = RAT_FOLDER_SIZE; break;
case 'B': expPtr->typeList[index++] = RAT_FOLDER_SIZE_F; break;
case 'd': expPtr->typeList[index++] = RAT_FOLDER_DATE_F; break;
case 'D': expPtr->typeList[index++] = RAT_FOLDER_DATE_N; break;
case 'S': expPtr->typeList[index++] = RAT_FOLDER_STATUS; break;
case 'i': expPtr->typeList[index++] = RAT_FOLDER_INDEX; break;
case 't': expPtr->typeList[index++] = RAT_FOLDER_THREADING;break;
case 'M': expPtr->typeList[index++] = RAT_FOLDER_MSGID; break;
case 'u': expPtr->typeList[index++] = RAT_FOLDER_UID; break;
}
bufLen = 0;
} else {
buf[bufLen++] = format[i];
}
}
expPtr->size = index;
if (bufLen) {
buf[bufLen] = '\0';
expPtr->postString = cpystr(buf);
} else {
expPtr->postString = NULL;
}
return expPtr;
}
/*
*----------------------------------------------------------------------
*
* RatFreeListExpression --
*
* Frees all memory associated with a list expression.
*
* Results:
* None.
*
* Side effects:
* Some memory is freed.
*
*
*----------------------------------------------------------------------
*/
void
RatFreeListExpression(ListExpression *exPtr)
{
int i;
for (i=0; i<exPtr->size; i++) {
ckfree(exPtr->preString[i]);
}
ckfree(exPtr->preString);
ckfree(exPtr->typeList);
ckfree(exPtr->fieldWidth);
ckfree(exPtr->leftJust);
ckfree(exPtr->postString);
ckfree(exPtr);
}
/*
*----------------------------------------------------------------------
*
* RatDoList --
*
* Print the list information about a message.
*
* Results:
* A tcl object
*
* Side effects:
* None.
*
*
*----------------------------------------------------------------------
*/
Tcl_Obj*
RatDoList(Tcl_Interp *interp, ListExpression *exprPtr, RatInfoProc *infoProc,
ClientData clientData, int index)
{
Tcl_Obj *oPtr = Tcl_NewObj(), *iPtr;
char *str;
unsigned char *s2 = NULL;
int i, j, slen, length;
for (i=0; i<exprPtr->size; i++) {
if (exprPtr->preString[i]) {
Tcl_AppendToObj(oPtr, exprPtr->preString[i], -1);
}
iPtr = (*infoProc)(interp, clientData, exprPtr->typeList[i], index);
if (!iPtr) {
for (j=0; j<exprPtr->fieldWidth[i]; j++) {
Tcl_AppendToObj(oPtr, " ", 1);
}
continue;
}
str = Tcl_GetStringFromObj(iPtr, &slen);
for (j=0; j<slen && str[j] > ' '; j++);
if (j < slen) {
s2 = (unsigned char*)cpystr(str);
for (j=0; j<slen; j++) {
if (s2[j] < ' ') {
s2[j] = ' ';
}
}
str = (char*)s2;
}
if (exprPtr->fieldWidth[i]) {
length = Tcl_NumUtfChars(str, slen);
if (length > exprPtr->fieldWidth[i]) {
j = Tcl_UtfAtIndex(str, exprPtr->fieldWidth[i]) - str;
Tcl_AppendToObj(oPtr, str, j);
} else {
if (exprPtr->leftJust[i]) {
Tcl_AppendToObj(oPtr, str, slen);
for (j=length; j<exprPtr->fieldWidth[i]; j++) {
Tcl_AppendToObj(oPtr, " ", 1);
}
} else {
for (j=length; j<exprPtr->fieldWidth[i]; j++) {
Tcl_AppendToObj(oPtr, " ", 1);
}
Tcl_AppendToObj(oPtr, str, slen);
}
}
} else {
Tcl_AppendToObj(oPtr, str, slen);
}
if (s2) {
ckfree(s2);
s2 = NULL;
}
}
if (exprPtr->postString) {
Tcl_AppendToObj(oPtr, exprPtr->postString, -1);
}
return oPtr;
}
/*
*----------------------------------------------------------------------
*
* RatCheckListFormatCmd --
*
* CHeck if the given list format is correct
*
* Results:
* A tcl object
*
* Side effects:
* None.
*
*
*----------------------------------------------------------------------
*/
int
RatCheckListFormatCmd(ClientData dummy, Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[])
{
ListExpression *list;
char error, buf[1024];
Tcl_Obj *msg;
if (objc != 2) {
Tcl_AppendResult(interp, "Missing parameter", TCL_STATIC);
return TCL_ERROR;
}
list = RatParseList(Tcl_GetString(objv[1]), &error);
if (list != NULL) {
Tcl_SetResult(interp, "ok", TCL_STATIC);
RatFreeListExpression(list);
} else {
msg = Tcl_GetVar2Ex(interp, "t","illegal_list_format",TCL_GLOBAL_ONLY);
snprintf(buf, sizeof(buf), Tcl_GetString(msg), error);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
}
return TCL_OK;
}
|