<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <json-c/json.h>
#include "expressions.h"
#include "booleans.h"

#define MCE_BUFFER 256

void usage(char* name);
double getdoublevar(char* place,char* name,json_object* parent);
int getintvar(char* place,char* name,json_object* parent);
void getexpression(union expression* destination,json_object* json_exp);
void getexpressionarray(union expression** destarray,int* lgth,char* place, char* name, json_object* parent);
void getcondition(union boolean * destination, json_object* json_bool);
void getconditionarray(union boolean** destarray,int* lgth,char* place, char* name, json_object* parent);

int main(int argc, char** argv){

	double width,height,depth;
	long int minx,miny,minz;
	long int maxx,maxy,maxz;
	long int imgw,imgh;

	int vars[4];

	//uint8_t ***picture;

	uint8_t argcounter = 0;
	char argopt;

	char solid[MCE_BUFFER];
	char dir[MCE_BUFFER];
	char file[MCE_BUFFER];

	char* linebuffer;

	int picfd;

	char* json_solid_desc;

	union expression* expressions;
	int explength;

	union boolean condition;

	json_object* the_solid;
	json_object* condobj;

	int solid_fd;
	int solid_length;

	extern char* optarg; /* Variable to store arguments */



	/* Parameter reading: */
	while((argopt = getopt(argc,argv,"+hs:o:f:"))!=-1){
		switch(argopt){
			case 's':
				strncpy(solid,optarg,MCE_BUFFER);
				argcounter++;
				break;
			case 'o':
				strncpy(dir,optarg,MCE_BUFFER);
				argcounter++;
				break;
			case 'f':
				if(sscanf(optarg,"%d",&(vars[3]))==1){
					argcounter++;
					break;
				}
			default:
				usage(argv[0]);
				exit(0);
				break;
		}
	}
	if(argcounter != 3){
		usage(argv[0]);
		exit(-1);
	}

	/* Opening solid file: */

	solid_fd = open(solid,O_RDONLY);
	if(solid_fd < 0){
		perror(argv[0]);
		exit(-1);
	}

	/* Determination of file size: */
	solid_length = lseek(solid_fd,0,SEEK_END);
	if(lseek(solid_fd,0,SEEK_SET)<0){
		perror(argv[0]);
		exit(-1);
	}
#ifdef MCE_DEBUG
	printf("Size: %d\n",solid_length);
#endif

	/* Allocation of memory: */
	json_solid_desc = malloc(solid_length + 1);
	if(read(solid_fd,json_solid_desc,solid_length)!=solid_length){
		perror(argv[0]);
		exit(-1);
	}
	json_solid_desc[solid_length] = 0;
#ifdef MCE_DEBUG
	printf("%s\n",json_solid_desc);
#endif

	/* Parsing of the solid: */
	the_solid = json_tokener_parse(json_solid_desc);

		/* Width, Height, Depth: */
	width = getdoublevar(solid,"width",the_solid);
	height = getdoublevar(solid,"height",the_solid);
	depth = getdoublevar(solid,"depth",the_solid);

	minx = ceil(getdoublevar(solid,"minx",the_solid) * vars[3]);
	miny = ceil(getdoublevar(solid,"miny",the_solid) * vars[3]);
	minz = ceil(getdoublevar(solid,"minz",the_solid) * vars[3]);

		/* Expressions: */
	getexpressionarray(&expressions,&explength,solid, "expressions", the_solid);

		/* Condition: */
	if(!json_object_object_get_ex(the_solid,"condition",&condobj)){
		printf("Error parsing %s, no condition\n",solid);
		exit(-1);
	}

	getcondition(&condition,condobj);

#ifdef MCE_DEBUG
	printf("Finished parsing %s. %f by %f by %f\n",solid,width,height,depth);
#endif

	/* Creating output directory: */

	if(mkdir(dir,0777)<0){
		perror(argv[0]);
	}

	/* Calculations: */

	maxx = minx + ceil(width * vars[3]);
	maxy = miny + ceil(height * vars[3]);
	maxz = minz + ceil(depth * vars[3]);

	imgw = 4 * ((maxx-minx) + 1) + 1;
	imgh = 4 * ((maxy-miny) + 1) + 1;

	//picture = malloc((maxz+1)*sizeof(uint8_t**));

#ifdef MCE_DEBUG
	printf("\n");
#endif
	linebuffer = malloc((imgw+32)*sizeof(char));
	for(vars[2]=minz;vars[2]<=maxz;vars[2]++){

		/* File opening: */
		sprintf(file,"%s/%03d.xpm",dir,vars[2]);
		picfd = open(file,O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
		if(picfd<0){
			printf("Error creating \"%s\".\n",file);
			perror(argv[0]);
			exit(-1);
		}

		//picture[vars[2]]= malloc((maxx+1)*sizeof(uint8_t*));


		/* Printing Header: */
		sprintf(linebuffer,"/* XPM */\n");
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"static char * %03d_xpm[] = {\n",vars[2]);
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"\"%ld %ld 3 1\",\n",imgh,imgw);
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"\"-\tc #ffffff\",\n");
		write(picfd,linebuffer,strlen(linebuffer));<commit_msg>Changed file numbering to be non-negative<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <json-c/json.h>
#include "expressions.h"
#include "booleans.h"

#define MCE_BUFFER 256

