歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> uclinux-2008R1-RC8(bf561)到VDSP5的移植(2):代碼注釋

uclinux-2008R1-RC8(bf561)到VDSP5的移植(2):代碼注釋

日期:2017/3/3 16:43:51   编辑:關於Linux

因為uclinux內核是個龐然大物,為避免一開始就遭受打擊,所以就決定先將所有的代碼注釋掉。但是與此同時要保留各個文件之間的依賴關系,因此必須保留#include這樣的語句。再考慮到uclinux是通過宏定義來控制各種功能實現的,且宏定義幾乎不會對移植造成任何困擾,所以也保留了#if #define這樣的語句。

以下就是自己寫的一小段代碼,用於實現上述功能,在VS2005下可以使用。

// hprocess.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <windows.h>
#include <fstream>
#pragma warning(disable:4996)
using namespace std;
void ChangeFile(char* pFile)
{
char line[10000];
char* p, c;
printf("%s processing ... ", pFile);
ifstream f(pFile);
ofstream o("tmp.h");
c = pFile[strlen(pFile)-1];
if(c == 'c' || c == 'C')
o << "#include <config.h>" << endl;
bool bIsDefine = false;
do
{
f.getline(line, 10000);
if(bIsDefine)
{
if(line[strlen(line)-1] != '//')
bIsDefine = false;
}
else
{
if(strstr(line, "#define") && line[strlen(line)-1] == '//')
bIsDefine = true;
else
{
p = line;
while(*p == ' ') p++;
if(*p != '#')
o << "//";
}
}
o << line << endl;
}while(!f.eof());
f.close();
o.close();
CopyFile("tmp.h", pFile, FALSE);
printf("done!/n");
}
int ProcessFile(char* pDir)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH]; // directory specification
char NextPath[MAX_PATH]; // directory specification
DWORD dwError;
printf ("Target directory is %s./n", pDir);
strcpy (DirSpec, pDir);
strcat (DirSpec, "*");
hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u/n", GetLastError());
return (-1);
}
else
{
do
{
if(strcmp(FindFileData.cFileName, ".") == 0 || strcmp(FindFileData.cFileName, "..") == 0) continue;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName, "vs2005") == 0) continue;
strcpy(NextPath, pDir);
strcat(NextPath, FindFileData.cFileName);
strcat(NextPath, "//");
ProcessFile(NextPath);
}
else
{
int len = strlen(FindFileData.cFileName);
if(FindFileData.cFileName[len-2] == '.' && (FindFileData.cFileName[len-1] == 'h' || FindFileData.cFileName[len-1] == 'H' || FindFileData.cFileName[len-1] == 'C' || FindFileData.cFileName[len-1] == 'c'))
{
strcpy(NextPath, pDir);
strcat(NextPath, FindFileData.cFileName);
ChangeFile(NextPath);
}
}
}while (FindNextFile(hFind, &FindFileData) != 0);
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u/n", dwError);
return (-1);
}
}
return (0);
}
int _tmain(int argc, _TCHAR* argv[])
{
ProcessFile("D://uc2008//linux-2.6.x//");
printf("all ok");
getchar();
return 0;
}

當然,在使用之前要設置好正確的路徑。

不過這段代碼有個BUG,就是對換行符的處理。因為linux下的文件和windows下的文件換行符是不同的,因此如果#define語句有換行,那麼就很可能不會得到正確的結果,需要在移植時加以注意。

Copyright © Linux教程網 All Rights Reserved