diff --git a/lib/mu-flags.c b/lib/mu-flags.c index 4c66002c..bd9ee667 100644 --- a/lib/mu-flags.c +++ b/lib/mu-flags.c @@ -1,5 +1,5 @@ /* -** Copyright (C) 2011 +** Copyright (C) 2011-2012 ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -16,6 +16,8 @@ ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** */ + +#include #include "mu-flags.h" struct _FlagInfo { @@ -191,6 +193,36 @@ mu_flags_from_str (const char *str, MuFlagType types, +char* +mu_flags_custom_from_str (const char *str) +{ + char *custom; + const char* cur; + unsigned u; + + g_return_val_if_fail (str, NULL); + + for (cur = str, u = 0, custom = NULL; *cur; ++cur) { + + MuFlags flag; + flag = mu_flag_from_char (*cur); + + /* if it's a valid file flag, ignore it */ + if (flag != MU_FLAG_INVALID && + mu_flag_type (flag) == MU_FLAG_TYPE_MAILFILE) + continue; + + /* otherwise, add it to our custom string */ + if (!custom) + custom = g_new0 (char, strlen(str) + 1); + custom[u++] = *cur; + } + + return custom; +} + + + void mu_flags_foreach (MuFlagsForeachFunc func, gpointer user_data) { diff --git a/lib/mu-flags.h b/lib/mu-flags.h index 62020d98..3bc0205b 100644 --- a/lib/mu-flags.h +++ b/lib/mu-flags.h @@ -114,7 +114,7 @@ const char* mu_flags_to_str_s (MuFlags flags, MuFlagType types); /** * Get the (OR'ed) flags corresponding to a string representation * - * @param str the string representation + * @param str the file info string * @param types the flag types to accept (other will be ignored) * @param ignore invalid if TRUE, ignore invalid flags, otherwise return * MU_FLAG_INVALID if an invalid flag is encountered @@ -124,6 +124,19 @@ const char* mu_flags_to_str_s (MuFlags flags, MuFlagType types); MuFlags mu_flags_from_str (const char *str, MuFlagType types, gboolean ignore_invalid); + +/** + * return the concatenation of all non-standard file flags in str + * (ie., characters other than DFPRST) as a newly allocated string. + * + * @param str the file info string + * + * @return concatenation of all non-standard flags, as a string; free + * with g_free when done. If there are no such flags, return NULL. + */ +char* mu_flags_custom_from_str (const char *str) G_GNUC_WARN_UNUSED_RESULT; + + /** * Update #oldflags with the flags in #str, where #str consists of the * the normal flag characters, but prefixed with either '+' or '-', @@ -141,8 +154,6 @@ MuFlags mu_flags_from_str_delta (const char *str, MuFlags oldflags, MuFlagType types); - - typedef void (*MuFlagsForeachFunc) (MuFlags flag, gpointer user_data); /** diff --git a/lib/tests/test-mu-flags.c b/lib/tests/test-mu-flags.c index 464f959e..6296b6f8 100644 --- a/lib/tests/test-mu-flags.c +++ b/lib/tests/test-mu-flags.c @@ -137,11 +137,33 @@ test_mu_flags_from_str_delta (void) MU_FLAG_SIGNED | MU_FLAG_DRAFT, MU_FLAG_TYPE_ANY),==, MU_FLAG_PASSED | MU_FLAG_SEEN | MU_FLAG_SIGNED); +} - /* g_assert_cmpuint (mu_flags_from_str_delta ("foobar", */ - /* MU_FLAG_INVALID, */ - /* MU_FLAG_TYPE_ANY),==, */ - /* MU_FLAG_INVALID); */ + +static void +test_mu_flags_custom_from_str (void) +{ + unsigned u; + + struct { + const char *str; + const char *expected; + } cases[] = { + { "ABC", "ABC" }, + { "PAF", "A" }, + { "ShelloPwoFrDldR123", "helloworld123" }, + { "SPD", NULL } + }; + + for (u = 0; u != G_N_ELEMENTS(cases); ++u) { + char *cust; + cust = mu_flags_custom_from_str (cases[u].str); + if (g_test_verbose()) + g_print ("%s: str:%s; got:%s; expected:%s\n", + __FUNCTION__, cases[u].str, cust, cases[u].expected); + g_assert_cmpstr (cust, ==, cases[u].expected); + g_free (cust); + } } @@ -158,6 +180,8 @@ main (int argc, char *argv[]) g_test_add_func ("/mu-flags/test-mu-flags-to-str-s",test_mu_flags_to_str_s); g_test_add_func ("/mu-flags/test-mu-flags-from-str",test_mu_flags_from_str); g_test_add_func ("/mu-flags/test-mu-flags-from-str-delta",test_mu_flags_from_str_delta ); + g_test_add_func ("/mu-flags/test-mu-flags-custom-from-str", + test_mu_flags_custom_from_str); g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,