Mercurial > pidgin
comparison libpurple/plugins/log_reader.c @ 17531:21773944db4b
Don't create the temp GString unless it's actually needed, and avoid
doing an strlen() there unless that's really needed.
| author | Richard Laager <rlaager@wiktel.com> |
|---|---|
| date | Thu, 07 Jun 2007 05:28:05 +0000 |
| parents | a6594c34635b |
| children | c8d2e131cc37 c0cd4d84ba52 |
comparison
equal
deleted
inserted
replaced
| 17530:a6594c34635b | 17531:21773944db4b |
|---|---|
| 1465 line = read; | 1465 line = read; |
| 1466 while (c) | 1466 while (c) |
| 1467 { | 1467 { |
| 1468 const char *link; | 1468 const char *link; |
| 1469 const char *footer = NULL; | 1469 const char *footer = NULL; |
| 1470 GString *temp; | 1470 GString *temp = NULL; |
| 1471 | 1471 |
| 1472 c = strstr(c, "\n"); | 1472 if ((c = strstr(c, "\n"))) |
| 1473 | 1473 { |
| 1474 if (c) { | |
| 1475 *c = '\0'; | 1474 *c = '\0'; |
| 1476 ++c; | 1475 c++; |
| 1477 } | 1476 } |
| 1478 | 1477 |
| 1479 /* Convert links. | 1478 /* Convert links. |
| 1480 * | 1479 * |
| 1481 * The format is (Link: URL)URL | 1480 * The format is (Link: URL)URL |
| 1486 * Then, replace the next " " (or add this if the end-of-line is reached) with: | 1485 * Then, replace the next " " (or add this if the end-of-line is reached) with: |
| 1487 * </a> | 1486 * </a> |
| 1488 * | 1487 * |
| 1489 * As implemented, this isn't perfect, but it should cover common cases. | 1488 * As implemented, this isn't perfect, but it should cover common cases. |
| 1490 */ | 1489 */ |
| 1491 temp = g_string_sized_new(strlen(line)); | |
| 1492 while (line && (link = strstr(line, "(Link: "))) | 1490 while (line && (link = strstr(line, "(Link: "))) |
| 1493 { | 1491 { |
| 1494 const char *tmp = link; | 1492 const char *tmp = link; |
| 1495 | 1493 |
| 1496 link += 7; | 1494 link += 7; |
| 1502 if (!(end_paren = strstr(link, ")"))) | 1500 if (!(end_paren = strstr(link, ")"))) |
| 1503 { | 1501 { |
| 1504 /* Something is not as we expect. Bail out. */ | 1502 /* Something is not as we expect. Bail out. */ |
| 1505 break; | 1503 break; |
| 1506 } | 1504 } |
| 1505 | |
| 1506 if (!temp) | |
| 1507 temp = g_string_sized_new(c ? (c - 1 - line) : strlen(line)); | |
| 1507 | 1508 |
| 1508 g_string_append_len(temp, line, (tmp - line)); | 1509 g_string_append_len(temp, line, (tmp - line)); |
| 1509 | 1510 |
| 1510 /* Start an <a> tag. */ | 1511 /* Start an <a> tag. */ |
| 1511 g_string_append(temp, "<a href=\""); | 1512 g_string_append(temp, "<a href=\""); |
| 1541 /* Something is not as we expect. Bail out. */ | 1542 /* Something is not as we expect. Bail out. */ |
| 1542 break; | 1543 break; |
| 1543 } | 1544 } |
| 1544 } | 1545 } |
| 1545 | 1546 |
| 1546 if (line) { | 1547 if (temp) |
| 1547 g_string_append(temp, line); | 1548 { |
| 1548 } | 1549 if (line) |
| 1549 line = temp->str; | 1550 g_string_append(temp, line); |
| 1551 line = temp->str; | |
| 1552 } | |
| 1550 | 1553 |
| 1551 if (*line == '[') { | 1554 if (*line == '[') { |
| 1552 const char *timestamp; | 1555 const char *timestamp; |
| 1553 | 1556 |
| 1554 if ((timestamp = strstr(line, "]"))) { | 1557 if ((timestamp = strstr(line, "]"))) { |
| 1670 } | 1673 } |
| 1671 } | 1674 } |
| 1672 | 1675 |
| 1673 g_string_append(formatted, line); | 1676 g_string_append(formatted, line); |
| 1674 | 1677 |
| 1678 line = c; | |
| 1679 if (temp) | |
| 1680 g_string_free(temp, TRUE); | |
| 1681 | |
| 1675 if (footer) | 1682 if (footer) |
| 1676 g_string_append(formatted, footer); | 1683 g_string_append(formatted, footer); |
| 1677 | 1684 |
| 1678 g_string_append_c(formatted, '\n'); | 1685 g_string_append_c(formatted, '\n'); |
| 1679 | |
| 1680 g_string_free(temp, TRUE); | |
| 1681 | |
| 1682 line = c; | |
| 1683 } | 1686 } |
| 1684 | 1687 |
| 1685 g_free(read); | 1688 g_free(read); |
| 1686 read = formatted->str; | 1689 /* XXX: TODO: Avoid this g_strchomp() */ |
| 1687 g_string_free(formatted, FALSE); | 1690 return g_strchomp(g_string_free(formatted, FALSE)); |
| 1688 | |
| 1689 return read; | |
| 1690 } | 1691 } |
| 1691 | 1692 |
| 1692 static int trillian_logger_size (PurpleLog *log) | 1693 static int trillian_logger_size (PurpleLog *log) |
| 1693 { | 1694 { |
| 1694 struct trillian_logger_data *data; | 1695 struct trillian_logger_data *data; |
