Wednesday, July 8, 2020

Text File To RTF in C

A program was written by Ben a.k.a DreamVB that I like to share a small tool to convert text files to RTF files using a C programming language.

I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below.https://www.unlimitedbooksph.com/

Program Listing

/*
  TXT2RTF
  A small tool to convert text files to RTF files.

  Author:    Ben a.k.a DreamVB
  Version:   1.0
  Type:      Open Source
  Contact:   dreamvb@outlook.com {for any questions regarding this program}
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define EXIT_IF(cnd) {if((cnd)) perror(NULL); exit(EXIT_FAILURE);}

void process_char(FILE* output, char ch)
{
  switch(ch)
  {
    case '\r':
      fputs("\\par", output);
      fputc('\r', output);
      break;
    case '\n':
      fputc(ch, output);
      break;
    case '\t':
      fputs("\\tab ", output);
      break;
    case '\\':
      fputs("\\\\", output);
      break;
    case '{':
      fputc('\\', output);
      fputc(ch, output);
      break;
    case '}':
      fputc('\\', output);
      fputc(ch, output);
      break;
    default:
      if ((ch >= 0x20 && (ch <= 0x7F)))
      {
        fputc(ch, output);
      }
      else
      {
        fputs("\\'", output);
        fprintf(output, "%02X", ch);
      }
  }
}

void process_files(FILE* input, FILE* output)
{
  int ch;
  while ((ch = fgetc(input)) != EOF)
  {
    process_char(output, ch);
  }
}

void print_header(FILE* fout, char *font_name, char *font_size)
{
  fputs("{\\rtf1\\ansi\\deff0\\nouicompat{\\fonttbl{\\f0\\fnil\\fcharset0 ", fout);
  fputs(font_name, fout);
  fputs(";}}\r\n", fout);
  fputs("{\\*\\generator DM TXT2RTF 1.0}\\viewkind4\\uc1 \r\n", fout);
  fputs("\\pard\\", fout);
  fputs("fs", fout);
  fputs(font_size, fout);
  fputs("\\lang2057 ", fout);
}

void print_footer(FILE* fout)
{
  fputs("\\par\r\n}\r\n", fout);
  fputc(0, fout);
}

int main(int argc, char* argv[])
{
  FILE *fin = NULL;
  FILE *fout = NULL;
  char sFont[30] = "Consolas";
  char sFontsize[10] = "11";

  if (argc < 3)
  {
    printf("Usage: <InFile> <OutFile> [FontName] [FontSize]\n");
    return 0;
  }

  fin = fopen(argv[1], "rb");
  EXIT_IF(fin == NULL);

  fout = fopen(argv[2], "wb");
  EXIT_IF(fout==NULL);

  if (argc == 4)
    strncpy(sFont, argv[3], sizeof(sFont)-1);

  if (argc == 5)
    strncpy(sFontsize, argv[4], sizeof(sFontsize)-1);

  print_header(fout, sFont, sFontsize);
  process_files(fin, fout);
  print_footer(fout);

  fclose(fin);
  fclose(fout);
  system(argv[2]);
  return 1;
}


No comments:

Post a Comment