Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "include/types.h"
- #include "include/gaussian.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <dirent.h>
- #include <string.h> // strcmp, strtok
- #include <sys/types.h> // mkdir
- #include <sys/stat.h> // mkdir
- void addDirectory(const char *dir, char ** toDir);
- void addDirectory(const char *dir, char ** toDir){
- char workingPath [256] = "test";
- strcpy( *toDir, workingPath );
- strcat( *toDir, "\\" );
- strcat( *toDir, dir );
- }
- int makeConfig(char ** path)
- {
- char configFile [12] = "kernels.cfg";
- DIR *dir;
- struct dirent *ent;
- int count = -1;
- if ( ( dir = opendir (*path) ) != NULL)
- {
- char ch;
- FILE* f;
- FILE* f2;
- f = fopen(configFile, "r");
- if( f != NULL ) // if config already exists, backup
- {
- f2 = fopen("kernels.bak", "w");
- while( ( ch = fgetc(f) ) != EOF )
- fputc(ch, f2);
- fclose(f);
- fclose(f2);
- }
- // write new file
- f = fopen(configFile, "w");
- if( f == NULL )
- perror("Cannot write to file kernels.cfg");
- int ckey = 0;
- size_t len;
- while ((ent = readdir (dir)) != NULL)
- {
- if ( ckey<2 && ( strcmp(".",ent->d_name) == 0 || strcmp("..",ent->d_name) == 0 ) )
- { ckey++; continue; }
- // backup ent->d_name because chTmp2 is changed by strtok
- len = strlen(ent->d_name);
- // char * dot = strchr( ent->d_name, '.' );
- if ( len >4 )
- {
- char name[KERNEL_FILENAME_SIZE];
- if (strlen(ent->d_name)>KERNEL_FILENAME_SIZE-1)
- {
- printf("Incorrect kernel source file name.%s , file name size can be max. %d bytes long.\n",ent->d_name,KERNEL_FILENAME_SIZE-1);
- return -4;
- }
- /*
- char * pName = ent->d_name;
- char * ext;
- ext = pName + len - 3;
- */
- memcpy( name, ent->d_name, len-3 );
- if ( 0 == strcmp( ".", name) )
- {
- printf("Incorrect kernel source name. File name %s, must not contain dots, but must have .cl extension.\n",ent->d_name);
- }
- else
- {
- name[len-3]='\0';
- fprintf(f, "%s\n", name);
- printf (".'%s'\n", ent->d_name);
- }
- }
- else
- {
- printf("Incorrect kernel source name. File name %s too short.\n",ent->d_name);
- }
- }
- closedir (dir); fclose(f);
- }
- else
- {
- /* could not open directory */
- char error [255];
- sprintf(error, "Directory not found: %s", *path);
- perror (error);
- return -1;
- }
- return count;
- }
- int readConfig(char ** path, FILES * files, bool readPrograms, SETTINGS settings )
- {
- char configFile [12] = "kernels.cfg";
- DIR *dir;
- if ( ( dir = opendir (*path) ) == NULL)
- {
- perror("directory of binary kernels not found");
- return -2;
- }
- else
- closedir(dir);
- FILE* f;
- FILE* f2;
- f = fopen(configFile, "r");
- if( f == NULL ) // if config already exists, backup
- {
- perror("Config file kernels.cfg not found. Run the program with -install argument.");
- return -3;
- }
- int ch,ch2; int c = 0; int prevc = 0; int count = 0;
- long int fsize2;
- char name[KERNEL_FILENAME_SIZE];
- while ((ch=fgetc(f)) != EOF )
- {
- name[c-prevc]=ch;
- c++;
- if (ch == '\n' && c-prevc>1 )
- {
- // files[count].name=malloc(KERNEL_FILENAME_SIZE);
- // files[count].filename=malloc(KERNEL_FILENAME_SIZE);
- // strncpy(files[count].name, name, KERNEL_FILENAME_SIZE);
- strcpy(files[count].name,name);
- files[count].name[c-prevc-1]='\0'; // remove linefeed \n
- if (strlen(files[count].name)>KERNEL_FILENAME_SIZE-4)
- {
- 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);
- return -4;
- }
- strcpy(files[count].filename,name);
- files[count].filename[c-prevc-1]='\0'; // remove linefeed: \n
- sprintf(files[count].filename,"%s_%d.bin",files[count].filename,settings.load_device_id);
- /** -install don't require reading of programs **/
- if (readPrograms)
- {
- char filepath [KERNEL_TOTAL_FILE_PATH_SIZE];
- if (strlen(*path) + 1 + strlen(files[count].filename) > KERNEL_TOTAL_FILE_PATH_SIZE-1 )
- {
- sprintf("File path is too long %s/%s. 511 bytes total is maximum.\n",*path,files[count].filename);
- continue;
- }
- strcpy(filepath,*path);
- strcat(filepath,"\\");
- strcat(filepath,files[count].filename);
- f2 = fopen(filepath, "r");
- if( f2 == NULL )
- printf("According to kernels.cfg the file %s cannot be found, program will not be loaded.\n",filepath);
- else
- {
- fseek(f2, 0L, SEEK_END); // get file size
- fsize2 = ftell(f2);
- fseek(f2, 0L, SEEK_SET); // go to begin of the file
- if (fsize2<KERNEL_MIN_SIZE)
- sprintf("File %s filesize, too small. Expected %d bztes at least.\n",files[count].filename,fsize2);
- else
- {
- /** ALLOCATE MEMORY **/
- files[count].program = malloc(fsize2);
- long int pos=0;
- while ((ch2=fgetc(f2)) != EOF )
- {
- files[count].program[pos]=ch2;
- pos++;
- }
- // files[count].program[pos+1]='\0';
- }
- fclose(f2);
- }
- }
- count++;
- prevc=c;
- }
- }
- fclose(f);
- // /** ALLOCATE REST OF MEMORY **/
- /*
- prevc=count;
- c=count;
- for (;c<MAX_KERNELS_COUNT; c++)
- {
- files[c].program = malloc(0);
- }
- count++;
- */
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment