Revision 9150f42e libav/utils.c

View differences:

libav/utils.c
548 548
    return 0;
549 549
}
550 550

  
551
/* Return in 'buf' the path with '%d' replaced by number. Also handles
552
   the '%0nd' format where 'n' is the total number of digits and
553
   '%%'. Return 0 if OK, and -1 if format error */
554
int get_frame_filename(char *buf, int buf_size,
555
                       const char *path, int number)
556
{
557
    const char *p;
558
    char *q, buf1[20];
559
    int nd, len, c, percentd_found;
560

  
561
    q = buf;
562
    p = path;
563
    percentd_found = 0;
564
    for(;;) {
565
        c = *p++;
566
        if (c == '\0')
567
            break;
568
        if (c == '%') {
569
            nd = 0;
570
            while (*p >= '0' && *p <= '9') {
571
                nd = nd * 10 + *p++ - '0';
572
            }
573
            c = *p++;
574
            switch(c) {
575
            case '%':
576
                goto addchar;
577
            case 'd':
578
                if (percentd_found)
579
                    goto fail;
580
                percentd_found = 1;
581
                snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
582
                len = strlen(buf1);
583
                if ((q - buf + len) > buf_size - 1)
584
                    goto fail;
585
                memcpy(q, buf1, len);
586
                q += len;
587
                break;
588
            default:
589
                goto fail;
590
            }
591
        } else {
592
        addchar:
593
            if ((q - buf) < buf_size - 1)
594
                *q++ = c;
595
        }
596
    }
597
    if (!percentd_found)
598
        goto fail;
599
    *q = '\0';
600
    return 0;
601
 fail:
602
    *q = '\0';
603
    return -1;
604
}
605

  
606

  

Also available in: Unified diff