LPBYTE _out_adr; DWORD _out_cur_size, _out_max_size; void _out_set(LPVOID adresse, DWORD taille_max) { _out_adr=(LPBYTE)adresse; _out_max_size=taille_max; _out_cur_size=0; } void _out_ecrit8(int c) { if (_out_cur_size+1>=_out_max_size) return; _out_adr[_out_cur_size++]=c; } int _out_lit8() { if (_out_cur_size+1>=_out_max_size) return -1; return _out_adr[_out_cur_size++]; } void _out_seek(DWORD pos) { _out_cur_size=pos; } //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=_out_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; } int fConvTxtTo89t(char *source, 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, *orig; //Trouve le dernier point et le supprime char *extension; strcpy(tempString,destFile); extension=strrchr(tempString,'.'); *extension=0; //Ouverture de l'original (lecture) orig=fopen(source, "rt"); //Non trouvé? if (!orig) { printf("Impossible d'ouvrir %s en écriture",source); return -1; } //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 :) while (1) { int c=fgetc(orig); if (c==-1 || c==0) { break; } 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 fputc(0x20, final); //Line type. checksum+=0x20; checksum+=0x0d; } else if (c!=0x0d) { fputc(c, final); checksum+=c; } } 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); fclose(final); fclose(orig); return 0; } /*void fConvTxtTo89t(char *source, char * folderName, char *destFile) { const int MAX_TEXTE=65536; char *charge; FILE *f; charge = (char*)malloc(MAX_TEXTE); memset(charge,0,MAX_TEXTE); //Conversion en txt f = fopen(source,"r"); fread (charge,1,MAX_TEXTE,f); _out_set (charge, MAX_TEXTE); ConvTxtTo89t(folderName,destFile); }*/ //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; } _out_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=_out_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=_out_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; }