LPBYTE _out_adr; DWORD _out_cur_size, _out_max_size; LPBYTE _in_adr; DWORD _in_cur_size, _in_max_size; #define _out_putc _out_ecrit8 void _out_set(LPVOID adresse, DWORD taille_max) { _out_adr=(LPBYTE)adresse; _out_max_size=taille_max; _out_cur_size=0; } void _in_set(LPVOID adresse, DWORD taille_max) { _in_adr=(LPBYTE)adresse; _in_max_size=taille_max; _in_cur_size=0; } void _out_ecrit8(int c) { if (_out_cur_size+1>=_out_max_size) return; _out_adr[_out_cur_size++]=c; } int _in_lit8() { if (_in_cur_size>=_in_max_size) return -1; return _in_adr[_in_cur_size++]; } void _out_seek(DWORD pos) { _out_cur_size=pos; } DWORD _out_tell() { return _out_cur_size; } void _in_seek(DWORD pos) { _in_cur_size=pos; } void _out_puts(const char *texte) { while(_out_cur_size+1<_out_max_size && *texte) _out_adr[_out_cur_size++]=*texte++; } void _out_write(const void *donnees, int taille) { const char *data=(const char*)donnees; while(_out_cur_size+1<_out_max_size && taille--) _out_adr[_out_cur_size++]=*data++; } void _out_printf(const char *format, ...) { char vbuf[5000]; va_list arg_ptr; va_start(arg_ptr, format); _vsnprintf(vbuf, sizeof(vbuf), format, arg_ptr); _out_puts(vbuf); } //Convertit un TXT en 89T. Le TXT doit se trouver dans le buffer _out. //This assumes that folderName is no more than 8 characters long. (That's checked by the code calling this) //When this is called, we should be in the folder already. (HandleFile('people','bob.txt') would be called after we have gone into the people folder.) int ConvTxtTo89t(char * folderName, char *destFile) { //First, see if there's a .89p file with the same name int a; char tempString[MAX_PATH], tempString2[MAX_PATH]; FILE *final; //Copie le nom du fichier vers les chaînes temporaires // strcpy(tempString, fileName); //Remplace ".txt" par ".89t" (à partir de tempString2 qui contiendra le fichier destination) /* strcpy(tempString2, tempString); char *extension=strrchr(tempString2,'.'); strcpy(extension,".89t"); //Essaie d'ouvrir le .89t pour voir s'il existe FILE* pfile=fopen(tempString2,"rt"); if (pfile!=NULL) { fclose(pfile); return 1; //Erreur }*/ /* //Ouverture de l'original (lecture) orig=fopen(tempString, "rt"); //Non trouvé? if (!orig) { printf("Impossible d'ouvrir %s en écriture",tempString); return -1; }*/ //Trouve le dernier point et le supprime char *extension; strcpy(tempString,destFile); extension=strrchr(tempString,'.'); *extension=0; //Contrôle si le nom excède 8 caractères. if (strlen(tempString)>8) { printf("Le nom %s est trop long (il ne doit pas excéder 8 caractères sans l'extension)",tempString); // fclose(orig); return 2; } //Ouvre le fichier de destination final=fopen(destFile, "wb"); if (final==NULL) { printf("Impossible d'ouvrir %s en écriture",destFile); // fclose(orig); return -1; } //Commence à écrire le fichier de destination fprintf(final,"%s","**TI92P*"); //Séparateur fputc(0x01,final); fputc(0x00,final); //Ecrit le nom de dossier int slen=strlen(folderName); for (a=0; a41) tempString2[41]=0; //Nom final fprintf(final,"%s",tempString2); for (a=slen; a<40; a++) { //buffer it with nulls fputc(0,final); } //Toujours identique car le .89t ne contient qu'une variable (séparateur) fputc(0x01, final); fputc(0x00, final); fputc(0x52, final); fputc(0x00, final); fputc(0x00, final); fputc(0x00, final); //Maintenant il faut écrire le nom de fichier dans la variable (toujours 8 caractères) slen=strlen(tempString); for (a=0; a 0x56 depuis le début fwrite(&vide, 2, 1, final); //Le fichier commence! Informations sur la position du curseur. fputc(0x00, final); fputc(0x01, final); checksum+=0x01; //Un espace (début de ligne) fputc(0x20, final); checksum+=0x20; //On commence le texte :) // int lineStart=2; while (1) { int c=_in_lit8(); if (c==-1 || c==0) { break; /* } else if (lineStart==2 && c==0x0c) { fputc(0xc, final); checksum+=0xc; lineStart=1; } else if (lineStart==2 && c==0x0e) { fputc(0x43, final); checksum+=0x43; lineStart=1; } else if (lineStart==2 && c==0x0f) { fputc(0x50, final); checksum+=0x50; lineStart=1; } else if (c==9) { //tab - I wonder if the TI-89 can handle tab characters... In any event, ToTxt does make them from leading spaces, and so it's no problem if that's where they occur. If they were after some text or something, though, they'd get changed to a space here and not changed back by ToTxt later, which might be annoying to some people. I would hope they would complain, if that's the case, though. Or modify the code to their liking, if they knew how (Ain't open-source great?). if (lineStart==2) { fputc(0x20, final); //Line type. checksum+=0x20; lineStart=1; } if (lineStart==1) { fputc(0x20, final); checksum+=0x20; } else { fputc(0x09, final); checksum+=0x09; }*/ } else if (c=='\n') { //J'ai inversé l'ordre... fputc(0x0d, final); //You know, I really have no idea whether \n is 0x0a or 0x0d, but the TI-89 always wants 0x0d, so... :P // if (lineStart==2) { fputc(0x20, final); //Line type. checksum+=0x20; // } // fputc(0x0d, final); //You know, I really have no idea whether \n is 0x0a or 0x0d, but the TI-89 always wants 0x0d, so... :P checksum+=0x0d; // lineStart=2; } else { // if (lineStart==2) { // fputc(0x20, final); //Line type. // checksum+=0x20; // } //We aren't going to bother to change back all the things ToTxt does. The readme says what symbols ToTxt changes to what text, by the way. fputc(c, final); checksum+=c; // lineStart=0; } } fputc(0x00, final); fputc(0xE0, final); checksum+=0xE0; //write blank where checksum goes int checksumPos=ftell(final); fwrite(&vide, 2, 1, final); int filesize=ftell(final); fseek(final, 0x4c, SEEK_SET); //fwrite(&filesize, 4, 1, final); fputc(filesize&0xff, final); fputc((filesize>>8)&0xff, final); fputc((filesize>>16)&0xff, final); fputc((filesize>>24)&0xff, final); int textsize=filesize-0x5a; //this should give the correct result fseek(final, 0x56, SEEK_SET); fputc((textsize>>8)&0xff, final); fputc((textsize)&0xff, final); checksum+=(textsize&0xff)+((textsize>>8)&0xff); //write checksum fseek(final, checksumPos, SEEK_SET); fputc(checksum&0xff, final); fputc((checksum>>8)&0xff, final); //It seems that we can write the upper two bytes too, and the TI-89 doesn't complain. Heh. But it may not be healthy still, so we won't (I did try it, though, just to see). //fputc((checksum>>16)&0xff, final); //fputc((checksum>>24)&0xff, final); fclose(final); // fclose(orig); return 0; } //Convertir un 89T en TXT. Le texte soit se trouver dans le buffer _out. int Conv89tToTxt(char *dest) { FILE *final; final=fopen(dest, "wt"); if (final==NULL) { printf("Impossible d'ouvrir %s en écriture",dest); return -1; } _in_seek(0x5a); int start=1; //start would be 2 if we weren't jumping past the first 'start--' statement initially. int wasNL=2; int startOverride=1; int c; while (1) { start--; c=_in_lit8(); notspace: if (c==-1) { break; } else if (c==0x0d || startOverride) { if (startOverride) { //we need to run this code on the character at 0x5a, but there's no preceding 0x0d. startOverride=0; } else { c=_in_lit8(); //line type fputc('\n',final); } if (c==0x20) { wasNL=1; continue; } else if (c==0x43) { fputc(0x0e,final); wasNL=1; continue; } else if (c==0x50) { fputc(0x0f,final); wasNL=1; continue; } else { wasNL=1; goto notspace; } } else if (c==0) { if (start) { //There's a 0 at 0x5b - Happens for blank programs (lacking Prgm and EndPrgm), and for tokenized ones. //get length /* fseek(orig, 0, SEEK_END); long filelen=ftell(orig); if (filetype=='p') { if (filelen<=103) { //empty 89p fprintf(final, "This program file appears to be empty!"); } else { fprintf(final, "This program file is tokenized. We can't translate tokenized files. If you want this program file translated, please open it in the TI-89 program editor (to untokenize it), and copy it to the computer & try converting it again. (Programs are tokenized when they are run, and untokenized if they're opened in the program editor).\n"); } }*/ } //eof break; /* } else if (wasNL) { if (c==0x20) { fputc('\t',final); continue; } else { wasNL=0; }*/ } /* if (c==0xad) { //other - fputc(0x2d,final); } else if (c==0x8c) { //PI fputc('P',final); fputc('I',final); } else if (c==0x9d) { //"Not equals to" (Diamond-equals) fputc('!',final); fputc('=',final); } else if (c==0x16) { //-> fputc('-',final); fputc('>',final); } else if (c==0x9c) { //<= fputc('<',final); fputc('=',final); } else if (c==0x9e) { //>= fputc('>',final); fputc('=',final); } else if (c==0xa8) { //square root fputc('s',final); fputc('q',final); fputc('r',final); fputc('t',final); } else {*/ fputc(c,final); // } } fclose(final); return 0; } int IOConv89tToTxt() { _in_seek(0x5a); int start=1; //start would be 2 if we weren't jumping past the first 'start--' statement initially. int wasNL=2; int startOverride=1; int c; while (1) { start--; c=_in_lit8(); notspace: if (c==-1) { break; } else if (c==0x0d || startOverride) { if (startOverride) { //we need to run this code on the character at 0x5a, but there's no preceding 0x0d. startOverride=0; } else { c=_in_lit8(); //line type _out_ecrit8('\n'); } if (c==0x20) { wasNL=1; continue; } else if (c==0x43) { _out_ecrit8(0x0e); wasNL=1; continue; } else if (c==0x50) { _out_ecrit8(0x0f); wasNL=1; continue; } else { wasNL=1; goto notspace; } } else if (c==0) { if (start) { //There's a 0 at 0x5b - Happens for blank programs (lacking Prgm and EndPrgm), and for tokenized ones. //get length /* fseek(orig, 0, SEEK_END); long filelen=ftell(orig); if (filetype=='p') { if (filelen<=103) { //empty 89p fprintf(final, "This program file appears to be empty!"); } else { fprintf(final, "This program file is tokenized. We can't translate tokenized files. If you want this program file translated, please open it in the TI-89 program editor (to untokenize it), and copy it to the computer & try converting it again. (Programs are tokenized when they are run, and untokenized if they're opened in the program editor).\n"); } }*/ } //eof break; /* } else if (wasNL) { if (c==0x20) { fputc('\t',final); continue; } else { wasNL=0; }*/ } /* if (c==0xad) { //other - fputc(0x2d,final); } else if (c==0x8c) { //PI fputc('P',final); fputc('I',final); } else if (c==0x9d) { //"Not equals to" (Diamond-equals) fputc('!',final); fputc('=',final); } else if (c==0x16) { //-> fputc('-',final); fputc('>',final); } else if (c==0x9c) { //<= fputc('<',final); fputc('=',final); } else if (c==0x9e) { //>= fputc('>',final); fputc('=',final); } else if (c==0xa8) { //square root fputc('s',final); fputc('q',final); fputc('r',final); fputc('t',final); } else {*/ _out_ecrit8(c); // } } return 0; } int IOConvTxtTo89t(char * folderName, char *destFile) { //First, see if there's a .89p file with the same name int a; char tempString[MAX_PATH], tempString2[MAX_PATH]; //Trouve le dernier point et le supprime char *extension, *derSlash, *ptr; strcpy(tempString,destFile); extension=strrchr(tempString,'.'); *extension=0; //Trouve le dernier slash ptr=tempString; derSlash=tempString; while (*ptr) { if (*ptr=='\\') derSlash=ptr+1; ptr++; } //Contrôle si le nom excède 8 caractères. if (strlen(derSlash)>8) { printf("Le nom %s est trop long (il ne doit pas excéder 8 caractères sans l'extension)",derSlash); // fclose(orig); return 2; } //Commence à écrire le fichier de destination _out_printf("%s","**TI92P*"); //Séparateur _out_putc(0x01); _out_putc(0x00); //Ecrit le nom de dossier int slen=strlen(folderName); for (a=0; a41) tempString2[41]=0; //Nom final _out_printf("%s",tempString2); for (a=slen; a<40; a++) { //buffer it with nulls _out_putc(0); } //Toujours identique car le .89t ne contient qu'une variable (séparateur) _out_putc(0x01); _out_putc(0x00); _out_putc(0x52); _out_putc(0x00); _out_putc(0x00); _out_putc(0x00); //Maintenant il faut écrire le nom de fichier dans la variable (toujours 8 caractères) slen=strlen(derSlash); for (a=0; a 0x56 depuis le début _out_write(&vide, 2); //Le fichier commence! Informations sur la position du curseur. _out_putc(0x00); _out_putc(0x01); checksum+=0x01; //Un espace (début de ligne) _out_putc(0x20); checksum+=0x20; //On commence le texte :) // int lineStart=2; while (1) { int c=_in_lit8(); if (c==-1 || c==0) { break; /* } else if (lineStart==2 && c==0x0c) { fputc(0xc, final); checksum+=0xc; lineStart=1; } else if (lineStart==2 && c==0x0e) { fputc(0x43, final); checksum+=0x43; lineStart=1; } else if (lineStart==2 && c==0x0f) { fputc(0x50, final); checksum+=0x50; lineStart=1; } else if (c==9) { //tab - I wonder if the TI-89 can handle tab characters... In any event, ToTxt does make them from leading spaces, and so it's no problem if that's where they occur. If they were after some text or something, though, they'd get changed to a space here and not changed back by ToTxt later, which might be annoying to some people. I would hope they would complain, if that's the case, though. Or modify the code to their liking, if they knew how (Ain't open-source great?). if (lineStart==2) { fputc(0x20, final); //Line type. checksum+=0x20; lineStart=1; } if (lineStart==1) { fputc(0x20, final); checksum+=0x20; } else { fputc(0x09, final); checksum+=0x09; }*/ } else if (c=='\n') { //J'ai inversé l'ordre... _out_putc(0x0d); //You know, I really have no idea whether \n is 0x0a or 0x0d, but the TI-89 always wants 0x0d, so... :P // if (lineStart==2) { _out_putc(0x20); //Line type. checksum+=0x20; // } // fputc(0x0d, final); //You know, I really have no idea whether \n is 0x0a or 0x0d, but the TI-89 always wants 0x0d, so... :P checksum+=0x0d; // lineStart=2; } else if (c!=0xd) { // if (lineStart==2) { // fputc(0x20, final); //Line type. // checksum+=0x20; // } //We aren't going to bother to change back all the things ToTxt does. The readme says what symbols ToTxt changes to what text, by the way. _out_putc(c); checksum+=c; // lineStart=0; } } _out_putc(0x00); _out_putc(0xE0); checksum+=0xE0; //write blank where checksum goes int checksumPos=_out_tell(); _out_write(&vide, 2); int filesize=_out_tell(); _out_seek(0x4c); //fwrite(&filesize, 4, 1, final); _out_putc(filesize&0xff); _out_putc((filesize>>8)&0xff); _out_putc((filesize>>16)&0xff); _out_putc((filesize>>24)&0xff); int textsize=filesize-0x5a; //this should give the correct result _out_seek(0x56); _out_putc((textsize>>8)&0xff); _out_putc((textsize)&0xff); checksum+=(textsize&0xff)+((textsize>>8)&0xff); //write checksum _out_seek(checksumPos); _out_putc(checksum&0xff); _out_putc((checksum>>8)&0xff); //It seems that we can write the upper two bytes too, and the TI-89 doesn't complain. Heh. But it may not be healthy still, so we won't (I did try it, though, just to see). //fputc((checksum>>16)&0xff, final); //fputc((checksum>>24)&0xff, final); // fclose(orig); return 0; }