1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#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
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[64];
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 )
{
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;
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; int c = 0; int prevc = 0; int count = 0;
char name[64];
while ((ch=fgetc(f)) != EOF )
{
name[c]=ch;
c++;
if (ch == '\n')
{
files[count].name=malloc(64);
files[count].filename=malloc(64);
// strncpy(files[count].name, name, 64);
strcpy(files[count].name,name);
files[count].name[c-prevc-1]='\0'; // remove linefeed \n
strcpy(files[count].filename,name);
// files[count].filename[c-prevc+1]='\0';
files[count].name[c-prevc+4]='\0';
strcat(files[count].filename,".bin");
count++;
prevc=c;
}
}
return count;
}
|