summaryrefslogtreecommitdiff
path: root/src/common.c
blob: c8b8d9a25b533730d51a4510fe31f25353da4bcc (plain)
    1 /**
    2  * Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
    3  *
    4  * Noteless is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 3 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * Noteless is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with Noteless.  If not, see <http://www.gnu.org/licenses/>.
   16  */
   17 #include "common.h"
   18 
   19 
   20 char* trim(char* str) {
   21   char* end = &str[strlen(str)];
   22   while(str[0] == ' ' || str[0] == '\t')
   23     str++;
   24   while(end[0] == ' ' || end[0] == '\t' || end[0] == '\n' || end[0] == '\0')
   25     end--;
   26   end[1] = '\0';
   27   return str;
   28 }
   29 
   30 /**
   31  * Determins the text editor that should be used. Checks the environmental
   32  * variable EDITOR for this.
   33  *
   34  * @return char* Path to the editor binary to be used (stored in heap)
   35  */
   36 void get_user_editor(char* editor) {
   37   if(getenv("EDITOR") != NULL) {
   38     /* If the EDITOR variable is set, use that */
   39     //int editor_len = strlen(getenv("EDITOR"));
   40     // *editor = calloc(editor_len, sizeof(char));
   41     strcpy(editor, getenv("EDITOR"));
   42   } else {
   43     strcpy(editor, "vi");
   44   }
   45 }
   46 
   47 /**
   48  * TODO: Write this function description
   49  *
   50  * @return int Extension char length
   51  */
   52 int get_extension(char* path, char* extension) {
   53   int path_len = strlen(path);
   54   // Start from the right and move left
   55   for(int i = path_len; i > 0; i--) {
   56     // Copy if an extension delimiter (a period) is found
   57     if(path[i] == '.') {
   58       int ext_len = path_len - i;
   59       strncpy(extension, &path[i+1], ext_len);
   60       return ext_len;
   61     }
   62   }
   63   return -1;
   64 }
   65 
   66 
   67 /**
   68  * Opens a note using the specified editor
   69  *
   70  * @param string editor Path to the editor binary to use for editing.
   71  *
   72  * @return int Success or failure of the command
   73  */
   74 int file_edit(char* path, char* editor) {
   75   char cmd[strlen(editor) + 3 + strlen(path)];
   76   sprintf(cmd, "%s %s", editor, path);
   77   return system(cmd);
   78 }
   79 
   80 
   81 /**
   82  * Yup, wrote my own.
   83  * TODO: Make this work with escaped path delimiters
   84  * TODO: Write this function description
   85  *
   86  * @return int Basename path length
   87  */
   88 int basename(char* path, char* out) {
   89   int path_len = strlen(path);
   90   // Start from the right and move left
   91   for(int i = path_len; i > 0; i--) {
   92     // Copy if an extension delimiter (a period) is found
   93     if(path[i] == '/') {
   94       strcpy(out, &path[i + 1]);
   95       return i;
   96     }
   97   }
   98   return -1;
   99 }
  100 
  101 
  102 /**
  103  * Just a wrapper function to call the specified line search function for case
  104  * insensitive or case sensitive matching.
  105  *
  106  * @param line Line to check for the search term
  107  * @param term Search term to find in line
  108  *
  109  * @return int Search term found (1) or not (0)
  110  */
  111 int str_contains(char* line, char* term, int case_insensitive) {
  112   if(case_insensitive == 1) {
  113     return str_contains_case_insensitive(line, term);
  114   } else {
  115     return str_contains_case_sensitive(line, term);
  116   }
  117 }
  118 
  119 
  120 /**
  121  * Performs a case insensitive search in the given line for the specified term.
  122  *
  123  * Note that for the sake of efficiency, this function is fairly confusing
  124  * (sorry).  Learning how chars and ints relate in c will help tremendously
  125  * to understand this function.
  126  *
  127  * @param str String to check for the search term
  128  * @param term Search term to find in line
  129  *
  130  * @return int Search term found (1) or not (0)
  131  */
  132 int str_contains_case_insensitive(char* str, char* term) {
  133   // String char index
  134   int si = 0;
  135   // Term char index
  136   int ti = 0;
  137   int term_len = strlen(term);
  138 
  139   // Compare one char at a time
  140   while(str[si] != '\0') {
  141     // Current str char
  142     char lc;
  143     // Current term char
  144     char tc;
  145 
  146     // Welcome to the confusing part (see function description)
  147 
  148     // Convert current term char to lowercase
  149     if(term[ti] >= 97 && term[ti] <= 122)  {
  150       // Already lower case
  151       tc = term[ti];
  152     } else if(term[ti] >= 65 && term[ti] <= 90) {
  153       // Uppercase. Add 32 to get lowercase equivelant.
  154       tc = term[ti] + 32;
  155     } else {
  156       // Term char isn't a letter. Must be a symbol or something ELSE...
  157       // Take it as it is.
  158       tc = term[ti];
  159     }
  160 
  161     // Convert current str char to lowercase
  162     if(str[si] >= 97 && str[si] <= 122)  {
  163       // Already lower case
  164       lc = str[si];
  165     } else if(str[si] >= 65 && str[si] <= 90) {
  166       // Uppercase. Add 32 to get lowercase equivelant.
  167       lc = str[si] + 32;
  168     } else {
  169       // Term char isn't a letter. Must be a symbol or something ELSE...
  170       // Take it as it is.
  171       lc = str[si];
  172     }
  173 
  174     // Compare the temp lowercase version of the current str and term chars
  175     if(lc == tc) {
  176       ti++;
  177     } else {
  178       ti = 0;
  179     }
  180 
  181     // Return if we've matched the entire search term
  182     if(ti == term_len) { return 1; }
  183 
  184     si++;
  185   }
  186   return 0;
  187 }
  188 
  189 
  190 /**
  191  * Performs a case sensitive search in the given string for the specified term.
  192  *
  193  * @param str String to check for the search term
  194  * @param term Search term to find in str
  195  *
  196  * @return int Search term found (1) or not (0)
  197  */
  198 int str_contains_case_sensitive(char* str, char* term) {
  199   int s = 0;
  200   int t = 0;
  201   int term_len = strlen(term);
  202 
  203   // Compare one char at a time
  204   while(str[s] != '\0') {
  205     if(t == term_len) {
  206       return 1;
  207     } else if(term[t] == str[s]) {
  208       // Found a single char match. Increment to the next term char
  209       t++;
  210     } else {
  211       // Reset the match counter if a partial or no match was made
  212       t = 0;
  213     }
  214     s++;
  215   }
  216   return 0;
  217 }
  218 
  219 
  220 /**
  221  * Removes the extension from the given string filename
  222  *
  223  * @param filename Filename/path to strip extension from
  224  *
  225  * @return int Result of the strip operation
  226  */
  227 int strip_extension(char* filename) {
  228   for(int i = strlen(filename); i > 0; i--) {
  229     if(filename[i] == '.') {
  230       filename[i] = '\0';
  231       return 0;
  232     }
  233   }
  234   return -1;
  235 }

Generated by cgit