示例#1
0
// ==================================================================================================
bool c4s::generate_next_base(path &target, const char *wild)
/*! Searches the directory (or cwd if dir part is empty) with given wild card. Then determines the next
  possible unique filename and stores it to the base part. Please note that only one wild card '*' is
  accepted.
  \param wild A search string.
  \retval bool True on succes, false on error or too many wild cards.
*/
{
    const char *pos;
    char next[128], *tail, *ptr, *tail_copy;

    if(!strchr(wild,'*'))
        return false;
    // Search files based on wild
    path_list bases(target,wild);
    if(bases.size()==0) {
        string base = target.get_base();
        if(base.empty())
            target.set_base("fil_001");
        base=wild;
        size_t ap = base.find('*');
        if(ap==string::npos)
            return false;
        base.replace(ap,1,"001");
        target.set_base(base);
        return true;
    }
    // Get the last one
    bases.sort(path_list::ST_PARTIAL);
    path last = bases.back();
    strcpy(next, last.get_base().c_str());
    ptr = next;
    // Find the 'wild' part
    for(pos=wild; *pos && *pos!='*'; pos++)
        ptr++;
    tail = strstr(ptr,pos+1);
    if(!tail)
        tail = next + strlen(next) ;
    tail_copy = tail;
    tail--;
    // Start calculating from the back.
    while(tail>=ptr) {
        (*tail)++;
        if(*tail==0x3A) {
            *tail=0x30;
            tail--;
        }
        else if(*tail==0x5B) {
            *tail=0x41;
            tail--;
        }
        else if(*tail==0x7B) {
            *tail=0x61;
            tail--;
        }
        else
            break;
    }
    if(tail<ptr) {
        memcpy(tail_copy+1, tail_copy, tail_copy-next-1);
        *tail_copy = 0x30;
    }
    target.set_base(next);
    return true;
}