Bob Mottram 3 years ago
parent
commit
984b101dbe
6 changed files with 243 additions and 143 deletions
  1. 44 19
      src/accounts.c
  2. 3 3
      src/accounts.h
  3. 1 1
      src/calendar.c
  4. 1 1
      src/calendar.h
  5. 180 105
      src/conversions.c
  6. 14 14
      src/conversions.h

+ 44 - 19
src/accounts.c

@@ -1,6 +1,6 @@
 /*
   Fin - a simple personal accounts manager
-  Copyright (C) 2012-2015  Bob Mottram <bob@robotics.uk.to>
+  Copyright (C) 2012-2015,2017  Bob Mottram <bob@freedombone.net>
 
   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
@@ -18,37 +18,62 @@
 
 #include "accounts.h"
 
-/* returns the account name from the given filename,
-   which ends with .sqlite3 */
-static void account_name_from_filename(char * filename,
-                                       char * account)
+/**
+ * @brief returns the account name from the given filename,
+ *        which ends with .sqlite3
+ * @param filename The account filename to be checked
+ * @param account Returned account name
+ */
+static void account_name_from_filename(char filename[], char account[])
 {
     sprintf(account, "%s", filename);
     account[strlen(filename)-8] = 0;
 }
 
-/* returns 1 if the given filename is an account database */
-static int is_account_database(char * filename)
+/**
+ * @brief Returns true if the given string ends with the given extension
+ * @param str The string to be tested
+ * @param extension The extension to check for
+ * @returns True if the string ends with the extension
+ */
+static int string_ends_with_extension(char str[], char extension[])
+{
+    char * dot;
+
+    dot = strrchr(str, '.');
+    if (dot)
+        if (!strcmp(++dot, extension))
+            return (1==1);
+
+    return (1==0);
+}
+
+/**
+ * @brief returns 1 if the given filename is an account database
+ * @param filename Filename to be checked
+ * @returns 1 if the given filename is an account database
+ */
+static int is_account_database(char filename[])
 {
     if (strlen(filename) < 9)
         return 0;
 
-    if ((filename[strlen(filename)-1]=='3') &&
-        (filename[strlen(filename)-2]=='e') &&
-        (filename[strlen(filename)-3]=='t') &&
-        (filename[strlen(filename)-4]=='i') &&
-        (filename[strlen(filename)-5]=='l') &&
-        (filename[strlen(filename)-6]=='q') &&
-        (filename[strlen(filename)-7]=='s') &&
-        (filename[strlen(filename)-8]=='.')) {
+    if (string_ends_with_extension(filename, "sqlite3"))
         return 1;
-    }
+
     return 0;
 }
 