void usage(char* name);
double getdoublevar(char* place,char* name,json_object* parent);
int getintvar(char* place,char* name,json_object* parent);
void getexpression(union expression* destination,json_object* json_exp);
void getexpressionarray(union expression** destarray,int* lgth,char* place, char* name, json_object* parent);
void getcondition(union boolean * destination, json_object* json_bool);
void getconditionarray(union boolean** destarray,int* lgth,char* place, char* name, json_object* parent);

int main(int argc, char** argv){

	double width,height,depth;
	long int minx,miny,minz;
	long int maxx,maxy,maxz;
	long int imgw,imgh;

	long int filecounter;

	int vars[4];

	//uint8_t ***picture;

	uint8_t argcounter = 0;
	char argopt;

	char solid[MCE_BUFFER];
	char dir[MCE_BUFFER];
	char file[MCE_BUFFER];

	char* linebuffer;

	int picfd;

	char* json_solid_desc;

	union expression* expressions;
	int explength;

	union boolean condition;

	json_object* the_solid;
	json_object* condobj;

	int solid_fd;
	int solid_length;

	extern char* optarg; /* Variable to store arguments */



	/* Parameter reading: */
	while((argopt = getopt(argc,argv,"+hs:o:f:"))!=-1){
		switch(argopt){
			case 's':
				strncpy(solid,optarg,MCE_BUFFER);
				argcounter++;
				break;
			case 'o':
				strncpy(dir,optarg,MCE_BUFFER);
				argcounter++;
				break;
			case 'f':
				if(sscanf(optarg,"%d",&(vars[3]))==1){
					argcounter++;
					break;
				}
			default:
				usage(argv[0]);
				exit(0);
				break;
		}
	}
	if(argcounter != 3){
		usage(argv[0]);
		exit(-1);
	}

	/* Opening solid file: */

	solid_fd = open(solid,O_RDONLY);
	if(solid_fd < 0){
		perror(argv[0]);
		exit(-1);
	}

	/* Determination of file size: */
	solid_length = lseek(solid_fd,0,SEEK_END);
	if(lseek(solid_fd,0,SEEK_SET)<0){
		perror(argv[0]);
		exit(-1);
	}
#ifdef MCE_DEBUG
	printf("Size: %d\n",solid_length);
#endif

	/* Allocation of memory: */
	json_solid_desc = malloc(solid_length + 1);
	if(read(solid_fd,json_solid_desc,solid_length)!=solid_length){
		perror(argv[0]);
		exit(-1);
	}
	json_solid_desc[solid_length] = 0;
#ifdef MCE_DEBUG
	printf("%s\n",json_solid_desc);
#endif

	/* Parsing of the solid: */
	the_solid = json_tokener_parse(json_solid_desc);

		/* Width, Height, Depth: */
	width = getdoublevar(solid,"width",the_solid);
	height = getdoublevar(solid,"height",the_solid);
	depth = getdoublevar(solid,"depth",the_solid);

	minx = ceil(getdoublevar(solid,"minx",the_solid) * vars[3]);
	miny = ceil(getdoublevar(solid,"miny",the_solid) * vars[3]);
	minz = ceil(getdoublevar(solid,"minz",the_solid) * vars[3]);

		/* Expressions: */
	getexpressionarray(&expressions,&explength,solid, "expressions", the_solid);

		/* Condition: */
	if(!json_object_object_get_ex(the_solid,"condition",&condobj)){
		printf("Error parsing %s, no condition\n",solid);
		exit(-1);
	}

	getcondition(&condition,condobj);

#ifdef MCE_DEBUG
	printf("Finished parsing %s. %f by %f by %f\n",solid,width,height,depth);
#endif

	/* Creating output directory: */

	if(mkdir(dir,0777)<0){
		perror(argv[0]);
	}

	/* Calculations: */

	maxx = minx + ceil(width * vars[3]);
	maxy = miny + ceil(height * vars[3]);
	maxz = minz + ceil(depth * vars[3]);

	imgw = 4 * ((maxx-minx) + 1) + 1;
	imgh = 4 * ((maxy-miny) + 1) + 1;

	//picture = malloc((maxz+1)*sizeof(uint8_t**));

#ifdef MCE_DEBUG
	printf("\n");
#endif

	filecounter = 0;

	linebuffer = malloc((imgw+32)*sizeof(char));
	for(vars[2]=minz;vars[2]<=maxz;vars[2]++,filecounter++){

		/* File opening: */
		sprintf(file,"%s/%03ld.xpm",dir,filecounter);
		picfd = open(file,O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
		if(picfd<0){
			printf("Error creating \"%s\".\n",file);
			perror(argv[0]);
			exit(-1);
		}

		//picture[vars[2]]= malloc((maxx+1)*sizeof(uint8_t*));


		/* Printing Header: */
		sprintf(linebuffer,"/* XPM */\n");
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"static char * %03d_xpm[] = {\n",vars[2]);
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"\"%ld %ld 3 1\",\n",imgh,imgw);
		write(picfd,linebuffer,strlen(linebuffer));
		sprintf(linebuffer,"\"-\tc #ffffff\",\n");
		write(picfd,linebuffer,strlen(linebuffer));
{'fraction_non_alphanumeric': 0.18019535197036038, 'fraction_numerical': 0.015044347142696755, 'mean_word_length': 5.118131868131868, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5666409', 'n_tokens_mistral': 3780, 'n_tokens_neox': 3454, 'n_words': 637}