Guest User

Untitled

a guest
Sep 14th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.73 KB | None | 0 0
  1. #include "include/types.h"
  2. #include "include/gaussian.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6.  
  7. #include <dirent.h>
  8. #include <string.h> // strcmp, strtok
  9. #include <sys/types.h> // mkdir
  10. #include <sys/stat.h> // mkdir
  11.  
  12. void addDirectory(const char *dir, char ** toDir);
  13. void addDirectory(const char *dir, char ** toDir){
  14.     char workingPath [256] = "test";
  15.     strcpy( *toDir, workingPath );
  16.     strcat(  *toDir, "\\" );
  17.     strcat(  *toDir, dir );
  18. }
  19.  
  20. int makeConfig(char ** path)
  21. {
  22.     char configFile [12] = "kernels.cfg";
  23.     DIR *dir;
  24.     struct dirent *ent;
  25.     int count = -1;
  26.  
  27.     if ( ( dir = opendir (*path) ) != NULL)
  28.     {
  29.     char ch;
  30.     FILE* f;
  31.     FILE* f2;
  32.     f = fopen(configFile, "r");
  33.     if( f != NULL ) // if config already exists, backup
  34.         {
  35.         f2 = fopen("kernels.bak", "w");
  36.         while( ( ch = fgetc(f) ) != EOF )
  37.             fputc(ch, f2);
  38.         fclose(f);
  39.         fclose(f2);
  40.         }
  41.     // write new file
  42.     f = fopen(configFile, "w");
  43.         if( f == NULL )
  44.             perror("Cannot write to file kernels.cfg");
  45.  
  46.  
  47.     int ckey = 0;
  48.     size_t len;
  49.  
  50.     while ((ent = readdir (dir)) != NULL)
  51.         {
  52.             if ( ckey<2 && ( strcmp(".",ent->d_name) == 0 ||  strcmp("..",ent->d_name) == 0 )  )
  53.                 { ckey++; continue; }
  54.  
  55.             // backup ent->d_name because chTmp2 is changed by strtok
  56.             len = strlen(ent->d_name);
  57.             // char * dot = strchr( ent->d_name, '.' );
  58.             if ( len >4 )
  59.                 {
  60.                 char name[KERNEL_FILENAME_SIZE];
  61.                 if (strlen(ent->d_name)>KERNEL_FILENAME_SIZE-1)
  62.                 {
  63.                     printf("Incorrect kernel source file name.%s , file name size can be max. %d bytes long.\n",ent->d_name,KERNEL_FILENAME_SIZE-1);
  64.                     return -4;
  65.                 }
  66.  
  67.                 /*
  68.                 char * pName = ent->d_name;
  69.                 char * ext;
  70.                 ext = pName + len - 3;
  71.                 */
  72.                 memcpy( name, ent->d_name, len-3 );
  73.                 if ( 0 == strcmp( ".",  name) )
  74.                     {
  75.                     printf("Incorrect kernel source name. File name %s, must not contain dots, but must have .cl extension.\n",ent->d_name);
  76.                     }
  77.                     else
  78.                     {
  79.                     name[len-3]='\0';
  80.                     fprintf(f, "%s\n", name);
  81.                     printf (".'%s'\n", ent->d_name);
  82.                     }
  83.                 }
  84.                 else
  85.                 {
  86.                  printf("Incorrect kernel source name. File name %s too short.\n",ent->d_name);
  87.                 }
  88.         }
  89.         closedir (dir); fclose(f);
  90.     }
  91.     else
  92.     {
  93.         /* could not open directory */
  94.         char error [255];
  95.         sprintf(error, "Directory not found: %s", *path);
  96.         perror (error);
  97.         return -1;
  98.     }
  99. return count;
  100. }
  101.  
  102.  
  103. int readConfig(char ** path, FILES * files, bool readPrograms, SETTINGS settings )
  104. {
  105.     char configFile [12] = "kernels.cfg";
  106.     DIR *dir;
  107.  
  108.     if ( ( dir = opendir (*path) ) == NULL)
  109.         {
  110.             perror("directory of binary kernels not found");
  111.             return -2;
  112.         }
  113.         else
  114.             closedir(dir);
  115.  
  116.     FILE* f;
  117.     FILE* f2;
  118.     f = fopen(configFile, "r");
  119.     if( f == NULL ) // if config already exists, backup
  120.         {
  121.         perror("Config file kernels.cfg not found. Run the program with -install argument.");
  122.         return -3;
  123.         }
  124.  
  125.     int ch,ch2; int c = 0; int prevc = 0; int count = 0;
  126.     long int fsize2;
  127.     char name[KERNEL_FILENAME_SIZE];
  128.     while ((ch=fgetc(f)) != EOF )
  129.     {
  130.        name[c-prevc]=ch;
  131.        c++;
  132.        if (ch == '\n' && c-prevc>1 )
  133.            {
  134.            // files[count].name=malloc(KERNEL_FILENAME_SIZE);
  135.            // files[count].filename=malloc(KERNEL_FILENAME_SIZE);
  136.            // strncpy(files[count].name, name, KERNEL_FILENAME_SIZE);
  137.            strcpy(files[count].name,name);
  138.            files[count].name[c-prevc-1]='\0'; // remove linefeed \n
  139.             if (strlen(files[count].name)>KERNEL_FILENAME_SIZE-4)
  140.             {
  141.                 sprintf("Incorrect kernel name.%s , kernel name in config file kernels.cfg my be max %d bytes long.\n",files[count].name,KERNEL_FILENAME_SIZE-4);
  142.                 return -4;
  143.             }
  144.  
  145.            strcpy(files[count].filename,name);
  146.            files[count].filename[c-prevc-1]='\0'; // remove linefeed: \n
  147.            sprintf(files[count].filename,"%s_%d.bin",files[count].filename,settings.load_device_id);
  148.  
  149.            /** -install don't require reading of programs **/
  150.            if (readPrograms)
  151.                {
  152.                    char filepath [KERNEL_TOTAL_FILE_PATH_SIZE];
  153.                    if (strlen(*path) + 1 + strlen(files[count].filename) > KERNEL_TOTAL_FILE_PATH_SIZE-1 )
  154.                    {
  155.                        sprintf("File path is too long %s/%s. 511 bytes total is maximum.\n",*path,files[count].filename);
  156.                        continue;
  157.                    }
  158.  
  159.                    strcpy(filepath,*path);
  160.                    strcat(filepath,"\\");
  161.                    strcat(filepath,files[count].filename);
  162.  
  163.                    f2 = fopen(filepath, "r");
  164.                    if( f2 == NULL )
  165.                         printf("According to kernels.cfg the file %s cannot be found, program will not be loaded.\n",filepath);
  166.                    else
  167.                         {
  168.                         fseek(f2, 0L, SEEK_END); // get file size
  169.                         fsize2 = ftell(f2);
  170.                         fseek(f2, 0L, SEEK_SET); // go to begin of the file
  171.                         if (fsize2<KERNEL_MIN_SIZE)
  172.                             sprintf("File %s filesize, too small. Expected %d bztes at least.\n",files[count].filename,fsize2);
  173.                         else
  174.                             {
  175.                             /** ALLOCATE MEMORY **/
  176.                             files[count].program = malloc(fsize2);
  177.                             long int pos=0;
  178.                             while ((ch2=fgetc(f2)) != EOF )
  179.                                {
  180.                                 files[count].program[pos]=ch2;
  181.                                 pos++;
  182.                                }
  183.                             // files[count].program[pos+1]='\0';
  184.                             }
  185.                         fclose(f2);
  186.                         }
  187.                }
  188.            count++;
  189.            prevc=c;
  190.            }
  191.     }
  192.     fclose(f);
  193.     // /** ALLOCATE REST OF MEMORY **/
  194.     /*
  195.     prevc=count;
  196.     c=count;
  197.     for (;c<MAX_KERNELS_COUNT; c++)
  198.     {
  199.         files[c].program = malloc(0);
  200.     }
  201.     count++;
  202.     */
  203. return count;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment