pidgin/pidgin

c69c52a5ffe7
Parents 360997c25b1a
Children 564fcabca6d9
Add purple_get_month() util function

Reducing copypaste in libpurple/log.c
--- a/libpurple/log.c Wed Nov 06 04:54:32 2019 +0000
+++ b/libpurple/log.c Sat Nov 09 22:05:20 2019 +0300
@@ -1704,31 +1704,7 @@
purple_debug_warning("log", "invalid date format\n");
}
/* Ugly hack, in case current locale is not English */
- if (purple_strequal(month_str, "Jan")) {
- month = 1;
- } else if (purple_strequal(month_str, "Feb")) {
- month = 2;
- } else if (purple_strequal(month_str, "Mar")) {
- month = 3;
- } else if (purple_strequal(month_str, "Apr")) {
- month = 4;
- } else if (purple_strequal(month_str, "May")) {
- month = 5;
- } else if (purple_strequal(month_str, "Jun")) {
- month = 6;
- } else if (purple_strequal(month_str, "Jul")) {
- month = 7;
- } else if (purple_strequal(month_str, "Aug")) {
- month = 8;
- } else if (purple_strequal(month_str, "Sep")) {
- month = 9;
- } else if (purple_strequal(month_str, "Oct")) {
- month = 10;
- } else if (purple_strequal(month_str, "Nov")) {
- month = 11;
- } else if (purple_strequal(month_str, "Dec")) {
- month = 12;
- }
+ month = purple_get_month(month_str);
if (lasttime)
g_date_time_unref(lasttime);
lasttime = g_date_time_new_local(year, month, day,
--- a/libpurple/plugins/log_reader.c Wed Nov 06 04:54:32 2019 +0000
+++ b/libpurple/plugins/log_reader.c Sat Nov 09 22:05:20 2019 +0300
@@ -32,19 +32,6 @@
NAME_GUESS_THEM
};
-/* Some common functions. */
-static int get_month(const char *month)
-{
- int iter;
- const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
- for (iter = 0; months[iter]; iter++) {
- if (purple_strequal(month, months[iter]))
- break;
- }
- return iter;
-}
-
/*****************************************************************************
* Adium Logger *
@@ -1222,7 +1209,7 @@
} else {
PurpleLog *log;
- month = get_month(month_str);
+ month = purple_get_month(month_str);
data = g_new0(
struct trillian_logger_data, 1);
@@ -2026,7 +2013,7 @@
"Error parsing start date for %s\n",
filename);
} else {
- month = get_month(month_str);
+ month = purple_get_month(month_str);
found_start = TRUE;
offset = c - contents;
--- a/libpurple/util.c Wed Nov 06 04:54:32 2019 +0000
+++ b/libpurple/util.c Sat Nov 09 22:05:20 2019 +0300
@@ -561,6 +561,20 @@
return retval;
}
+gint purple_get_month(const char *month_abbr)
+{
+ const char *months[] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+ NULL};
+ for (gint month = 0; months[month] != NULL; month++) {
+ if (purple_strequal(month_abbr, months[month])) {
+ return month + 1;
+ }
+ }
+ return 0;
+}
+
/**************************************************************************
* Markup Functions
**************************************************************************/
--- a/libpurple/util.h Wed Nov 06 04:54:32 2019 +0000
+++ b/libpurple/util.h Sat Nov 09 22:05:20 2019 +0300
@@ -314,6 +314,17 @@
*/
GDateTime *purple_str_to_date_time(const char *timestamp, gboolean utc);
+/**
+ * purple_get_month:
+ * @month_abbr: The 3-letter month abbreviation
+ *
+ * Get month number suitable for GDateTime. If @month_abbr is unknown,
+ * returns 0.
+ *
+ * Returns: A month number or 0.
+ */
+gint purple_get_month(const char *month_abbr);
+
/**************************************************************************/
/* Markup Functions */
/**************************************************************************/