登录站点

用户名

密码

uclinux-2008R1-RC8到VDSP5的移植(2):代码注释

已有 426 次阅读  2009-09-23 14:34   标签注释  移植  代码 
因为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_;
     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_)
     {
         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语句有换行,那么就很可能不会得到正确的结果,需要在移植时加以注意。

没有了 没有了

分享 举报