Sun, 29 Apr 2007 01:08:11 +0000
Fix a bug with the bugfix for empty icons. The logic was backwards here.
This caused all buddy icons to be lost on migration and as an added bonus,
it failed to solve the empty icon NULL extension bug!
| 13201 | 1 | /* |
|
14013
86dac5633bd9
[gaim-migrate @ 16496]
Mark Doliner <markdoliner@pidgin.im>
parents:
13830
diff
changeset
|
2 | * @file circbuffer.h Buffer Utility Functions |
| 13201 | 3 | * @ingroup core |
| 4 | * | |
| 15884 | 5 | * Purple is the legal property of its developers, whose names are too numerous |
| 13201 | 6 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 7 | * source distribution. | |
| 8 | * | |
| 9 | * This program is free software; you can redistribute it and/or modify | |
| 10 | * it under the terms of the GNU General Public License as published by | |
| 11 | * the Free Software Foundation; either version 2 of the License, or | |
| 12 | * (at your option) any later version. | |
| 13 | * | |
| 14 | * This program is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU General Public License | |
| 20 | * along with this program; if not, write to the Free Software | |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | */ | |
| 23 | #include "internal.h" | |
| 24 | ||
|
14013
86dac5633bd9
[gaim-migrate @ 16496]
Mark Doliner <markdoliner@pidgin.im>
parents:
13830
diff
changeset
|
25 | #include "circbuffer.h" |
| 13201 | 26 | |
| 27 | #define DEFAULT_BUF_SIZE 256 | |
| 28 | ||
| 15884 | 29 | PurpleCircBuffer * |
| 30 | purple_circ_buffer_new(gsize growsize) { | |
| 31 | PurpleCircBuffer *buf = g_new0(PurpleCircBuffer, 1); | |
| 13201 | 32 | buf->growsize = growsize ? growsize : DEFAULT_BUF_SIZE; |
| 33 | return buf; | |
| 34 | } | |
| 35 | ||
| 15884 | 36 | void purple_circ_buffer_destroy(PurpleCircBuffer *buf) { |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
37 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
38 | |
| 13201 | 39 | g_free(buf->buffer); |
| 40 | g_free(buf); | |
| 41 | } | |
| 42 | ||
| 15884 | 43 | static void grow_circ_buffer(PurpleCircBuffer *buf, gsize len) { |
| 13201 | 44 | int in_offset = 0, out_offset = 0; |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
45 | int start_buflen; |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
46 | |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
47 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
48 | |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
49 | start_buflen = buf->buflen; |
| 13201 | 50 | |
| 51 | while ((buf->buflen - buf->bufused) < len) | |
| 52 | buf->buflen += buf->growsize; | |
| 53 | ||
| 54 | if (buf->inptr != NULL) { | |
| 55 | in_offset = buf->inptr - buf->buffer; | |
| 56 | out_offset = buf->outptr - buf->buffer; | |
| 57 | } | |
| 58 | buf->buffer = g_realloc(buf->buffer, buf->buflen); | |
| 59 | ||
| 60 | /* adjust the fill and remove pointer locations */ | |
| 61 | if (buf->inptr == NULL) { | |
| 62 | buf->inptr = buf->outptr = buf->buffer; | |
| 63 | } else { | |
| 64 | buf->inptr = buf->buffer + in_offset; | |
| 65 | buf->outptr = buf->buffer + out_offset; | |
| 66 | } | |
| 67 | ||
| 68 | /* If the fill pointer is wrapped to before the remove | |
| 69 | * pointer, we need to shift the data */ | |
| 70 | if (in_offset < out_offset) { | |
| 71 | int shift_n = MIN(buf->buflen - start_buflen, | |
| 72 | in_offset); | |
| 73 | memcpy(buf->buffer + start_buflen, buf->buffer, | |
| 74 | shift_n); | |
| 75 | ||
| 76 | /* If we couldn't fit the wrapped read buffer | |
| 77 | * at the end */ | |
| 78 | if (shift_n < in_offset) { | |
|
13210
15dcfe11860e
[gaim-migrate @ 15573]
Daniel Atallah <datallah@pidgin.im>
parents:
13201
diff
changeset
|
79 | memmove(buf->buffer, |
| 13201 | 80 | buf->buffer + shift_n, |
| 81 | in_offset - shift_n); | |
| 82 | buf->inptr = buf->buffer + | |
| 83 | (in_offset - shift_n); | |
| 84 | } else { | |
| 85 | buf->inptr = buf->buffer + | |
| 86 | start_buflen + in_offset; | |
| 87 | } | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 15884 | 91 | void purple_circ_buffer_append(PurpleCircBuffer *buf, gconstpointer src, gsize len) { |
| 13201 | 92 | |
| 93 | int len_stored; | |
| 94 | ||
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
95 | g_return_if_fail(buf != NULL); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
96 | |
| 13201 | 97 | /* Grow the buffer, if necessary */ |
| 98 | if ((buf->buflen - buf->bufused) < len) | |
| 99 | grow_circ_buffer(buf, len); | |
| 100 | ||
|
13213
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
101 | /* If there is not enough room to copy all of src before hitting |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
102 | * the end of the buffer then we will need to do two copies. |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
103 | * One copy from inptr to the end of the buffer, and the |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
104 | * second copy from the start of the buffer to the end of src. */ |
|
8dcf2385a862
[gaim-migrate @ 15576]
Mark Doliner <markdoliner@pidgin.im>
parents:
13210
diff
changeset
|
105 | if (buf->inptr >= buf->outptr) |
| 13201 | 106 | len_stored = MIN(len, buf->buflen |
| 107 | - (buf->inptr - buf->buffer)); | |
| 108 | else | |
| 109 | len_stored = len; | |
| 110 | ||
| 111 | memcpy(buf->inptr, src, len_stored); | |
| 112 | ||
| 113 | if (len_stored < len) { | |
|
15017
2264f54d2342
[gaim-migrate @ 17734]
Luke Schierer <lschiere@pidgin.im>
parents:
14254
diff
changeset
|
114 | memcpy(buf->buffer, (char*)src + len_stored, len - len_stored); |
| 13201 | 115 | buf->inptr = buf->buffer + (len - len_stored); |
| 116 | } else if ((buf->buffer - buf->inptr) == len_stored) { | |
| 117 | buf->inptr = buf->buffer; | |
| 118 | } else { | |
| 119 | buf->inptr += len_stored; | |
| 120 | } | |
| 121 | ||
| 122 | buf->bufused += len; | |
| 123 | } | |
| 124 | ||
| 15884 | 125 | gsize purple_circ_buffer_get_max_read(const PurpleCircBuffer *buf) { |
|
15609
e432251bd57e
sf patch #1647731, from Markus Elfring
Mark Doliner <markdoliner@pidgin.im>
parents:
15435
diff
changeset
|
126 | gsize max_read; |
| 13201 | 127 | |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
128 | g_return_val_if_fail(buf != NULL, 0); |
|
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
129 | |
| 13201 | 130 | if (buf->bufused == 0) |
| 131 | max_read = 0; | |
| 132 | else if ((buf->outptr - buf->inptr) >= 0) | |
| 133 | max_read = buf->buflen - (buf->outptr - buf->buffer); | |
| 134 | else | |
| 135 | max_read = buf->inptr - buf->outptr; | |
| 136 | ||
| 137 | return max_read; | |
| 138 | } | |
| 139 | ||
| 15884 | 140 | gboolean purple_circ_buffer_mark_read(PurpleCircBuffer *buf, gsize len) { |
|
15359
4317e3cb888e
[gaim-migrate @ 18088]
Evan Schoenberg <evands@pidgin.im>
parents:
15017
diff
changeset
|
141 | g_return_val_if_fail(buf != NULL, FALSE); |
| 15884 | 142 | g_return_val_if_fail(purple_circ_buffer_get_max_read(buf) >= len, FALSE); |
| 13201 | 143 | |
| 144 | buf->outptr += len; | |
| 145 | buf->bufused -= len; | |
| 146 | /* wrap to the start if we're at the end */ | |
| 147 | if ((buf->outptr - buf->buffer) == buf->buflen) | |
| 148 | buf->outptr = buf->buffer; | |
| 149 | ||
| 150 | return TRUE; | |
| 151 | } | |
| 152 |