Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include "sqlite3.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void execute_script(const char *database_path, const char *script_text);
  6.  
  7. int parse_arguments(int argc, char**argv, char **database_path, char **script_path);
  8.  
  9. char *read_file(const char *file_path);
  10.  
  11. const char *const PARAM_HELP = {
  12.     "The following parameters are accepted:\n"
  13.     "\t/d [database path] /s [script path]\n"
  14.     "\t/s [script path] /d [database path]"
  15. };
  16.  
  17. int main(int argc, char **argv)
  18. {
  19.     char *database_path, *script_path;
  20.  
  21.     if (parse_arguments(argc, argv, &database_path, &script_path))
  22.     {
  23.         char *script_text = read_file(script_path);
  24.  
  25.         if (script_text == NULL)
  26.         {
  27.             printf("unable to open file: %s\n", script_path);
  28.         }
  29.         else
  30.         {
  31.             execute_script(database_path, script_text);
  32.             free(script_text);
  33.         }
  34.     }
  35.     else
  36.     {
  37.         printf(PARAM_HELP);
  38.     }
  39. }
  40.  
  41. char *allocate_string(size_t string_length)
  42. {
  43.     char *result = malloc(string_length + 1);
  44.  
  45.     if (result != NULL)
  46.     {
  47.         result[string_length] = '\0';
  48.     }
  49.     return result;
  50. }
  51.  
  52. void execute_script(const char *database_path, const char *script_text)
  53. {
  54.     sqlite3* database;
  55.     const int error_code = sqlite3_open_v2(database_path, &database, SQLITE_OPEN_READWRITE, NULL);
  56.  
  57.     if (error_code)
  58.     {
  59.         printf("%s\n", sqlite3_errstr(error_code));
  60.     }
  61.     else
  62.     {
  63.         char *error_message = NULL;
  64.  
  65.         if (sqlite3_exec(database, script_text, NULL, NULL, &error_message))
  66.         {
  67.             printf("%s\n", error_message);
  68.             sqlite3_free(error_message);
  69.         }
  70.     }
  71.     sqlite3_close(database);
  72. }
  73.  
  74. long file_length(FILE *file)
  75. {
  76.     const long initial = ftell(file);
  77.  
  78.     fseek(file, 0, SEEK_END);
  79.     const long length = ftell(file);
  80.  
  81.     fseek(file, initial, SEEK_SET);
  82.  
  83.     return length;
  84. }
  85.  
  86. const size_t VALID_ARGC = 5;
  87.  
  88. int parse_arguments(int argc, char**argv, char **database_path, char **script_path)
  89. {
  90.     *database_path = *script_path = NULL;
  91.  
  92.     if (argc == VALID_ARGC)
  93.     {
  94.         for (size_t i = 1; i < VALID_ARGC; ++i)
  95.         {
  96.             if (argv[i][0] != '/' || !argv[i][1] || argv[i][2]) return 0;
  97.  
  98.             switch (argv[i][1])
  99.             {
  100.             case 'd':
  101.             case 'D':
  102.                 *database_path = argv[++i];
  103.                 break;
  104.             case 's':
  105.             case 'S':
  106.                 *script_path = argv[++i];
  107.                 break;
  108.             default:
  109.                 return 0;
  110.             }
  111.         }
  112.     }
  113.     return *database_path && *script_path;
  114. }
  115.  
  116. const char *const FILE_OPEN_MODE = "rb";
  117.  
  118. const size_t NUMBER_FILE_ELEMENTS = 1;
  119.  
  120. char* read_file(const char *file_path)
  121. {
  122.     char *text = NULL;
  123.     FILE *file = fopen(file_path, FILE_OPEN_MODE);
  124.  
  125.     if (file)
  126.     {
  127.         const long file_size = file_length(file);
  128.  
  129.         if (file_size)
  130.         {
  131.             if ((text = allocate_string(file_size)))
  132.             {
  133.                 if (fread(text, file_size, NUMBER_FILE_ELEMENTS, file) != NUMBER_FILE_ELEMENTS)
  134.                 {
  135.                     free(text);
  136.                     text = NULL;
  137.                 }
  138.             }
  139.         }
  140.         fclose(file);
  141.     }
  142.     return text;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement