Linux下UTF8与UTF8-BOM文件格式互转

2020-02-19T12:21:51

今天在编译AWTK的时候遇到以下问题:

In file included from awtk-port/fb_test.c:2:
awtk-port/fb_info.h:1: error: stray '\357' in program
awtk-port/fb_info.h:1: error: stray '\273' in program
awtk-port/fb_info.h:1: error: stray '\277' in program
In file included from awtk-port/fb_info.h:33,
                 from awtk-port/fb_test.c:2:
/home/dwx/src/zlgopen/awtk/src/base/lcd.h:1: error: stray '\357' in program
/home/dwx/src/zlgopen/awtk/src/base/lcd.h:1: error: stray '\273' in program
/home/dwx/src/zlgopen/awtk/src/base/lcd.h:1: error: stray '\277' in program
In file included from /home/dwx/src/zlgopen/awtk/src/base/lcd.h:25,
                 from awtk-port/fb_info.h:33,
                 from awtk-port/fb_test.c:2:
/home/dwx/src/zlgopen/awtk/src/tkc/rect.h:1: error: stray '\357' in program
/home/dwx/src/zlgopen/awtk/src/tkc/rect.h:1: error: stray '\273' in program
/home/dwx/src/zlgopen/awtk/src/tkc/rect.h:1: error: stray '\277' in program
In file included from /home/dwx/src/zlgopen/awtk/src/tkc/rect.h:25,
                 from /home/dwx/src/zlgopen/awtk/src/base/lcd.h:25


一查问题原因,才知道是因为文件格式不对所导致的。
因为在windows下使用VS建立的工程文件,默认是使用带BOM的UTF-8编码格式,而在Linux下,使用的是不带BOM的UTF-8编码格式,所以工程文件在windows下和linux下互转时,会遇到一系列的问题。
为了方便解决这个问题,所以做了一个在Linux下使用脚本对带BOM格式的UTF8文件和不带BOM的UTF8文件进行互转,提高工作效率,当然不使用这个脚本,使用Vscode和notepad++也是可以进行脚本互转的,不过需要一个一个的进行互转。

UTF-8带BOM转UTF-8无BOM:
[scode type="red"] 进入Linux终端,输入命令:vim rmbom.sh,然后输入以下内容[/scode]

#!/bin/bash
#将UTF-8带BOM编码的文件转化为UTF-8无BOM格式
if [[ -z "$1" ]];then
    echo '用法:./rmbom.sh [folder | file]'
    echo '将UTF-8编码的文件转化为UTF-8无BOM格式'
    exit 1
fi
 
 
path=$1
find $path -type f -name "*" -print | xargs -i sed -i '1 s/^\xef\xbb\xbf//' {}
echo "Convert finish"

然后保存,退出,执行以下命令:

sudo chmod 777 *
sudo sh rmbom.sh

这样就可以轻松的将UTF8-BOM文件格式转为无BOM的UTF8文件。

接下来就是从无BOM的UTF8文件转为带BOM的UTF文件了。
UTF-8无BOM转UTF-8带BOM:

#!/bin/bash
#将UTF-8无BOM编码的文件转化为UTF-8带BOM格式
 
 
if [[ -z "$1" ]];then
    echo '用法:./addbom.sh [folder | file]'
    echo '将UTF-8无BOM格式编码的文件转化为UTF-8带BOM'
    exit 1
fi
 
 
path=$1
find $path -type f -name "*" -print | xargs -i sed -i '1 s/^/\xef\xbb\xbf&/' {}
echo "Convert finish"

执行的步骤差不多,到这里就结束了,接下来编译看看。

scons

稍等一会,就会提示编译成功了。

 -DWITH_NANOVG_AGGE -DWITH_WIDGET_TYPE_CHECK -DLINUX -DHAS_PTHREAD -DENABLE_CURSOR -DHAS_TSLIB -I/home/dwx/src/tslib/include -I/home/dwx/src/zlgopen/awtk -I/home/dwx/src/zlgopen/awtk/src -I/home/dwx/src/zlgopen/awtk/3rd -I/home/dwx/src/zlgopen/awtk/src/ext_widgets -I/home/dwx/src/zlgopen/awtk/tools -I/home/dwx/src/zlgopen/awtk/3rd/agge -I/home/dwx/src/zlgopen/awtk/3rd/agg/include -I/home/dwx/src/zlgopen/awtk/3rd/nanovg -I/home/dwx/src/zlgopen/awtk/3rd/nanovg/base -I/home/dwx/src/zlgopen/awtk/3rd/libunibreak -I/home/dwx/src/zlgopen/awtk/3rd/gpinyin/include build/var/awtk/3rd/agg/src/agg_vcgen_stroke.cpp
/opt/gcc-4.3-ls232-softfloat/bin/mipsel-linux-ar rc build/lib/libagg.a build/var/awtk/3rd/agg/src/agg_color_rgba.o build/var/awtk/3rd/agg/src/agg_image_filters.o build/var/awtk/3rd/agg/src/agg_sqrt_tables.o build/var/awtk/3rd/agg/src/agg_trans_affine.o build/var/awtk/3rd/agg/src/agg_vcgen_stroke.o
ranlib build/lib/libagg.a
scons: done building targets.

在编译的时候,还提示以下问题:

/opt/gcc-4.3-ls232-softfloat/bin/mipsel-linux-gcc -o build/var/awtk/src/native_window/native_window_raw.o -c -std=gnu99 -Wall -Os -DHAS_STD_MALLOC -DWITH_FS_RES -DHAS_STDIO -DWITH_VGCANVAS -DWITH_UNICODE_BREAK -DLINUX -DSTBTT_STATIC -DSTB_IMAGE_STATIC -DWITH_STB_IMAGE -DWITH_STB_FONT -DWITH_NANOVG_AGGE -DWITH_WIDGET_TYPE_CHECK -DLINUX -DHAS_PTHREAD -DENABLE_CURSOR -DHAS_TSLIB -I/home/dwx/src/tslib/include -I/home/dwx/src/zlgopen/awtk -I/home/dwx/src/zlgopen/awtk/src -I/home/dwx/src/zlgopen/awtk/3rd -I/home/dwx/src/zlgopen/awtk/src/ext_widgets -I/home/dwx/src/zlgopen/awtk/tools -I/home/dwx/src/zlgopen/awtk/3rd/agge -I/home/dwx/src/zlgopen/awtk/3rd/agg/include -I/home/dwx/src/zlgopen/awtk/3rd/nanovg -I/home/dwx/src/zlgopen/awtk/3rd/nanovg/base -I/home/dwx/src/zlgopen/awtk/3rd/libunibreak -I/home/dwx/src/zlgopen/awtk/3rd/gpinyin/include build/var/awtk/src/native_window/native_window_raw.c
build/var/awtk/src/native_window/native_window_raw.c: In function 'native_window_create':
build/var/awtk/src/native_window/native_window_raw.c:115: warning: implicit declaration of function 'widget_set_prop_pointer'
build/var/awtk/src/native_window/native_window_raw.c:115: error: 'WIDGET_PROP_NATIVE_WINDOW' undeclared (first use in this function)
build/var/awtk/src/native_window/native_window_raw.c:115: error: (Each undeclared identifier is reported only once
build/var/awtk/src/native_window/native_window_raw.c:115: error: for each function it appears in.)
scons: *** [build/var/awtk/src/native_window/native_window_raw.o] Error 1

解决办法:
在native_window_raw.h文件里面加上#include "base/widget_consts.h",这句话就可以了。

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »