root/tally.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. clear_m_tally
  2. find_m_tally
  3. add_m_tally
  4. get_m_tally

   1 #include "tally.h"
   2 #include <stddef.h>
   3 #include <stdlib.h>
   4 #include <string.h>
   5 
   6 struct tally_lang {
   7         const char *name;
   8         size_t bytes;
   9 };
  10 
  11 struct tally_list {
  12         struct tally_lang *langs;
  13         size_t used;
  14 };
  15 
  16 static struct tally_list m_tally = { .langs = NULL, .used = 0 };
  17 
  18 void clear_m_tally() {
  19         free(m_tally.langs);
  20         m_tally.used = 0;
  21         m_tally.langs = NULL;
  22 }
  23 
  24 struct tally_lang* find_m_tally(const char *name) {
  25         for (size_t i = 0; i < m_tally.used; i++)
  26                 if (strcmp(m_tally.langs[i].name, name) == 0) return &m_tally.langs[i];
  27 
  28         return NULL;
  29 }
  30 
  31 void add_m_tally(const char *name, size_t bytes) {
  32         struct tally_lang *ll = find_m_tally(name);
  33         if (!ll) {
  34                 void *np = np = realloc(
  35                         m_tally.langs, sizeof(struct tally_lang) * (m_tally.used + 1));
  36                 if (!np) return;
  37                 m_tally.langs = np;
  38                 ll = &m_tally.langs[m_tally.used];
  39                 ll->name = name;
  40                 ll->bytes = 0;
  41                 m_tally.used++;
  42         }
  43         ll->bytes += bytes;
  44 }
  45 
  46 const char *get_m_tally() {
  47         struct tally_lang largest = { .name = "Unknown", .bytes = 0 };
  48 
  49         for (size_t i = 0; i < m_tally.used; i++)
  50                 if (m_tally.langs[i].bytes > largest.bytes) {
  51                         largest.name = m_tally.langs[i].name;
  52                         largest.bytes = m_tally.langs[i].bytes;
  53                 }
  54 
  55         return largest.name;

/* [previous][next][first][last][top][bottom][index][help] */