예제 #1
0
파일: shapefile.cpp 프로젝트: tbian/aov
//-----------------------------------------------------------------------------
void ReadShapeFile (
    vec_Mat &Mats,           // out
    vec_string &Tags,        // out: strings preceding each mat i.e. tags
    char sImageDirs[],       // out: string following "Directories" in shape file
                             //      can be NULL
    const char sTagRegex[],  // in: only read matrices whose tag
                             //     matches this regular expression
    unsigned Mask0,          // in:
    unsigned Mask1,          // in: only read matrices where
                             //     (Attr & Mask0) == Mask1
                             //     (Attr is hex part of tag string)
    const char sShapeFile[]) // in:
{
clock_t StartTime = clock();
PrintMatchParams(sTagRegex, Mask0, Mask1);  // print informational msg
if (VERBOSE_ASM_SEARCH)
    lprintf("Reading %s ", sShapeFile);
FILE *pShapeFile = Fopen(sShapeFile, "r");  // will issue Err if can't open
CheckShapeFileMagicNumber(pShapeFile, sShapeFile);
char sImageDirsDummy[SLEN];
if (!sImageDirs)
    sImageDirs = sImageDirsDummy;
GetImageDirsFromShapeFile(sImageDirs, sShapeFile, pShapeFile);
ReadMatVec(Mats, &Tags, sShapeFile, pShapeFile, sTagRegex, Mask0, Mask1);
fclose(pShapeFile);
lprintf("%d mat%s [%.1f secs]\n",
        Mats.size(), (Mats.size()==1? "": "s"), double(clock() - StartTime) / CLOCKS_PER_SEC);
}
예제 #2
0
StasmMat FindMatInFile (const char sFile[],  // in
                   char *psImageDirs[], // out: directories in shape file if any, optional
                   const char sRegex[], // in: can be null
                   unsigned Mask0,      // in
                   unsigned Mask1)      // in
{
static char       sFile1[SLEN]; // the mat file name
static char       sImageDirs[SLEN];
static vec_Mat    MatV;         // the matrices read in from the file
static vec_string Tags;         // the tags (i.e. string before each mat in the file)
static vec_int    TagInts;      // hex number at start of each of above tags

// iMat is static so we start where we finished last time, which means that
// searches are fast if this function is invoked for matrices in order
// It also means with multiple matches to sRegex, succesive matching shapes
// are returned each time this function is called.

static unsigned iMat;

if (strcmp(sFile, sFile1))  // first time (for this file)?
    {
    // initialize the static variables

    strcpy(sFile1, sFile);
    iMat = 0;
    // read all shapes, we will filter the one we want later
    ReadShapeFile(MatV, Tags, sImageDirs, NULL, 0, 0, sFile);
    TagInts.resize(MatV.size());
    for (size_t i = 0; i < MatV.size(); i++)
        {
        char const *s = Tags[i].c_str();
        unsigned n;
        if (1 != sscanf(s, "%x", &n))
            Err("can't convert tag %s in %s to a hex number", s, sFile);
        TagInts[i] = n;
        }
    }
// regcomp flags: egrep style expression (supports |),
// ignore case, use simple failure reporting in regexec

// regex_t CompiledRegex;
// if (0 != regcomp(&CompiledRegex, sRegex, REG_EXTENDED|REG_ICASE|REG_NOSUB))
    // Err("invalid regular expression %s", sRegex);

size_t i;
for (i = 0; i < MatV.size(); i++)
    {
    if (fMatchAttr(TagInts[iMat], Mask0, Mask1))
	    // && // filter first on attributes
        // fRegexMatch(CompiledRegex, sGetBasenameFromTag(Tags[iMat])))
        {
        break;                  // found
        }
    iMat++;
    if (iMat >= MatV.size())
        iMat = 0;               // wrap
    }
// regfree(&CompiledRegex);

if (psImageDirs)
    *psImageDirs = sImageDirs;

StasmMat m;                          // returned matrix
if (i < MatV.size())            // found?
    m = MatV[iMat];

return m;
}