Changeset 875 for libcaca/trunk/cucul/import.c
- Timestamp:
- Apr 25, 2006, 4:12:31 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/import.c
r871 r875 53 53 54 54 if(size==0 || data==NULL) 55 55 return NULL; 56 56 57 57 if(!strcasecmp("caca", format)) … … 62 62 return import_ansi(data, size); 63 63 64 /* Autodetection */ 64 65 if(!strcasecmp("", format)) 65 66 { 66 /* Autodetection */ 67 if(size>=4) /* if 4 first letters are CACA */ 68 { 69 if(buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') 70 return import_caca(data, size); 71 } 72 if(size>=2) /* If 2 first characters are ESC[ (not reliable at all) */ 73 { 74 if((buf[0] == 0x1b) && (buf[1] == '[')) 75 return import_ansi(data, size); 76 } 77 /* Otherwise, import it as text */ 78 return import_caca(data, size); 67 /* if 4 first letters are CACA */ 68 if(size >= 4 && 69 buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A') 70 return import_caca(data, size); 71 72 /* If 2 first characters are ESC[ (not reliable at all) */ 73 if(size >= 2 && buf[0] == 0x1b && buf[1] == '[') 74 return import_ansi(data, size); 75 76 /* Otherwise, import it as text */ 77 return import_text(data, size); 79 78 } 80 79 return NULL; … … 97 96 "text", "plain text", 98 97 "caca", "native libcaca format", 99 98 "ansi", "ANSI coloured text", 100 99 NULL, NULL 101 100 }; … … 206 205 cucul_canvas_t *cv; 207 206 unsigned char const *buffer = (unsigned char const*)data; 208 unsigned int i, sent_size =0;207 unsigned int i, sent_size = 0; 209 208 unsigned char c; 210 unsigned int count =0;209 unsigned int count = 0; 211 210 unsigned int tuple[1024]; /* Should be enough. Will it be ? */ 212 int x =0, y=0, max_x = 80, max_y = 25;213 int save_x =0, save_y=0;214 unsigned int j, add =0;211 int x = 0, y = 0, max_x = 80, max_y = 25; 212 int save_x = 0, save_y = 0; 213 unsigned int j, add = 0; 215 214 int fg, bg, old_fg, old_bg; 216 215 … … 220 219 cv = cucul_create_canvas(max_x, max_y); 221 220 222 for(i = 0 ;i < size; i++) 223 { 224 if((buffer[i] == 0x1b) && (buffer[i+1] == '[')) /* ESC code */ 221 for(i = 0; i < size; i++) 222 { 223 if((buffer[i] == 0x1b) && (buffer[i + 1] == '[')) /* ESC code */ 224 { 225 i++; // ESC 226 i++; // [ 227 sent_size = size - i; 228 c = get_ansi_command(&buffer[i], sent_size); 229 add = parse_tuple(tuple, &buffer[i], sent_size); 230 count = 0; 231 232 while(tuple[count] != 0x1337) 233 count++; /* Gruik */ 234 235 switch(c) 236 { 237 case 'f': 238 case 'H': 239 if(tuple[0] != 0x1337) 225 240 { 226 i++; // ESC 227 i++; // [ 228 sent_size = size-i; 229 c = get_ansi_command(&buffer[i], sent_size); 230 add = parse_tuple(tuple, &buffer[i], sent_size); 231 count = 0; 232 233 while(tuple[count] != 0x1337) count++; /* Gruik */ 234 235 236 switch(c) 237 { 238 case 'f': 239 case 'H': 240 if(tuple[0] != 0x1337) 241 { 242 x=tuple[0]; 243 if(tuple[1] != 0x1337) 244 y=tuple[1]; 245 } 246 else 247 { 248 x = 0; 249 y = 0; 250 } 251 updateCanvasSize(cv, x, y, &max_x, &max_y); 252 break; 253 case 'A': 254 if(tuple[0] == 0x1337) 255 y-=1; 256 else 257 y-=tuple[0]; 258 if(y<0) y=0; 259 updateCanvasSize(cv, x, y, &max_x, &max_y); 260 break; 261 case 'B': 262 if(tuple[0] == 0x1337) 263 y++; 264 else 265 y+=tuple[0]; 266 updateCanvasSize(cv, x, y, &max_x, &max_y); 267 break; 268 case 'C': 269 if(tuple[0] == 0x1337) 270 x++; 271 else 272 x+=tuple[0]; 273 updateCanvasSize(cv, x, y, &max_x, &max_y); 274 break; 275 case 'D': 276 if(tuple[0] == 0x1337) 277 x--; 278 else 279 x-=tuple[0]; 280 if(x<0) x=0; 281 updateCanvasSize(cv, x, y, &max_x, &max_y); 282 break; 283 case 's': 284 save_x = x; 285 save_y = y; 286 break; 287 case 'u': 288 x = save_x; 289 y = save_y; 290 updateCanvasSize(cv, x, y, &max_x, &max_y); 291 break; 292 case 'J': 293 if(tuple[0] == 2) 294 { 295 x = 0; 296 y = 0; 297 updateCanvasSize(cv, x, y, &max_x, &max_y); 298 } 299 break; 300 case 'K': 301 // CLEAR END OF LINE 302 break; 303 case 'm': 304 for(j=0; j < count; j++) 305 manage_modifiers(tuple[j], &fg, &bg, &old_fg, &old_bg); 306 cucul_set_color(cv, fg, bg); 307 break; 308 default: 309 /*printf("Unknow command %c (%c)\n", c, IS_ALPHA(c)?c:'.');*/ 310 break; 311 } 312 313 314 } else { 315 if(buffer[i] == '\n') 316 { 317 x = 0; 318 y++; 319 updateCanvasSize(cv, x, y, &max_x, &max_y); 320 } 321 else if(buffer[i] == '\r') 322 { 323 // DOS sucks. 324 } 325 else 326 { 327 if((buffer[i]>=0x20) || (buffer[i]<=0x7E)) 328 { 329 _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i])); 330 331 // cucul_putchar(cv, x, y, buffer[i]); 332 } 333 else 334 cucul_putchar(cv, x, y, '?'); 335 x++; 336 updateCanvasSize(cv, x, y, &max_x, &max_y); 337 } 241 x = tuple[0]; 242 if(tuple[1] != 0x1337) 243 y = tuple[1]; 338 244 } 339 340 341 342 i+=add; // add is tuple char count, then +[ +command 343 add = 0; 344 345 } 245 else 246 { 247 x = 0; 248 y = 0; 249 } 250 updateCanvasSize(cv, x, y, &max_x, &max_y); 251 break; 252 case 'A': 253 if(tuple[0] == 0x1337) 254 y -= 1; 255 else 256 y -= tuple[0]; 257 if(y < 0) y = 0; 258 updateCanvasSize(cv, x, y, &max_x, &max_y); 259 break; 260 case 'B': 261 if(tuple[0] == 0x1337) 262 y++; 263 else 264 y += tuple[0]; 265 updateCanvasSize(cv, x, y, &max_x, &max_y); 266 break; 267 case 'C': 268 if(tuple[0] == 0x1337) 269 x++; 270 else 271 x += tuple[0]; 272 updateCanvasSize(cv, x, y, &max_x, &max_y); 273 break; 274 case 'D': 275 if(tuple[0] == 0x1337) 276 x--; 277 else 278 x -= tuple[0]; 279 if(x < 0) x = 0; 280 updateCanvasSize(cv, x, y, &max_x, &max_y); 281 break; 282 case 's': 283 save_x = x; 284 save_y = y; 285 break; 286 case 'u': 287 x = save_x; 288 y = save_y; 289 updateCanvasSize(cv, x, y, &max_x, &max_y); 290 break; 291 case 'J': 292 if(tuple[0] == 2) 293 { 294 x = 0; 295 y = 0; 296 updateCanvasSize(cv, x, y, &max_x, &max_y); 297 } 298 break; 299 case 'K': 300 // CLEAR END OF LINE 301 break; 302 case 'm': 303 for(j = 0; j < count; j++) 304 manage_modifiers(tuple[j], &fg, &bg, &old_fg, &old_bg); 305 cucul_set_color(cv, fg, bg); 306 break; 307 default: 308 /*printf("Unknow command %c (%c)\n", c, IS_ALPHA(c)?c:'.');*/ 309 break; 310 } 311 } 312 else 313 { 314 if(buffer[i] == '\n') 315 { 316 x = 0; 317 y++; 318 updateCanvasSize(cv, x, y, &max_x, &max_y); 319 } 320 else if(buffer[i] == '\r') 321 { 322 // DOS sucks. 323 } 324 else 325 { 326 if((buffer[i] >= 0x20) || (buffer[i] <= 0x7E)) 327 { 328 _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i])); 329 330 // cucul_putchar(cv, x, y, buffer[i]); 331 } 332 else 333 cucul_putchar(cv, x, y, '?'); 334 x++; 335 updateCanvasSize(cv, x, y, &max_x, &max_y); 336 } 337 } 338 339 i += add; // add is tuple char count, then +[ +command 340 add = 0; 341 } 342 346 343 return cv; 347 344 } … … 351 348 unsigned char get_ansi_command(unsigned char const *buffer, int size) 352 349 { 353 int i = 0;354 355 for(i =0; i < size; i++)350 int i; 351 352 for(i = 0; i < size; i++) 356 353 if(IS_ALPHA(buffer[i])) 357 354 return buffer[i]; … … 367 364 unsigned char nbr[1024]; 368 365 369 370 366 ret[0] = 0x1337; 371 367 372 for(i=0; i < size; i++) 373 { 374 if(IS_ALPHA(buffer[i])) 375 { 376 if(j!=0) { 377 ret[t] = atoi((char*)nbr); 378 t++; 379 } 380 ret[t] = 0x1337; 381 j=0; 382 return i; 383 } 384 if(buffer[i] != ';') 385 { 386 nbr[j] = buffer[i]; 387 nbr[j+1] = 0; 388 j++; 389 } else 390 { 391 ret[t] = atoi((char*)nbr); 392 t++; 393 ret[t] = 0x1337; 394 j=0; 395 } 396 } 368 for(i = 0; i < size; i++) 369 { 370 if(IS_ALPHA(buffer[i])) 371 { 372 if(j != 0) 373 { 374 ret[t] = atoi((char*)nbr); 375 t++; 376 } 377 ret[t] = 0x1337; 378 j = 0; 379 return i; 380 } 381 382 if(buffer[i] != ';') 383 { 384 nbr[j] = buffer[i]; 385 nbr[j + 1] = 0; 386 j++; 387 } 388 else 389 { 390 ret[t] = atoi((char*)nbr); 391 t++; 392 ret[t] = 0x1337; 393 j = 0; 394 } 395 } 397 396 return size; 398 397 } … … 402 401 void manage_modifiers(char c, int *fg, int *bg, int *old_fg, int *old_bg) 403 402 { 404 switch(c) { 403 switch(c) 404 { 405 405 case 0: 406 *fg = CUCUL_COLOR_ LIGHTGRAY;407 *bg = CUCUL_COLOR_ BLACK;406 *fg = CUCUL_COLOR_DEFAULT; 407 *bg = CUCUL_COLOR_DEFAULT; 408 408 break; 409 409 case 1: // BOLD 410 411 410 break; 412 411 case 4: // Underline 413 414 412 break; 415 413 case 5: // blink 416 417 414 break; 418 415 case 7: // reverse 419 *fg = 15 -*fg;420 *bg = 15 -*bg;416 *fg = 15 - *fg; 417 *bg = 15 - *bg; 421 418 break; 422 419 case 8: // invisible … … 430 427 *bg = *old_bg; 431 428 break; 432 case 30: 433 *fg = CUCUL_COLOR_BLACK; 434 break; 435 case 31: 436 *fg = CUCUL_COLOR_RED; 437 break; 438 case 32: 439 *fg = CUCUL_COLOR_GREEN; 440 break; 441 case 33: 442 *fg = CUCUL_COLOR_BROWN; 443 break; 444 case 34: 445 *fg = CUCUL_COLOR_BLUE; 446 break; 447 case 35: 448 *fg = CUCUL_COLOR_MAGENTA; 449 break; 450 case 36: 451 *fg = CUCUL_COLOR_CYAN; 452 break; 453 case 37: 454 *fg = CUCUL_COLOR_WHITE; 455 break; 456 case 39: 457 *fg = CUCUL_COLOR_LIGHTGRAY; 458 break; 459 case 40: 460 *bg = CUCUL_COLOR_BLACK; 461 break; 462 case 41: 463 *bg = CUCUL_COLOR_RED; 464 break; 465 case 42: 466 *bg = CUCUL_COLOR_GREEN; 467 break; 468 case 43: 469 *bg = CUCUL_COLOR_BROWN; 470 break; 471 case 44: 472 *bg = CUCUL_COLOR_BLUE; 473 break; 474 case 45: 475 *bg = CUCUL_COLOR_MAGENTA; 476 break; 477 case 46: 478 *bg = CUCUL_COLOR_CYAN; 479 break; 480 case 47: 481 *bg = CUCUL_COLOR_WHITE; 482 break; 483 case 49: 484 *bg = CUCUL_COLOR_BLACK; 485 break; 486 487 case 90: 488 *fg = CUCUL_COLOR_DARKGRAY; 489 break; 490 case 91: 491 *fg = CUCUL_COLOR_LIGHTRED; 492 break; 493 case 92: 494 *fg = CUCUL_COLOR_LIGHTGREEN; 495 break; 496 case 93: 497 *fg = CUCUL_COLOR_YELLOW; 498 break; 499 case 94: 500 *fg = CUCUL_COLOR_LIGHTBLUE; 501 break; 502 case 95: 503 *fg = CUCUL_COLOR_LIGHTMAGENTA; 504 break; 505 case 96: 506 *fg = CUCUL_COLOR_LIGHTCYAN; 507 break; 508 case 97: 509 *fg = CUCUL_COLOR_WHITE; 510 break; 511 case 100: 512 *bg = CUCUL_COLOR_DARKGRAY; 513 break; 514 case 101: 515 *bg = CUCUL_COLOR_LIGHTRED; 516 break; 517 case 102: 518 *bg = CUCUL_COLOR_LIGHTGREEN; 519 break; 520 case 103: 521 *bg = CUCUL_COLOR_YELLOW; 522 break; 523 case 104: 524 *bg = CUCUL_COLOR_LIGHTBLUE; 525 break; 526 case 105: 527 *bg = CUCUL_COLOR_LIGHTMAGENTA; 528 break; 529 case 106: 530 *bg = CUCUL_COLOR_LIGHTCYAN; 531 break; 532 case 107: 533 *bg = CUCUL_COLOR_WHITE; 534 break; 429 case 30: *fg = CUCUL_COLOR_BLACK; break; 430 case 31: *fg = CUCUL_COLOR_RED; break; 431 case 32: *fg = CUCUL_COLOR_GREEN; break; 432 case 33: *fg = CUCUL_COLOR_BROWN; break; 433 case 34: *fg = CUCUL_COLOR_BLUE; break; 434 case 35: *fg = CUCUL_COLOR_MAGENTA; break; 435 case 36: *fg = CUCUL_COLOR_CYAN; break; 436 case 37: *fg = CUCUL_COLOR_WHITE; break; 437 case 39: *fg = CUCUL_COLOR_LIGHTGRAY; break; 438 case 40: *bg = CUCUL_COLOR_BLACK; break; 439 case 41: *bg = CUCUL_COLOR_RED; break; 440 case 42: *bg = CUCUL_COLOR_GREEN; break; 441 case 43: *bg = CUCUL_COLOR_BROWN; break; 442 case 44: *bg = CUCUL_COLOR_BLUE; break; 443 case 45: *bg = CUCUL_COLOR_MAGENTA; break; 444 case 46: *bg = CUCUL_COLOR_CYAN; break; 445 case 47: *bg = CUCUL_COLOR_WHITE; break; 446 case 49: *bg = CUCUL_COLOR_BLACK; break; 447 448 case 90: *fg = CUCUL_COLOR_DARKGRAY; break; 449 case 91: *fg = CUCUL_COLOR_LIGHTRED; break; 450 case 92: *fg = CUCUL_COLOR_LIGHTGREEN; break; 451 case 93: *fg = CUCUL_COLOR_YELLOW; break; 452 case 94: *fg = CUCUL_COLOR_LIGHTBLUE; break; 453 case 95: *fg = CUCUL_COLOR_LIGHTMAGENTA; break; 454 case 96: *fg = CUCUL_COLOR_LIGHTCYAN; break; 455 case 97: *fg = CUCUL_COLOR_WHITE; break; 456 case 100: *bg = CUCUL_COLOR_DARKGRAY; break; 457 case 101: *bg = CUCUL_COLOR_LIGHTRED; break; 458 case 102: *bg = CUCUL_COLOR_LIGHTGREEN; break; 459 case 103: *bg = CUCUL_COLOR_YELLOW; break; 460 case 104: *bg = CUCUL_COLOR_LIGHTBLUE; break; 461 case 105: *bg = CUCUL_COLOR_LIGHTMAGENTA; break; 462 case 106: *bg = CUCUL_COLOR_LIGHTCYAN; break; 463 case 107: *bg = CUCUL_COLOR_WHITE; break; 464 535 465 default: 536 /* printf("Unknow option to 'm' %d (%c)\n", c, IS_ALPHA(c)?c:'.'); */ 537 break; 538 539 } 540 } 541 466 /* printf("Unknow option to 'm' %d (%c)\n", c, IS_ALPHA(c)?c:'.'); */ 467 break; 468 } 469 } 542 470 543 471 void updateCanvasSize(cucul_canvas_t *cv, int x, int y, int *max_x, int *max_y) 544 472 { 545 if(x>*max_x) *max_x = x; 546 if(y>*max_y) *max_y = y; 473 if(x > *max_x) 474 *max_x = x; 475 476 if(y > *max_y) 477 *max_y = y; 547 478 548 479 cucul_set_canvas_size(cv, *max_x, *max_y); 549 480 } 481
Note: See TracChangeset
for help on using the changeset viewer.