Example #1
0
/*
=======================================
    Mac Shell 命令行
=======================================
*/
CR_API void_t*
qst_mac_tran (
  __CR_IN__ const ansi_t*   string,
  __CR_OT__ uint_t*         ot_size
    )
{
    leng_t  len;
    ansi_t* dat;

    len = str_lenA(string);
    dat = str_allocA(len + 1);
    if (dat == NULL)
        return (NULL);
    mem_cpy(dat, string, len);
    dat[len + 0] = '\r';
    *ot_size = len + 1;
    return (dat);
}
Example #2
0
/*
---------------------------------------
    文件处理回调
---------------------------------------
*/
static bool_t do_each_file (void_t *param, sSEARCHa *finfo)
{
    wide_t *tmp;
    sCP_PARAM *ctx;
    leng_t idx, len;
    ansi_t *src, *dst;

    /* 统计文件总数 */
    ctx = (sCP_PARAM*)param;
    ctx->tot += 1;

    /* 跳过空文件和只读文件 */
    if ((finfo->size == 0) ||
        (finfo->attr & CR_FILE_ATTR_RDO))
        return (TRUE);

    /* 过滤掉大文件 */
    if (finfo->size > CR_M2B(4))
        return (TRUE);
    if (ctx->del_u8_bom && finfo->size <= 3)
        return (TRUE);

    /* 加载整个文件 */
    src = file_load_as_strA(finfo->name);
    if (src == NULL)
        return (TRUE);

    /* 跳过二进制文件 */
    len = str_lenA(src);
    if (len != finfo->size)
        goto _func_out;

    /* 删除 UTF-8 文件的 BOM 标志 */
    if (ctx->del_u8_bom) {
        if (chr_cmpA(src, BOM_UTF8, 3) != 0)
            goto _func_out;
        if (!is_utf8_file(src))
            goto _func_out;
        if (!file_saveA(finfo->name, src + 3, len - 3))
            goto _func_out;
        ctx->cvt += 1;
        printf("%s\n", finfo->name);
        goto _func_out;
    }

    /* 检查是否是 UTF-8, 跳过不用转换的文件 */
    if (ctx->skip_utf8 && is_utf8_file(src))
        goto _func_out;

    /* 转换到 UTF-16 编码 */
    len = MultiByteToWideChar(ctx->from, MB_ERR_INVALID_CHARS,
                              src, -1, NULL, 0);
    if (len == 0)
        goto _func_out;
    if (ctx->to == CR_UTF16LE || ctx->to == CR_UTF16BE) {
        tmp = str_allocW(len + 1);
        if (tmp == NULL)
            goto _func_out;
        len = MultiByteToWideChar(ctx->from, 0, src, -1, tmp + 1, len);
        mem_cpy(tmp, BOM_UTF16LE, 2);
        if (ctx->to == CR_UTF16BE) {
            for (idx = 0; idx < len; idx++)
                tmp[idx] = xchg_int16u(tmp[idx]);
        }
        len = len * 2 + 1;
        dst = (ansi_t*)tmp;
        mem_free(src);
    }
    else {
        tmp = str_allocW(len);
        if (tmp == NULL)
            goto _func_out;
        MultiByteToWideChar(ctx->from, 0, src, -1, tmp, len);
        mem_free(src);

        /* 转换到目标编码 */
        len = WideCharToMultiByte(ctx->to, 0, tmp, -1, NULL, 0, NULL, NULL);
        if (len == 0) {
            mem_free(tmp);
            return (TRUE);
        }
        dst = str_allocA(len);
        if (dst == NULL) {
            mem_free(tmp);
            return (TRUE);
        }
        len = WideCharToMultiByte(ctx->to, 0, tmp, -1, dst, len, NULL, NULL);
        mem_free(tmp);
    }

    /* 写入整个文件 (去掉后面的0) */
    if (!file_saveA(finfo->name, dst, len - 1)) {
        mem_free(dst);
        return (TRUE);
    }
    ctx->cvt += 1;
    printf("%s\n", finfo->name);
    mem_free(dst);
    return (TRUE);

_func_out:
    mem_free(src);
    return (TRUE);
}