-/* shows a summary of all accounts */
-void summary_of_accounts(FILE * fp, char * directory,
-                         char * currency, int year,
+/**
+ * @brief shows a summary of all accounts
+ * @param fp Pointer to the file to output to
+ * @param directory Directory where account files exist
+ * @param currency The currency to show
+ * @param year A given year to summarize
+ * @param column_separator Character used to indicate columns
+ */
+void summary_of_accounts(FILE * fp, char directory[],
+                         char currency[], int year,
                          char column_separator)
 {
     DIR *dir;

+ 3 - 3
src/accounts.h

@@ -1,6 +1,6 @@
 /*
   Fin - a simple personal accounts manager
-  Copyright (C) 2012-2015  Bob Mottram <bob@robotics.uk.to>
+  Copyright (C) 2012-2015,2017  Bob Mottram <bob@freedombone.net>
 
   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
@@ -26,8 +26,8 @@
 #include "summary.h"
 #include "settings.h"
 
-void summary_of_accounts(FILE * fp, char * directory,
-                         char * currency, int year,
+void summary_of_accounts(FILE * fp, char directory[],
+                         char currency[], int year,
                          char column_separator);
 
 #endif

+ 1 - 1
src/calendar.c

@@ -1,6 +1,6 @@
 /*
   Fin - a simple personal accounts manager
-  Copyright (C) 2012-2015  Bob Mottram <bob@robotics.uk.to>
+  Copyright (C) 2012-2015,2017  Bob Mottram <bob@freedombone.net>
 
   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

+ 1 - 1
src/calendar.h

@@ -1,6 +1,6 @@
 /*
     Fin - a simple personal accounts manager
-    Copyright (C) 2012-2015  Bob Mottram <bob@robotics.uk.to>
+    Copyright (C) 2012-2015,2017  Bob Mottram <bob@freedombone.net>
 
     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

+ 180 - 105
src/conversions.c

@@ -1,6 +1,6 @@
 /*
   Fin - a simple personal accounts manager
-  Copyright (C) 2012-2015  Bob Mottram <bob@robotics.uk.to>
+  Copyright (C) 2012-2015,2017  Bob Mottram <bob@freedombone.net>
 
   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
@@ -18,106 +18,156 @@
 
 #include "conversions.h"
 
-static void value_to_integer(char * money, char * integer,
+/**
+ * @brief Convert a money value string to an integer value
+ * @param money String containing money value
+ * @param integer Returned string containing integer value
+ * @param decimal_places The number of decimal places to allow
+ */
+static void value_to_integer(char money[], char integer[],
                              int decimal_places)
 {
-    int i,ctr=0;
+    int i, ctr = 0;
     char intval[STRING_BLOCK];
     char decval[STRING_BLOCK];
 
-    for (i=0;i<strlen(money);i++) {
-        if (money[i]=='.')
+    for (i = 0; i < strlen(money); i++) {
+        if (money[i] == '.')
             break;
 
-        if ((money[i]>='0') && (money[i]<='9'))
-            intval[ctr++]=money[i];
+        if ((money[i] >= '0') && (money[i] <= '9'))
+            intval[ctr++] = money[i];
     }
-    intval[ctr]=0;
-    if (i==strlen(money)) {
-        ctr=0;
-        for (i=0;i<strlen(money);i++) {
-            if (money[i]==',')
+
+    intval[ctr] = 0;
+
+    if (i == strlen(money)) {
+        ctr = 0;
+        for (i = 0; i < strlen(money); i++) {
+            if (money[i] == ',')
                 break;
 
-            if ((money[i]>='0') && (money[i]<='9'))
+            if ((money[i] >= '0') && (money[i] <= '9'))
                 intval[ctr++]=money[i];
         }
-        intval[ctr]=0;
+        intval[ctr] = 0;
     }
 
     i++;
-    ctr=0;
-    while (i<strlen(money)) {
-        if ((money[i]>='0') && (money[i]<='9'))
-            decval[ctr++]=money[i];
+    ctr = 0;
+    while (i < strlen(money)) {
+        if ((money[i] >= '0') && (money[i] <= '9'))
+            decval[ctr++] = money[i];
 
         i++;
     }
-    decval[ctr]=0;
+    decval[ctr] = 0;
 
     ctr=0;
-    for (i=0;i<strlen(intval);i++)
+    for (i = 0; i < strlen(intval); i++)
         integer[ctr++] = intval[i];
 
-    for (i=0;i<strlen(decval);i++)
+    for (i = 0; i < strlen(decval); i++)
         integer[ctr++] = decval[i];
 
-    for (i=0;i<decimal_places-strlen(decval);i++)
+    for (i = 0; i < decimal_places - strlen(decval); i++)
         integer[ctr++] = '0';
 
     integer[ctr]=0;
 }
 
-static void integer_to_value(char * integer, char * money,
+/**
+ * @brief Converts an integer value into a money value
+ * @param integer String containing integer value
+ * @param money Returned string containing money value
+ * @param decimal_places The number of decimal places to allow
+ */
+static void integer_to_value(char integer[], char money[],
                              int decimal_places)
 {
-    int i,ctr=0;
+    int i, ctr = 0;
 
-    for (i=0;i<strlen(integer)-decimal_places;i++)
-        money[ctr++]=integer[i];
+    for (i = 0; i < strlen(integer) - decimal_places; i++)
+        money[ctr++] = integer[i];
 
-    money[ctr++]='.';
-    while (i<strlen(integer)) {
-        money[ctr++]=integer[i];
-        i++;
-    }
-    money[ctr]=0;
+    money[ctr++] = '.';
+
+    while (i < strlen(integer))
+        money[ctr++] = integer[i++];
+
+    money[ctr] = 0;
 }
 
-void money_to_integer(char * money, char * integer)
+/**
+ * @brief Convert a money string to an integer string with the
+ *        default number of decimal places
+ * @param money String containing money value
+ * @param integer Returned string containing integer value
+ */
+void money_to_integer(char money[], char integer[])
 {
     value_to_integer(money,integer, DECIMAL_PLACES);
 }
 
-void integer_to_money(char * integer, char * money)
+/**
+ * @brief Convert an integer string to a  money string with the
+ *        default number of decimal places
+ * @param integer String containing integer value
+ * @param money Returned string containing money value
+ */
+void integer_to_money(char integer[], char money[])
 {
     integer_to_value(integer, money, DECIMAL_PLACES);
 }
 
-void quantity_to_integer(char * money, char * integer)
+/**
+ * @brief Convert a quantity string to an integer string with the
+ *        default number of decimal places
+ * @param qty String containing quantity value
+ * @param integer Returned string containing integer value
+ */
+void quantity_to_integer(char qty[], char integer[])
 {
-    value_to_integer(money,integer, DECIMAL_PLACES_QUANTITY);
+    value_to_integer(qty, integer, DECIMAL_PLACES_QUANTITY);
 }
 
-void integer_to_quantity(char * integer, char * money)
+/**
+ * @brief Convert an integer string to a quantity string with the
+ *        default number of decimal places
+ * @param integer String containing integer value
+ * @param qty Returned string containing quantity value
+ */
+void integer_to_quantity(char integer[], char qty[])
 {
-    integer_to_value(integer, money, DECIMAL_PLACES_QUANTITY);
+    integer_to_value(integer, qty, DECIMAL_PLACES_QUANTITY);
 }
 
-void pad_text(char * str, int width)
+/**
+ * @brief Adds space padding to a given string to make it fixed width
+ * @param str String to be padded
+ * @param width Width to pad to
+ */
+void pad_text(char str[], int width)
 {
     int i = 0;
 
-    for (i=strlen(str);i<width;i++)
-        str[i]=' ';
+    for (i = strlen(str); i < width; i++)
+        str[i] = ' ';
 
-    str[i]=0;
+    str[i] = 0;
 }
 
-void pad_value(char * value, char * padded,
+/**
+ * @brief Pads a value string with leading and trailing spaces
+ * @param value String to be padded
+ * @param padded The returned padded string
+ * @param leading_spaces Number of leading spaces
+ * @param trailing_spaces Number of trailing spaces
+ */
+void pad_value(char value[], char padded[],
                int leading_spaces, int trailing_zeros)
 {
-    int i,decimal_point=-1,zeros,leading;
+    int i, decimal_point = -1, zeros, leading;
     char temp[32];
 
     if (value == 0)
@@ -167,9 +217,15 @@ void pad_value(char * value, char * padded,
     padded[leading + i] = 0;
 }
 
-/* convert a time string into a standard form */
-void convert_time_to_standard(char * timestr, char * converted,
-                              char * am, char * pm)
+/**
+ * @brief convert a time string into a standard form
+ * @param timestr String containing time
+ * @param converted Returned string containing standard time
+ * @param am String which indicates AM
+ * @param pm String which indicates PM
+ */
+void convert_time_to_standard(char timestr[], char converted[],
+                              char am[], char pm[])
 {
     int i, ctr = 0, state = 0;
     int hr = 0, min = 0, sec = 0;
@@ -177,7 +233,7 @@ void convert_time_to_standard(char * timestr, char * converted,
 
     /* handle empty string */
     if (timestr[0] == 0) {
-        converted[0]=0;
+        converted[0] = 0;
         return;
     }
 
@@ -186,13 +242,13 @@ void convert_time_to_standard(char * timestr, char * converted,
         timestr[i] = tolower(timestr[i]);
 
     for (i = 0; i < strlen(timestr); i++) {
-        if ((timestr[i]==am[0]) ||
-            (timestr[i]==pm[0])) {
-            str[ctr]=0;
+        if ((timestr[i] == am[0]) ||
+            (timestr[i] == pm[0])) {
+            str[ctr] = 0;
             switch(state) {
             case 0: {
                 hr = atoi(str);
-                if (timestr[i]==pm[0])
+                if (timestr[i] == pm[0])
                     if (hr < 12) hr += 12;
 
                 break;
@@ -202,12 +258,12 @@ void convert_time_to_standard(char * timestr, char * converted,
             }
             break;
         }
-        if ((timestr[i]==':') ||
-            (timestr[i]=='.') || (i == strlen(timestr)-1)) {
+        if ((timestr[i] == ':') ||
+            (timestr[i] == '.') || (i == strlen(timestr)-1)) {
             if (i == strlen(timestr)-1)
                 str[ctr++] = timestr[i];
 
-            str[ctr]=0;
+            str[ctr] = 0;
             switch(state) {
             case 0: { hr = atoi(str); break; }
             case 1: { min = atoi(str); break; }
@@ -220,49 +276,59 @@ void convert_time_to_standard(char * timestr, char * converted,
             str[ctr++] = timestr[i];
     }
 
-    sprintf(converted,"%02d:%02d:%02d",hr,min,sec);
+    sprintf(converted, "%02d:%02d:%02d", hr, min, sec);
 }
 
-/* convert a date to a standard format for storage in the database */
-void convert_date_to_standard(char * date, char * date_format,
-                              char * converted)
+/**
+ * @brief convert a date to a standard format for storage in the database
+ * @param date Date string to be converted
+ * @param date_format Format to be used
+ * @param converted Returned converted date string
+ */
+void convert_date_to_standard(char date[], char date_format[],
+                              char converted[])
 {
-    int i,month=1,day=1,year=0;
+    int i, month = 1, day = 1, year = 0;
     char str[10];
 
-    for (i=0;i<strlen(date_format)-1;i++) {
-        if (date_format[i]=='%') {
-            if (date_format[i+1]=='m') {
-                str[0]=date[i];
-                str[1]=date[i+1];
-                str[2]=0;
+    for (i = 0; i < strlen(date_format) - 1; i++) {
+        if (date_format[i] == '%') {
+            if (date_format[i+1] == 'm') {
+                str[0] = date[i];
+                str[1] = date[i+1];
+                str[2] = 0;
                 month = atoi((char*)str);
             }
-            if (date_format[i+1]=='d') {
-                str[0]=date[i];
-                str[1]=date[i+1];
-                str[2]=0;
+            if (date_format[i+1] == 'd') {
+                str[0] = date[i];
+                str[1] = date[i+1];
+                str[2] = 0;
                 day = atoi((char*)str);
             }
-            if (date_format[i+1]=='Y') {
-                str[0]=date[i];
-                str[1]=date[i+1];
-                str[2]=date[i+2];
-                str[3]=date[i+3];
-                str[4]=0;
+            if (date_format[i+1] == 'Y') {
+                str[0] = date[i];
+                str[1] = date[i+1];
+                str[2] = date[i+2];
+                str[3] = date[i+3];
+                str[4] = 0;
                 year = atoi((char*)str);
             }
         }
     }
-    sprintf(converted,"%04d-%02d-%02d",year,month,day);
+    sprintf(converted, "%04d-%02d-%02d", year, month, day);
 }
 
-/* convert a date from the database into the desired date format */
-void convert_standard_to_date(char * standard, char * date_format,
-                              char * date)
+/**
+ * @brief convert a date from the database into the desired date format
+ * @param standard String containing standard format date
+ * @param date_format The date format to be used
+ * @param date Returned date string in the desired format
+ */
+void convert_standard_to_date(char standard[], char date_format[],
+                              char date[])
 {
     char str[5];
-    int day,month,year,i;
+    int day, month, year, i;
 
     str[0] = standard[0];
     str[1] = standard[1];
@@ -281,44 +347,49 @@ void convert_standard_to_date(char * standard, char * date_format,
     str[2] = 0;
     day = atoi((char*)str);
 
-    sprintf(date,"%s",date_format);
+    sprintf(date, "%s", date_format);
 
-    for (i=0;i<strlen(date_format)-1;i++) {
-        if (date_format[i]=='%') {
-            if (date_format[i+1]=='m')
-                sprintf((char*)&date[i],"%02d",month);
+    for (i = 0; i < strlen(date_format) - 1; i++) {
+        if (date_format[i] == '%') {
+            if (date_format[i+1] == 'm')
+                sprintf((char*)&date[i], "%02d", month);
 
-            if (date_format[i+1]=='d')
-                sprintf((char*)&date[i],"%02d",day);
+            if (date_format[i+1] == 'd')
+                sprintf((char*)&date[i], "%02d", day);
 
-            if (date_format[i+1]=='Y')
-                sprintf((char*)&date[i],"%04d",year);
+            if (date_format[i+1] == 'Y')
+                sprintf((char*)&date[i], "%04d", year);
         }
     }
-    for (i=0;i<strlen(date_format);i++) {
-        if ((date_format[i]=='/') ||
-            (date_format[i]=='-') ||
-            (date_format[i]=='.'))
+    for (i = 0; i < strlen(date_format); i++) {
+        if ((date_format[i] == '/') ||
+            (date_format[i] == '-') ||
+            (date_format[i] == '.'))
             date[i] = date_format[i];
     }
-    date[10]=0;
+    date[10] = 0;
 }
 
-int reverse_file(char * filename)
+/**
+ * @brief Reverses the lines in a file
+ * @param filename The file to be reversed
+ * @returns zero on success
+ */
+int reverse_file(char filename[])
 {
     FILE * fp;
-    int i,row,rows=0,retval=0;
+    int i, row, rows = 0, retval = 0;
     char line[STRING_BLOCK];
     char header[STRING_BLOCK];
     char * output_filename = "temp_reverse.txt";
 
     /* count the number of rows */
     fp = fopen(filename,"r");
-    if (fp!=0) {
+    if (fp != 0) {
         while (!feof(fp)) {
             if (fgets(line , 255 , fp) != NULL ) {
-                if (rows==0)
-                    sprintf((char*)header,"%s",line);
+                if (rows == 0)
+                    sprintf((char*)header, "%s", line);
 
                 rows++;
             }
@@ -335,15 +406,15 @@ int reverse_file(char * filename)
     for (i = rows; i > 1; i--) {
         row = 0;
         fp = fopen(filename,"r");
-        if (fp!=0) {
-            while ((!feof(fp)) && (row<i)) {
+        if (fp != 0) {
+            while ((!feof(fp)) && (row < i)) {
                 if (fgets(line , 255 , fp) != NULL )
                     row++;
             }
             fclose(fp);
         }
 
-        fp = fopen(output_filename,"a");
+        fp = fopen(output_filename, "a");
         if (fp!=0) {
             fprintf(fp,"%s",line);
             fclose(fp);
@@ -359,8 +430,12 @@ int reverse_file(char * filename)
     return retval;
 }
 
-/* trims the first line from a text file */
-void trim_file_header(char * filename, char * trimmed)
+/**
+ * @brief trims the first line from a text file
+ * @param filename Filename to trim
+ * @param trimmed Resulting filename with trimming
+ */
+void trim_file_header(char filename[], char trimmed[])
 {
     FILE * fp, * fp_trimmed;
     int row=0;

+ 14 - 14
src/conversions.h

@@ -25,24 +25,24 @@
 #include <ctype.h>
 #include "globals.h"
 
-void money_to_integer(char * money, char * integer);
-void integer_to_money(char * integer, char * money);
+void money_to_integer(char money[], char integer[]);
+void integer_to_money(char integer[], char money[]);
 
-void quantity_to_integer(char * money, char * integer);
-void integer_to_quantity(char * integer, char * money);
+void quantity_to_integer(char qty[], char integer[]);
+void integer_to_quantity(char integer[], char qty[]);
 
-void pad_value(char * value, char * padded, int leading_spaces,
+void pad_value(char value[], char padded[], int leading_spaces,
                int trailing_zeros);
-void pad_text(char * str, int width);
+void pad_text(char str[], int width);
 
-void convert_date_to_standard(char * date,
-                              char * date_format, char * converted);
-void convert_standard_to_date(char * standard,
-                              char * date_format, char * date);
+void convert_date_to_standard(char date[],
+                              char date_format[], char converted[]);
+void convert_standard_to_date(char standard[],
+                              char date_format[], char date[]);
 
-int reverse_file(char * filename);
-void trim_file_header(char * filename, char * trimmed);
-void convert_time_to_standard(char * timestr, char * converted,
-                              char * am, char * pm);
+int reverse_file(char filename[]);
+void trim_file_header(char filename[], char trimmed[]);
+void convert_time_to_standard(char timestr[], char converted[],
+                              char am[], char pm[]);
 
 #endif