/* * Convert.c (c) 1991 Hayco 'Xyquary' de Jong. * This file was originally created to help convert between the BSXdraw files. * Later it grew to a more extensive tool, in which the .xpf (see below) * format was created by me (Xyquary). I find it useful to make small patches * to drawings, when, for instance I want to change the colour of a polygon, * or when I have made a small error while drawing one. * You may freely distribute use, copy and modify this program, but when you * have interesting ideas, or otherwise suggestions which may contribute * to this program, or when you think something is missing that might be * helpful to others, please mail me (see below for address) so I can implement * the changes and have Bram Stolk upload the new version to the ftp-site. * * The program was written and compiled with the Gnu C-compiler, but since * it is all ANSI standard, the cc compiler (which most of you probably * have on your system) will also understand it. * * For more information on the .mud files I keep referring to the file * /doc/graphics. Don't start looking for that file in this program, * it is located (as far as I know) in the mud itself! If you can't * find that file you'd better mail Bram Stolk (stolk@fwi.uva.nl) since * he's the one that probably moved it, or otherwise knows where it is. * Should this file be moved, and you notice that this program still * has the old location, please mail me, so I can change it, since I * no longer need the file and chances are that I won't look at the * /doc/graphics file for a long time :-) * * Bugs, ideas, credits, words of gratitude etc to: jong@fwi.uva.nl * */ /* Mail me an idea or a bug and this number *will* change ;-) */ #define VERSION 3.1 /* Include files. Most of them are needed for the registration- * routine. */ #include #include #include #include #include #include /* These defines are the extensions which are used on the files. * Should you whish to use your own extensions, simply change them * here and recompile. * Note: you must then of course, rename the files with 'old' extensions * to your own. */ #define PPF "ppf" /* Standard .ppf files, as generated by BSXdraw. */ #define BSX "bsx" /* Standard .bsx files, as generated by BSXdraw. */ #define MUD "mud" /* Ready-to-use mud files, see /doc/graphics. */ #define XPF "xpf" /* Xyquary's Personal Format (*smile*) */ /* Two filepointers to the files which are used. */ FILE *in; FILE *out; /* This is the colourlist I extracted from 'xtool.c' in the * bsxclient which was in the ftp-site. Should the colourlist * ever change, just change this list accordingly. */ char *colournames[]={ "Black", "Blue", "ForestGreen", "SkyBlue", "IndianRed", "HotPink", "Brown", "LightGrey", "DimGrey", "DeepSkyBlue", "Green", "Cyan", "Tomato", "Magenta", "Yellow", "White" }; /* Simple routine to check whether a character is hexadecimal or not. * Lowercase characters 'a..f' are also considered to be hex. * Only useful for single 'digits'. */ int ishex(c) char c; { return ((c>='0'&&c<='9')||(c>='A'&&c<='F')||(c>='a'&&c<='f')); } /* The following routine searches a file for a given string just like * the Unix 'grep' command. It returns 1 when the string is in the file * and then the filepointer is set just after where the string was found. * If the string does not occur in the string then a zero is returned. * I am aware of the Boyer-Moore algorithm and when I have time I will * probably implement it, although I am not sure if it will yield a * significant increase of search time, since I mostly search for short * strings. (hmm, actually I only use it once as I see it now...:-) ) */ int grep(file, target) FILE *file; char *target; { char *buf,c; int lcv,len,found=0; len=strlen(target); if(!(buf=(char *)malloc(len))){ fprintf(stderr,"Unable to allocate memory.\n"); exit(1); } fread(buf,sizeof(char),len,file); while(!feof(file)){ if(!(strncmp(buf,target,len))){ found=1; break; } for(lcv=0; lcv='0'&&hex<='9') return hex-'0'; if(hex>='A'&&hex<='F') return 10+(hex-'A'); if(hex>='a'&&hex<='f') return 10+(hex-'a'); fprintf(stderr,"Warning: file contains non-hex characters!\n"); return -1; } /* The following routine converts decimal values to hexadecimal characters. * Only values in the range 0..15 are understood. If larger or smaller numbers * are given, an error message will be dumped and 'X' will be returned. */ char dec2hex(dec) int dec; { if(dec>=0&&dec<10) return dec+'0'; if(dec>=10&&dec<16) return 'A'+(dec-10); fprintf(stderr,"Can't convert '%d' to hexadecimal.\n",dec); return 'X'; } /* The following routine attempts to open the two major files in this program. * The three arguments that are passed are: the 'basic'-filename (without * extensions which was provided upon calling this program). The second is * the extension to the filename from which input is to be read and finally * the third argument is the extension to the filename to which output is * directed. Not being able to open one of these files is such a major * error, that the program is unable to continue, so an errormessage is * dumped and the program terminates. * I used dynamic allocation, so that when you change one or more of the * defines for extention names to a length of greater than 3 the program * will not just crash, but will work just as good. (I hate using arrays * of fixed length). */ void OpenFiles(name, from, to) char *name; char *from; char *to; { char *filename; int len; len=strlen(from); if(strlen(to)>len) len=strlen(to); len+=strlen(name)+1; if(!(filename=(char *)malloc(len))){ fprintf(stderr,"Unable to allocate memory.\n"); exit(1); } sprintf(filename,"%s.%s",name,from); if(!(in=fopen(filename,"r"))){ fprintf(stderr,"Unable to open file '%s' for reading.\n",filename); exit(1); } sprintf(filename,"%s.%s",name,to); if(!(out=fopen(filename,"w"))){ fprintf(stderr,"Unable to open file '%s' for writing.\n",filename); exit(1); } } /* The following routine closes the files which were used for input * and output and prints the ever so boring 'Done' message. */ void CloseAndQuit() { if(in) fclose(in); if(out) fclose(out); printf("Done.\n"); exit(0); } /* The following routine converts a '.bsx' file to a '.col' file. * On the first line of the latter file, the total number of polygons * is found. On the following line the format is as follows: * NO HEXADECIMAL VALUES ARE IN THIS FORMAT! * I did this since there are many non-computer science students, for * whom having to learn and understand the hexadecimal system is quite * problematic, although this is hard to understand for us computer- * scientists. (Thanks to Chris 'Freak' Craven for bringing this to my * attention). * - First decimalvalue : number of dots the polygon has. * - Second string : the colour this polygon has. * - Rest of decimalvalues: the coordinates of the dots in the polygon. * These coordinates are grouped into (x,y) format. * When changing this file, please take care with upper- and lowercase. */ void bsx2xpf(name) char *name; { int lcv, i, number, colour; char c, pair[2]; OpenFiles(name,BSX,XPF); fscanf(in,"%2s",pair); fprintf(out,"%d\n",hex2dec(pair[0])*16+hex2dec(pair[1])); for(pair[0]=getc(in); !feof(in); pair[0]=getc(in)){ pair[1]=getc(in); number=hex2dec(pair[0])*16+hex2dec(pair[1]); fprintf(out,"%d ",number); fscanf(in,"%2s",pair); colour=hex2dec(pair[0])*16+hex2dec(pair[1]); if(colour<0||colour>15){ fprintf(stderr,"Illegal colour '%d' detected.\n",colour); fprintf(out,"%2s",pair); }else fprintf(out,"%s",colournames[colour]); putc(' ',out); for(lcv=0; lcvh_addr_list)); if(!(hp=gethostbyaddr(&ia,4,AF_INET))){ fprintf(stderr,"Unable to resolve hostnumber.\n"); exit(1); } pwd=getpwuid(getuid()); for(lcv=0; hp->h_name[lcv]!='.'; lcv++) ; if(!(email=(char *)malloc(strlen(pwd->pw_name)+strlen(&hp->h_name[++lcv])+2))){ fprintf(stderr,"Unable to allocate memory.\n"); exit(1); } sprintf(email,"%s@%s",pwd->pw_name,&hp->h_name[lcv]); printf("My research tells me your email-address is: '%s'.\n",email); printf("If this is correct, type 'y' now.\n"); rewind(stdin); if(getchar()!='y'){ fprintf(stderr,"I'm sorry, but I cannot grant your request using\n"); fprintf(stderr,"the fully-automated daemon. Please send\n"); fprintf(stderr,"a mail yourself to 'jong@fwi.uva.nl'.\n"); exit(1); } /* PLEASE DO NOT MISUSE THE NEXT LINE. I KNOW IT IS 'FUNNY' TO SEND NONSENSE * BUT DON'T RUIN THIS SERVICE FOR OTHER PEOPLE, USE IT SERIOUSELY, THANKS */ sprintf(command,"echo \"%s\t%d\" | mail -s \"RequestDaemon\" jong@fwi.uva.nl",email,(int)(VERSION*100)); system(command); CloseAndQuit(); } /* The one that does it all... */ int main(argc,argv) int argc; char *argv[]; { FILE *in, *out; char filename[64]; char option; if(argc==2){ if(!(strcmp(argv[1],"-help"))) GiveHelp(); if(!(strcmp(argv[1],"-col"))) GiveColourList(); if(!(strcmp(argv[1],"-request"))) Register(); } if(argc==3){ sscanf(argv[1],"-%c",&option); switch(option){ case 'p': bsx2ppf(argv[2]); break; case 'b': ppf2bsx(argv[2]); break; case 'c': bsx2xpf(argv[2]); break; case 'm': bsx2mud(argv[2]); break; case 's': xpf2bsx(argv[2]); break; case 'x': mud2bsx(argv[2]); break; } } fprintf(stderr,"Usage: %s [-bsxpcm] [file].\n%s -help gives you some help.\n%s -col gives you colourlist.\n%s -request for sending a request for newest version\n",argv[0],argv[0],argv[0],argv[0]); exit(1); }