+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * 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 Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "totais.h"
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "pmhash.h"
+
+int totais_add(struct declaracao *dec, char *key, long long val)
+{
+ long long *p;
+ int r = -ENOMEM;
+ p = pmhash_get(dec->totais, key);
+ if (!p) {
+ p = malloc(sizeof(*p));
+ if (!p)
+ goto out_p;
+ key = strdup(key);
+ if (!key)
+ goto out_key;
+ r = pmhash_add(&dec->totais, key, p);
+ if (r)
+ goto out_hash;
+ *p = 0;
+ }
+ *p += val;
+ return 0;
+out_hash:
+ free(key);
+out_key:
+ free(p);
+out_p:
+ return r;
+}
+
+long long totais_get(struct declaracao *dec, char *key)
+{
+ long long *p;
+ p = pmhash_get(dec->totais, key);
+ if (!p)
+ return 0;
+ return *p;
+}