⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 name.c

📁 从大量的wince源代码中剥离出的fat文件系统源代码.移植性非常高. 里面带有source i.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
                            break;
                        }
                    }

                    pdi->di_achOEM[i] = '~';
                    itopch(v, &pdi->di_achOEM[i+1]);
                    break;
                }
            }
        }
    }
}




/*  OEMToUniName
 *
 *  ENTRY
 *      pwsName - UNICODE buffer
 *      pchOEM - 11-byte OEM name
 *
 *  EXIT
 *      Number of UNICODE chars copied
 */

int OEMToUniName(PWSTR pws, PCHAR pchOEM)
{
    BYTE    *p;
    int     cBytes;
    int     cConverted;
    int     cCopied = 0;

    // Count the characters in the name part.

    p = pchOEM;
    cBytes = 0;

    // Look until 8 bytes or a space

    while ( ( cBytes < 8 ) && ( *p != ' ' ) ) {
        if ( IsDBCSLeadByte(*p) ) {
            cBytes += 2;
            p += 2;
        }
        else {
            cBytes += 1;
            p += 1;
        }
    }

    // Paranoia.  We shouldn't ever see a DBCS lead byte as the 8th byte.

    if ( cBytes > 8 ) {
        ERRORMSG(1, (TEXT("fatfs/name.c: Saw DBCS lead byte in 8th byte.\r\n")));
    }

    if ( cBytes ) {
        cConverted = MultiByteToWideChar(
                                        CP_OEMCP,
                                        MB_PRECOMPOSED,
                                        pchOEM,
                                        cBytes,
                                        pws,
                                        8);
        pws += cConverted;
        cCopied += cConverted;
    }

    // Count the characters in the extension part.
    p = pchOEM+8;
    cBytes = 0;

    // Look until 3 bytes or a space

    while ( ( cBytes < 3 ) && ( *p != ' ' ) ) {
        if ( IsDBCSLeadByte(*p) ) {
            cBytes += 2;
            p += 2;
        }
        else {
            cBytes += 1;
            p += 1;
        }
    }

    // Paranoia.  We shouldn't ever see a DBCS lead byte as the 3rd byte.

    if ( cBytes > 3 ) {
        ERRORMSG(1, (TEXT("fatfs/name.c: Saw DBCS lead byte in 3rd byte.\r\n")));
    }

    if ( cBytes ) {
        *pws++ = TEXTW('.');
        cCopied++;
        cConverted = MultiByteToWideChar(
                                        CP_OEMCP,
                                        MB_PRECOMPOSED,
                                        pchOEM+8,
                                        cBytes,
                                        pws,
                                        3);
        pws += cConverted;
        cCopied += cConverted;
    }

    *pws = 0;
    return cCopied;
}





BOOL UniToOEMName(PCHAR pchOEM, PCWSTR pwsName, int cwName)
{
    WCHAR oc, wc;
    PCHAR pch;
    PCWCH pwcLastDot;
    int cbElement, cDots = 0;
    BOOL fLeadingDots, fReturn = 1, fUncertain = FALSE;

    pwcLastDot = wcsrchr(pwsName, '.');

  restart:
    pch = pchOEM;
    cbElement = 8;
    fLeadingDots = FALSE;
    memset(pchOEM, ' ', OEMNAMESIZE);

    for (; cwName ; cwName--) {

        oc = *pwsName++;

        // Certain characters that are not allowed in ANY names

        if ((oc < 32)   || (oc == '\\') || (oc == '/') ||
            (oc == '>') || (oc == '<')  || (oc == ':') ||
            (oc == '"') || (oc == '|')) {
            fReturn = -2;
            break;
        }

        // The rest of this does not apply if we've seen wildcards before

        if (fReturn < 0)
            continue;

        // Check for wildcards

        if (oc == '?' || oc == '*') {
            fReturn = -1;
            continue;
        }

        // Certain other characters are not allowed in 8.3 names;
        // NOTE that SPACE is normally allowed, but since Win95 prefers
        // to avoid it in auto-generated 8.3 names, we avoid it as well.

        if (oc == ' ') {
            fReturn = 0;
            continue;                   // avoid spaces in 8.3 names altogether
        }

        if (oc == '+' || oc == ',' || oc == ';' || oc == '=' || oc == '[' || oc == ']') {
            fReturn = 0;
            wc = TEXTW('_');            // map these to underscores in the 8.3 name
        }
        else
            wc = towupper(oc);          // could be an alpha char, so upper-case it

        // Lower-case requires UNICODE for creation, but as long as the name
        // is otherwise 8.3-conformant, it's perfectly good for 8.3 searches;
        // hence, the special return code.

        if (fReturn == 1 && wc != oc)
            fReturn = 2;

        // Perform "dot" validation and transitioning from the 8-part to the 3-part.

        if (wc == '.') {

            // Up to two leading dots are allowed

            if (++cDots <= 2 && cDots == 9-cbElement) {
                fLeadingDots = TRUE;
                goto copy;
            }

            // More than one dot anywhere else is not

            if (cDots > 1)
                fReturn = 0;

            // If this is the last dot, start storing at the extension

            if (pwsName > pwcLastDot) {
                pch = pchOEM + 8;       // point to extension
                cbElement = 3;
            }
            continue;
        }

        // We have a non-dot character, and we have already copied some
        // leading dots to the OEM buffer.  Since leading dots are allowed
        // in "." and ".." entries only, we must start the transfer over,
        // beginning with the current non-dot character.

        if (fLeadingDots) {
            fReturn = 0;
            pwsName--;
            goto restart;
        }

        // We used to disallow chars > 0x7f in order to force LFN generation, since
        // the LFN would be in UNICODE and hence more portable;  however, that effectively
        // disabled shortname generation for DBCS (and consequently also made our shortname 
        // generation inconsistent with Win95, resulting in spurious SCANDSKW errors), so
        // that check is now commented out. -JTP

        if (/* wc > 0x7f || */ cbElement == 0) {
            fReturn = 0;
            continue;
        }

      copy:
        {
            int cBytes;
            BOOL fLossy;
            BYTE OEMCharBuf[2];
            CONST BYTE DefCharBuf[2] = {OEM_REPL_CHAR, 0};

            // Need to convert from Unicode to multibyte and then check for enough space

            cBytes = WideCharToMultiByte(
                                        CP_OEMCP,
                                        0,      //REVIEW: do something special for unmapped/composite?
                                        &wc,
                                        1,
                                        &OEMCharBuf[0],
                                        2,
                                        &DefCharBuf[0],
                                        &fLossy);

            if ( cBytes && ( cBytes <= cbElement ) ) {
                int i;
                cbElement -= cBytes;
                for ( i = 0; i < cBytes; i++ )
                    *pch++ = OEMCharBuf[i];
                if (fLossy || cBytes > 1)
                    fUncertain = TRUE;
            }
            else {
                cbElement = 0;
                fReturn = 0;
            }
        }
    }

    if (fReturn >= 1 && fUncertain)
        fReturn = 3;

    return fReturn;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -