comparison mcabber/src/hbuf.c @ 1485:0121b6f3047c

New command: /buffer save
author Mikael Berthe <mikael@lilotux.net>
date Sun, 20 Apr 2008 16:06:37 +0200
parents bb1cc8902d0e
children f83a51eaa5ed
comparison
equal deleted inserted replaced
1484:7b36b91a4388 1485:0121b6f3047c
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA 19 * USA
20 */ 20 */
21 21
22 #include <string.h> 22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
23 26
24 #include "hbuf.h" 27 #include "hbuf.h"
25 #include "utils.h" 28 #include "utils.h"
26 #include "utf8.h" 29 #include "utf8.h"
30 #include "screen.h"
27 31
28 32
29 /* This is a private structure type */ 33 /* This is a private structure type */
30 34
31 typedef struct { 35 typedef struct {
327 array_elt = array; 331 array_elt = array;
328 332
329 for (i = 0 ; i < n ; i++) { 333 for (i = 0 ; i < n ; i++) {
330 if (hbuf) { 334 if (hbuf) {
331 int maxlen; 335 int maxlen;
336
332 blk = (hbuf_block*)(hbuf->data); 337 blk = (hbuf_block*)(hbuf->data);
333 maxlen = blk->ptr_end - blk->ptr; 338 maxlen = blk->ptr_end - blk->ptr;
334 *array_elt = (hbb_line*)g_new(hbb_line, 1); 339 *array_elt = (hbb_line*)g_new(hbb_line, 1);
335 (*array_elt)->timestamp = blk->prefix.timestamp; 340 (*array_elt)->timestamp = blk->prefix.timestamp;
336 (*array_elt)->flags = blk->prefix.flags; 341 (*array_elt)->flags = blk->prefix.flags;
342 } else { 347 } else {
343 // Propagate highlighting flags 348 // Propagate highlighting flags
344 (*array_elt)->flags |= last_persist_prefixflags & 349 (*array_elt)->flags |= last_persist_prefixflags &
345 (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT | 350 (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
346 HBB_PREFIX_INFO | HBB_PREFIX_IN); 351 HBB_PREFIX_INFO | HBB_PREFIX_IN);
347 //Continuation of a message - omit the prefix 352 // Continuation of a message - omit the prefix
348 (*array_elt)->flags |= HBB_PREFIX_CONT; 353 (*array_elt)->flags |= HBB_PREFIX_CONT;
349 (*array_elt)->mucnicklen = 0;//The nick is in the first one 354 (*array_elt)->mucnicklen = 0; // The nick is in the first one
350 } 355 }
351 356
352 hbuf = g_list_next(hbuf); 357 hbuf = g_list_next(hbuf);
353 } else 358 } else
354 break; 359 break;
410 hlen = g_list_length(hbuf); 415 hlen = g_list_length(hbuf);
411 416
412 return g_list_nth(hbuf, pc*hlen/100); 417 return g_list_nth(hbuf, pc*hlen/100);
413 } 418 }
414 419
420 // hbuf_dump_to_file(hbuf, filename)
421 // Save the buffer to a file.
422 void hbuf_dump_to_file(GList *hbuf, const char *filename)
423 {
424 hbuf_block *blk;
425 hbb_line line;
426 guint last_persist_prefixflags;
427 char pref[96];
428 FILE *fp;
429 struct stat statbuf;
430
431 if (!stat(filename, &statbuf)) {
432 scr_LogPrint(LPRINT_NORMAL, "The file already exists.");
433 return;
434 }
435 fp = fopen(filename, "w");
436 if (!fp) {
437 scr_LogPrint(LPRINT_NORMAL, "Unable to open the file.");
438 return;
439 }
440
441 for (hbuf = g_list_first(hbuf); hbuf; hbuf = g_list_next(hbuf)) {
442 int maxlen;
443
444 blk = (hbuf_block*)(hbuf->data);
445 maxlen = blk->ptr_end - blk->ptr;
446
447 memset(&line, 0, sizeof(line));
448 line.timestamp = blk->prefix.timestamp;
449 line.flags = blk->prefix.flags;
450 line.mucnicklen = blk->prefix.mucnicklen;
451 line.text = g_strndup(blk->ptr, maxlen);
452
453 if ((blk->flags & HBB_FLAG_PERSISTENT) && blk->prefix.flags) {
454 last_persist_prefixflags = blk->prefix.flags;
455 } else {
456 // Propagate highlighting flags
457 line.flags |= last_persist_prefixflags &
458 (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
459 HBB_PREFIX_INFO | HBB_PREFIX_IN);
460 // Continuation of a message - omit the prefix
461 line.flags |= HBB_PREFIX_CONT;
462 line.mucnicklen = 0; // The nick is in the first one
463 }
464
465 scr_line_prefix(&line, pref, sizeof pref);
466 fprintf(fp, "%s%s\n", pref, line.text);
467 }
468
469 fclose(fp);
470 return;
471 }
472
415 // hbuf_get_blocks_number() 473 // hbuf_get_blocks_number()
416 // Returns the number of allocated hbuf_block's. 474 // Returns the number of allocated hbuf_block's.
417 guint hbuf_get_blocks_number(GList *hbuf) 475 guint hbuf_get_blocks_number(GList *hbuf)
418 { 476 {
419 hbuf_block *hbuf_b_elt; 477 hbuf_block *hbuf_b_elt;