]> git.donarmstrong.com Git - samtools.git/blob - bam_tview_html.c
1c75005b9f58a883b43e7b86d2d485d12385f61b
[samtools.git] / bam_tview_html.c
1 #include "bam_tview.h"
2
3 typedef struct HtmlTview {
4         tview_t view;
5         int row_count;
6         tixel_t** screen;
7         FILE* out;
8         int attributes;/* color... */
9         } html_tview_t;
10
11 #define FROM_TV(ptr) ((html_tview_t*)ptr)
12
13 static void html_destroy(tview_t* base)
14         {
15         int i;
16         html_tview_t* tv=(html_tview_t*)base;
17         if(tv->screen!=NULL)
18                 {
19                 for(i=0;i< tv->row_count;++i) free(tv->screen[i]);
20                 free(tv->screen);
21                 }
22         base_tv_destroy(base);
23         free(tv);
24         }
25
26 /*
27  void (*my_mvprintw)(struct AbstractTview* ,int,int,const char*,...);
28     void (*my_)(struct AbstractTview*,int,int,int);
29     void (*my_attron)(struct AbstractTview*,int);
30     void (*my_attroff)(struct AbstractTview*,int);
31     void (*my_clear)(struct AbstractTview*);
32     int (*my_colorpair)(struct AbstractTview*,int);
33 */
34
35 static void html_mvprintw(struct AbstractTview* tv,int y ,int x,const char* fmt,...)
36         {
37         int i,nchars=0;
38         unsigned int size=tv->mcol+2;
39         char* str=malloc(size);
40         if(str==0) exit(EXIT_FAILURE);
41         va_list argptr;
42         va_start(argptr, fmt);
43         nchars=vsnprintf(str,size, fmt, argptr);
44         va_end(argptr);
45         
46         for(i=0;i< nchars;++i)
47                 {
48                 tv->my_mvaddch(tv,y,x+i,str[i]);
49                 }
50         free(str);
51         }
52
53 static void html_mvaddch(struct AbstractTview* tv,int y,int x,int ch)
54         {
55         tixel_t* row=NULL;
56         html_tview_t* ptr=FROM_TV(tv);
57         if( x >= tv->mcol ) return; //out of screen
58         while(ptr->row_count<=y)
59                 {
60                 int x;
61                 row=(tixel_t*)calloc(tv->mcol,sizeof(tixel_t));
62                 if(row==0)  exit(EXIT_FAILURE);
63                 for(x=0;x<tv->mcol;++x) {row[x].ch=' ';row[x].attributes=0;}
64                 ptr->screen=(tixel_t**)realloc(ptr->screen,sizeof(tixel_t*)*(ptr->row_count+1));
65                 ptr->screen[ptr->row_count++]=row;
66                 }
67         row=ptr->screen[y];
68         row[x].ch=ch;
69         row[x].attributes=ptr->attributes;
70         }
71         
72 static void html_attron(struct AbstractTview* tv,int flag)
73     {
74     html_tview_t* ptr=FROM_TV(tv);
75     ptr->attributes |= 1 << flag;
76     }
77    
78 static void html_attroff(struct AbstractTview* tv,int flag)
79     {
80     html_tview_t* ptr=FROM_TV(tv);
81     ptr->attributes &= ~(1 << flag);
82     }
83     
84 static void html_clear(struct AbstractTview* tv)
85     {
86     html_tview_t* ptr=FROM_TV(tv);
87     if(ptr->screen!=NULL)
88         {
89         int i;
90         for(i=0;i< ptr->row_count;++i) free(ptr->screen[i]);
91         free(ptr->screen);
92         ptr->screen=NULL;
93         }
94     ptr->row_count=0;
95     ptr->attributes=0;
96     }
97     
98 static int html_colorpair(struct AbstractTview* tv,int flag)
99     {
100     return flag;
101     }
102
103 static int html_drawaln(struct AbstractTview* tv, int tid, int pos)
104     {
105     int y,x;
106     html_tview_t* ptr=FROM_TV(tv);
107     html_clear(tv);
108     base_draw_aln(tv,  tid, pos);
109     fputs("<html><head>",ptr->out);
110     
111     //style
112     fputs("<style type='text/css'>",ptr->out);
113     #define CSS(id,col) fprintf(ptr->out,".tviewc%d {color:%s;}\n.tviewcu%d {color:%s;text-decoration:underline;}\n",id,col,id,col);
114         CSS(0, "black");
115         CSS(1, "blue");
116         CSS(2, "green");
117         CSS(3, "yellow");
118         CSS(4, "black");
119         CSS(5, "green");
120         CSS(6, "cyan");
121         CSS(7, "yellow");
122         CSS(8, "red");
123         CSS(9, "blue");
124     #undef CSS
125     fputs("</style>",ptr->out);
126     
127     fputs("</head><body>",ptr->out);
128     fputs("<pre class='tview'>\n",ptr->out);
129     for(y=0;y< ptr->row_count;++y)
130         {
131         
132         for(x=0;x< tv->mcol;++x)
133                 {
134                 
135                 
136                 if(x== 0 || ptr->screen[y][x].attributes!=ptr->screen[y][x-1].attributes)
137                         {
138                         int css=0;
139                         fputs("<span class='",ptr->out);
140                         while(css<10)
141                                 {
142                                 if((ptr->screen[y][x].attributes & (1 << css))!=0)
143                                         {
144                                         break;
145                                         }
146                                 ++css;
147                                 }
148                         if(css>=10) css=0;
149                         fprintf(ptr->out,"tviewc%d",css);
150                         fputs("'>",ptr->out);
151                         }
152                 
153                 int ch=ptr->screen[y][x].ch;
154                 switch(ch)
155                         {
156                         case '<': fputs("&lt;",ptr->out);break;
157                         case '>': fputs("&gt;",ptr->out);break;
158                         case '&': fputs("&amp;",ptr->out);break;
159                         default: fputc(ch,ptr->out); break;
160                         }
161                 
162                 
163                 if(x+1 == tv->mcol  || ptr->screen[y][x].attributes!=ptr->screen[y][x+1].attributes)
164                         {
165                         fputs("</span>",ptr->out);
166                         }
167                 }
168         if(y+1 < ptr->row_count) fputs("<br/>",ptr->out);
169         }
170     fputs("</pre></body></html>",ptr->out);
171     return 0;
172     }
173
174 static int text_drawaln(struct AbstractTview* tv, int tid, int pos)
175     {
176     int y,x;
177     html_tview_t* ptr=FROM_TV(tv);
178     html_clear(tv);
179     base_draw_aln(tv,  tid, pos);
180     for(y=0;y< ptr->row_count;++y)
181         {
182         for(x=0;x< tv->mcol;++x)
183                 {
184                 int ch=ptr->screen[y][x].ch;
185
186                 fputc(ch,ptr->out);
187                 }
188         fputc('\n',ptr->out);
189         }
190     return 0;
191     }
192
193
194 static int html_loop(tview_t* tv)
195         {
196         //tv->my_drawaln(tv, tv->curr_tid, tv->left_pos);
197         return 0;       
198         }
199
200 static int html_underline(tview_t* tv)
201         {
202         return 11;      
203         }
204
205 /*
206 static void init_pair(html_tview_t *tv,int id_ge_1, const char* pen, const char* paper)
207         {
208         
209         }
210 */
211
212 tview_t* html_tv_init(const char *fn, const char *fn_fa, const char *samples)
213         {
214         char* colstr=getenv("COLUMNS");
215         html_tview_t *tv = (html_tview_t*)calloc(1, sizeof(html_tview_t));
216         tview_t* base=(tview_t*)tv;
217         if(tv==0)
218                 {
219                 fprintf(stderr,"Calloc failed\n");
220                 return 0;
221                 }
222         tv->row_count=0;
223         tv->screen=NULL;
224         tv->out=stdout;
225         tv->attributes=0;
226         base_tv_init(base,fn,fn_fa,samples);
227         /* initialize callbacks */
228 #define SET_CALLBACK(fun) base->my_##fun=html_##fun;
229         SET_CALLBACK(destroy);
230         SET_CALLBACK(mvprintw);
231         SET_CALLBACK(mvaddch);
232         SET_CALLBACK(attron);
233         SET_CALLBACK(attroff);
234         SET_CALLBACK(clear);
235         SET_CALLBACK(colorpair);
236         SET_CALLBACK(drawaln);
237         SET_CALLBACK(loop);
238         SET_CALLBACK(underline);
239 #undef SET_CALLBACK
240
241         
242         if(colstr!=0)
243                 {
244                 base->mcol=atoi(colstr);
245                 if(base->mcol<10) base->mcol=80;
246                 }
247         base->mrow=99999;
248         
249 /*
250         init_pair(tv,1, "blue", "white");
251         init_pair(tv,2, "green", "white");
252         init_pair(tv,3, "yellow", "white");
253         init_pair(tv,4, "white", "white");
254         init_pair(tv,5, "green", "white");
255         init_pair(tv,6, "cyan", "white");
256         init_pair(tv,7, "yellow", "white");
257         init_pair(tv,8, "red", "white");
258         init_pair(tv,9, "blue", "white");
259         */
260         return base;
261         }
262
263
264 tview_t* text_tv_init(const char *fn, const char *fn_fa, const char *samples)
265         {
266         tview_t* tv=html_tv_init(fn,fn_fa,samples);
267         tv->my_drawaln=text_drawaln;
268         return tv;
269         }
270