Instantly share code, notes, and snippets.

Embed
What would you like to do?
Program using libvarnam that transliterates string literals
/*
Usage : ./a.out <pythonfile>
The transliterated file will be saved as original filename + ml.py
Remember to place the file in the examples subdir in the libvarnam codebase
wont work for multiple literals in the same line
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "../varnam.h"
#define buffer_size 300
void get_transliteration_output(varray *words,char* newword)
{
int i;
vword *word;
word = varray_get(words,i);
strcpy(newword,word->text);
}
void make_newline(char *line,char *start,char *stop,char *newword,char *newline)
{
int i;
char *ptr;
//Copying everything till " to newline
for(i=0,ptr=line;ptr!=start;ptr++,i++)
{
newline[i]=line[i];
}
//concatenating the malayalam pattern
strcat(newline,newword);
strcat(newline,"\"");
strcat(newline,stop);
//printf("%s",newline);
}
int main(int argc,char *argv[])
{
FILE *source,*target;
char *source_file,*ptr,*error_msg,*start,*stop,*newword,*newline;
varnam *handle;
varray *words;
int rc;
rc=varnam_init_from_lang("ml",&handle,&error_msg);
if(rc!=VARNAM_SUCCESS)
{
printf("Initialization failed : %s",error_msg);
free(error_msg);
return 1;
}
source_file=argv[1];
source=fopen(source_file,"r");
//Finding the . to extract the filename
ptr=strstr(source_file,".");
*ptr='\0';
//source_file does not have the '.py' extension now since we placed a '\0' there
target=fopen(strcat(source_file,"ml.py"),"w");
char line[200];
fprintf(target,"#!usr/bin/python\n");
fprintf(target,"# -*- coding: UTF-8 -*-\n");
while(fgets(line,sizeof(line),source))
{
start=strstr(line,"\"");
if(start)
{}
start+=1;
stop=strstr(start,"\"");
//Assuming that there is a matching ".
*stop='\0';
stop+=1;
rc=varnam_transliterate(handle,start,&words);
newword=(char*)malloc(buffer_size);
newline=(char*)malloc(buffer_size);
get_transliteration_output(words,newword);
make_newline(line,start,stop,newword,newline);
printf("Transliterated line : %s",newline);
fprintf(target,"%s",newline);
free(newword);
free(newline);
}
else
fprintf(target,"%s",line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment