Skip to content

Instantly share code, notes, and snippets.

@asserchiu
Created March 2, 2011 04:38
Show Gist options
  • Save asserchiu/850492 to your computer and use it in GitHub Desktop.
Save asserchiu/850492 to your computer and use it in GitHub Desktop.
convert threadtime format logcat into csv file
#include <stdio.h>
#include <stdlib.h>
int main(int argv, char *argc[])
{
if (argv!=2)
{
printf("Usage: %s \"threadtime_format_logcat_file\"\n",__FILE__);
exit(-1);
}
FILE *I=fopen(argc[1],"r");
FILE *O=fopen(strcat(argc[1],".csv"),"w");
if (I==NULL)
{
exit(-2);
printf("Input file error.\n");
}
if (O==NULL)
{
exit(-3);
printf("Output file error.\n");
}
printf("File open all okey.\n");
char c;
int col=1;
int tagORmsg=0;//0:tag 1:msg
int nl=1;//0:not empty newline 1:now at empty new line
while ((c=fgetc(I))!=EOF)
{
switch (col)
{
case 6:
case 19:
case 25:
case 31:
case 33:
fprintf(O,",");
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 26:
case 27:
case 28:
case 29:
case 30:
if (c!=' ')
fprintf(O,"%c",c);//print 2 integers but ignore space
break;
default:
if (col<33)//before tag
{
if ((c==0x0D)||(c==0x0A))
{
col--;//fprintf(O,"%c",c);//ignore second, third... new line char
}
else
{
fprintf(O,"%c",c);
}
}
else if (tagORmsg==0)//tag
{
if (c!=':'&&c!=' ')
{
fprintf(O,"%c",c);
}
else if (c==':')//c==':'
{
fprintf(O,",\"");
c=fgetc(I);
tagORmsg=1;
}
}
else//print all msg, replace " with '
{
if ((c==0x0D)||(c==0x0A))
{
if (col!=1)
{
fprintf(O,"\"\n");
col=1;
nl=1;
tagORmsg=0;
}
col--;
}
else if (c!='"')
{
fprintf(O,"%c",c);
}
else
{
fprintf(O,"'",c);
}
}
break;
}
col++;
}
fclose(I);
fclose(O);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment