Common code indentation, now using astyle

Christian Stimming stimming at tuhh.de
Fri Aug 28 17:16:02 EDT 2009


Another program, another try: I propose to unify some aspects of indentation 
using the "astyle" program, http://astyle.sourceforge.net . Different from 
the "indent" program, astyle has much less options and doesn't allow as many 
aspects to be fine-tuned. Instead, the astyle program can be used to unify 
only some aspects of the code (like the indentation itself) but leaving other 
aspects alone (like comment reformatting).

Also, there are no arguments to switch off something like in indent. Instead, 
by default everything is switched off and we only explicitly switch on those 
reformatting features which we want.

Hence, I propose the following arguments:

--indent=spaces=4     # Indent using 4 spaces per indent

--brackets=break        # Break brackets from their pre-block statements ( 
e.g. ANSI C / C++ style ).

# No "--indent-brackets", Add extra indentation to brackets.
# No "--indent-blocks", Add extra indentation to blocks within a function.

--pad-oper                   # Insert space padding around operators.

# No "--pad-paren-out", Insert space padding around parenthesis on the outside 
only.
# No "--unpad-paren", Remove extra space padding around parenthesis; in 
combination with the previous one this removes the extra space on the inside. 
This parenthesis padding doesn't work well enough, e.g. two opening 
parenthesis appear to get an extra padding, but two closing ones don't. 
Hence, we just leave it as-is.

As a one-liner:

$ astyle --indent=spaces=4 --brackets=break --pad-oper  *.[hc]

Below you will find some excerpts from src/engine/Account.c, reformatted with 
version astyle-1.23 and the above options. I think this is already quite some 
progress, and I'd suggest to apply this to all of trunk in the next weeks, so 
that we have done it before another stable branch is started later this year. 

What do you think?

Regards,

Christian



#include "config.h"
#include "AccountP.h"
#include "Split.h"

#define GNC_ID_ROOT_ACCOUNT        "RootAccount"

static QofLogModule log_module = GNC_MOD_ACCOUNT;

/* The Canonical Account Separator.  Pre-Initialized. */
static gchar account_separator[8] = ".";
gunichar account_uc_separator = ':';

enum
{
    LAST_SIGNAL
};

enum
{
    PROP_0,
    PROP_NAME,
    PROP_FULL_NAME,
    PROP_CODE,
};

typedef struct AccountPrivate
{
    /* The accountName is an arbitrary string assigned by the user.
     * It is intended to a short, 5 to 30 character long string that
     * is displayed by the GUI as the account mnemonic.
     */
    char *accountName;

    /* The description is an arbitrary string assigned by the user.
     * It is intended to be a longer, 1-5 sentence description of what
     * this account is all about.
     */
    char *description;

    /* cached parameters */
    gnc_numeric balance;
    gnc_numeric cleared_balance;
    gnc_numeric reconciled_balance;

    gboolean balance_dirty;     /* balances in splits incorrect */

    GList *splits;              /* list of split pointers */
    gboolean sort_dirty;        /* sort order of splits is bad */
} AccountPrivate;

#define GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_ACCOUNT, AccountPrivate))


/********************************************************************\
 * gnc_get_account_separator                                        *
 *   returns the current account separator character                *
 *                                                                  *
 * Args: none                                                       *
 * Returns: account separator character                             *
 \*******************************************************************/
const gchar *
gnc_get_account_separator_string (void)
{
    return account_separator;
}

void
gnc_set_account_separator (const gchar *separator)
{
    gunichar uc;
    gint count;

    uc = g_utf8_get_char_validated(separator, -1);
    if ((uc == (gunichar) - 2) || (uc == (gunichar) - 1) || 
g_unichar_isalnum(uc))
    {
        account_uc_separator = ':';
        strcpy(account_separator, ":");
        return;
    }

    account_uc_separator = uc;
    count = g_unichar_to_utf8(uc, account_separator);
    account_separator[count] = '\0';
}

/********************************************************************\
\********************************************************************/

G_INLINE_FUNC void mark_account (Account *acc);
void
mark_account (Account *acc)
{
    qof_instance_set_dirty(&acc->inst);
}

/********************************************************************\
\********************************************************************/


static void
gnc_account_get_property (GObject         *object,
                          guint            prop_id,
                          GValue          *value,
                          GParamSpec      *pspec)
{
    Account *account;
    AccountPrivate *priv;

    g_return_if_fail(GNC_IS_ACCOUNT(object));

    account = GNC_ACCOUNT(object);
    priv = GET_PRIVATE(account);
    switch (prop_id)
    {
    case PROP_NAME:
        g_value_set_string(value, priv->accountName);
        break;
    case PROP_FULL_NAME:
        g_value_take_string(value, xaccAccountGetFullName(account));
        break;
    case PROP_PLACEHOLDER:
        g_value_set_boolean(value, xaccAccountGetPlaceholder(account));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
gnc_account_class_init (AccountClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    gobject_class->dispose = gnc_account_dispose;
    gobject_class->finalize = gnc_account_finalize;
    gobject_class->set_property = gnc_account_set_property;
    gobject_class->get_property = gnc_account_get_property;

    g_type_class_add_private(klass, sizeof(AccountPrivate));

    g_object_class_install_property
    (gobject_class,
     PROP_NAME,
     g_param_spec_string ("name",
                          "Account Name",
                          "The accountName is an arbitrary string "
                          "assigned by the user.  It is intended to "
                          "a short, 5 to 30 character long string "
                          "that is displayed by the GUI as the "
                          "account mnemonic.  Account names may be "
                          "repeasted. but no two accounts that share "
                          "a parent may have the same name.",
                          NULL,
                          G_PARAM_READWRITE));


    g_object_class_install_property
    (gobject_class,
     PROP_PLACEHOLDER,
     g_param_spec_boolean ("placeholder",
                           "Placeholder",
                           "Whether the account is a placeholder account which 
does not "
                           "allow transactions to be created, edited or 
deleted.",
                           FALSE,
                           G_PARAM_READWRITE));
}

void
xaccAccountSetNotes (Account *acc, const char *str)
{
    g_return_if_fail(GNC_IS_ACCOUNT(acc));

    xaccAccountBeginEdit(acc);
    if (str)
    {
        gchar *tmp = g_strstrip(g_strdup(str));
        kvp_frame_set_slot_nc(acc->inst.kvp_data, "notes",
                              strlen(tmp) ? kvp_value_new_string(tmp) : NULL);
        g_free(tmp);
    }
    else
    {
        kvp_frame_set_slot_nc(acc->inst.kvp_data, "notes", NULL);
    }
    mark_account(acc);
    xaccAccountCommitEdit(acc);
}

gnc_numeric
xaccAccountGetProjectedMinimumBalance (const Account *acc)
{
    AccountPrivate *priv;
    GList *node;
    time_t today;
    gnc_numeric lowest = gnc_numeric_zero ();
    int seen_a_transaction = 0;

    g_return_val_if_fail(GNC_IS_ACCOUNT(acc), gnc_numeric_zero());

    priv = GET_PRIVATE(acc);
    today = gnc_timet_get_today_end();
    for (node = g_list_last(priv->splits); node; node = node->prev)
    {
        Split *split = node->data;

        if (!seen_a_transaction)
        {
            lowest = xaccSplitGetBalance (split);
            seen_a_transaction = 1;
        }
        else if (gnc_numeric_compare(xaccSplitGetBalance (split), lowest) < 0)
        {
            lowest = xaccSplitGetBalance (split);
        }

        if (xaccTransGetDate (xaccSplitGetParent (split)) <= today)
            return lowest;
    }

    return lowest;
}


More information about the gnucash-devel mailing list