Changeletters.c

A small tool I wrote to turn my texts into proper danish text. I use the Dvorak keyboard layout and thus have no danish letters available. Instead I just write my text with 'ae, 'oe and 'aa and run this program on the finished .tex file (yes, I also write in LaTeX). The below code (happy and with strange unfounded workarounds) is from 2005 when I was still a freshman. I still use it regularly when writing in danish.

/////////////////////////////////////////////////
/*   changeletters mkI.01
   by: Jes Toft Kristensen
   date: 040212 (modified 040305)
   mail: lookOnThisPage
   www:  buskefjomp.dk
   description:
   Small tool for changing 'ae, 'Oe etc. (danish
     letters) into their real types.
 
   Takes the target file as parameter, and writes
   backup file "backup.bac".
 
   Exchange scheme:
   'Ae = Æ, 'Oe =  Ø, 'Aa = Å, 
   'ae = æ, 'oe =  ø, 'aa=  å
 
   USE THIS AT YOUR OWN RISK!
 
   Modified 040305 (to version I.01)
   Changed default ' to '\'', instead of 101
   (we don't want e printed, we want ' )
   This should make the program fully operational
*/
/////////////////////////////////////////////////
 
 
#include <stdio.h>    // filo io
#include <stdlib.h>   // for the exit function
 
main (int argc, char *argv[])
{
  ///// Input checking /////
  if (argc != 2)
    {
      printf("\nusage: Changeletters <filename.foo> \n");
      printf("Exchange scheme in files:             \n");
      printf("'Ae = Æ, 'Oe =  Ø, 'Aa = Å,           \n");
      printf("'ae = æ, 'oe =  ø, 'aa =  å           \n");  
      printf("\nand writes backup file <backup.bac> \n ");
      printf("\nUSE THIS AT YOUR OWN RISK! \n");
      exit(1);
    }
 
 
  ///// Get file length /////
 
  FILE *inputfile;
  inputfile=fopen(argv[1], "r");
 
 
  int c, t = 0;            // t file length specifier, c general character holding
  int *pt = &t;            // pointer to file length specifier t
  while ( getc(inputfile) != EOF  )
    {                      // get input until file-end, increment t meanwhile
      *pt = (*pt + 1);
    }
 
  fclose(inputfile);
 
 
 
  ///// Read file into array /////
 
  FILE *readfile;
  readfile=fopen(argv[1], "r");
 
  int b[t];               // character holder, with length t
 
  int i = 0;              // general increment constant
 
  int *pb = &b[0];        // pointer to character holder (assigned to adress of the holder array).
 
  while ( i <= t )
    {                     // read characters into b[], via the pointer
      *(pb+i) = getc(readfile);
      i++;
    }
 
  fclose(readfile);
 
  ///// Backup file writing /////
 
  i = 0;
  FILE *backupfile;
  backupfile=fopen("backup.bac","w");
 
  while ( i < t  )
    {
      putc(*(pb+i),backupfile);
      i++;
  }
  fclose(backupfile);
 
  ///// Tjeck and write to new array /////
 
  int o[t];                 // output char holder, length t << WRONG! NEEDS CORRECTION
                            // length of o[] dependant of number of exchanges
  int *po = &o[0];          // pointer to output char holder
  i = 0;                    // reset increment
  int j = 0;                // output holders' increment
  int *pj = &j;             // pointer to output holders increment
  while ( i <= t )
    {
      if ( *(pb+i) == '\'' )// check for 'xx constellations, and check afterwards for correct combination
	{                   // ugly, but effective, changed 230 = 101
	       if ( (*(pb+i+1)== 'a') && (*(pb+i+2)== 'e') ){ *(po+ *pj) = 230; i=i+2;} 
	  else if ( (*(pb+i+1)== 'o') && (*(pb+i+2)== 'e') ){ *(po+ *pj) = 248; i=i+2;}
	  else if ( (*(pb+i+1)== 'a') && (*(pb+i+2)== 'a') ){ *(po+ *pj) = 229; i=i+2;} 
	  else if ( (*(pb+i+1)== 'A') && (*(pb+i+2)== 'e') ){ *(po+ *pj) = 198; i=i+2;}
	  else if ( (*(pb+i+1)== 'O') && (*(pb+i+2)== 'e') ){ *(po+ *pj) = 216; i=i+2;}
	  else if ( (*(pb+i+1)== 'A') && (*(pb+i+2)== 'a') ){ *(po+ *pj) = 197; i=i+2;}
	       else {*(po+ *pj) = '\''; } // int '\'' writes ' if no specials were found
 
	  // special character checking and inserting.
	  // integer values:
	  // int 'Æ' = 198
	  // int 'Ø' = 216
	  // int 'Å' = 197
	  // int 'æ' = 230
          // int 'ø' = 248
	  // int 'å' = 229	  
	}
      else { *(po + *pj) = *(pb +i);} // write normal character to output holder
      i++, *pj = (*pj +1);            // make increments to loop
    }
 
 
 
  ///// Write file out /////
 
  FILE *writefile;
  writefile = fopen(argv[1], "w");
  i = 0;                              // reset increment
  while ( i < *pj - 1)                // needs recalculation of upper limit
    {
      c = *(po+i);                    // get character from output holder
      putc(c,writefile);              // put character to output
      i++;                            // weee... increment.
    }
  fclose(writefile);
 
  // // printf("4 \n");
}
 
projects/changeletters.txt · Last modified: 2009/02/07 21:02 by buskefjomp
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki