第六遍遍历源代码,生成函数链表。

思路,未完成。

 

  1 #include <io.h>
  2 #include <time.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 #include <fcntl.h>
  7 #include <sys/stat.h>
  8 #include <unistd.h>
  9 #include <tchar.h>
 10 #include <dirent.h>
 11 #include <wchar.h>
 12 #include <winsock2.h>
 13 #include <Windows.h>
 14 
 15 struct localIty{
 16     struct localIty *next;
 17     char name[64];            /*变量名称*/
 18     char *bintype;            /*类型柜*/
 19     time_t identifier;        /*编号标识符*/
 20     //....
 21 }
 22 
 23 
 24 struct funChain{
 25     struct defSue *next;
 26     char name[64];            /*函数名*/
 27     char pou;                /*参数数量0-255*/
 28     char Retvt;                /*返回类型*/
 29     char pcrc;                /*去特殊符号与注释后 参数表的CRC值*/
 30     char *fun;                /*指向一个结构体字符串*/
 31 }
 32 
 33 struct funChain *fcP,fC;
 34 
 35 int lstat(const char *str, struct stat *sb)
 36 {
 37     int fd, rv;
 38 
 39     if ((fd = open(str, 0)) < 0)
 40         return (-1);
 41     rv = fstat(fd, sb);
 42     (void)close(fd);
 43     return (rv);
 44 }
 45 
 46 //第六遍遍历源代码,生成函数链表。
 47 int funFollect(char *file)
 48 {
 49     int fd;
 50     char *pl;
 51     struct stat fst;
 52     unsigned long int i,n,s,k40,k41,k123,k125,ret;
 53     unsigned char var[64],funname[64],temp[4096],buf[3200000];
 54     
 55     fd = open(file,O_RDONLY|O_BINARY,00700);
 56     if(fd == -1)return printf("open file - %s error...\n",file);
 57     fstat( fd, &fst );
 58     
 59     lseek( fd, 0, SEEK_SET );//再定位文件指针到文件头
 60     
 61     if(fst.st_size == 0)return printf("file size is zeero...\n");
 62     else if(fst.st_size > 3200000)return printf("sory file size too large...\n");
 63     //直接读内存缓冲区
 64     ret=read(fd,&buf,fst.st_size);
 65     if(ret != fst.st_size)return printf("file read size is something the matter...\n");
 66     //开始循环提取一行数据
 67     for(i=0;i<ret;i++)
 68     {
 69         pl=&buf[i];
 70         //pl 就是一行的数据 ,函数定义有一个特点必须有['{']开始['}']结束,就是[返回类型 函数名称 参数表 定义块]
 71         for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];n++,i++)
 72             var[n] = pl[i];
 73         var[n] = 0;
 74         //循环查找类型表分析var是否在表内
 75         if(...)
 76         {
 77             k40=0;k41=0;k123=0;k125=0;
 78             //直接跳过Space tab...
 79             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 80             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=',' && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];n++,i++)
 81             {
 82                 if(pl[i]==40){k40=i+1;n++,i++;break;}
 83                 funname[n] = pl[i];
 84             }
 85             funname[n] = 0;
 86             //直接跳过Space tab...
 87             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 88             //如果是定义的话会有一个';'结束符号
 89             if(!k40)for(n=i;pl[n]!=';' && pl[n];n++)if(pl[n]==40)k40=n+1;
 90             //循环查找综合表,分析funname是否在表内
 91             if(... && k40)
 92             {
 93                 for(n=k40,s=0;pl[n];n++)
 94                 {
 95                     if(pl[n]==40)s++;
 96                     else if(pl[n]==41)s--;
 97                     //注意用户语法格式[fun((char *)abc,){...}]
 98                     if(pl[n]==41 && s==0){k41 = n;break;}
 99                 }
100                 if(k41)
101                 {
102                     for(i=k41+1;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
103                     if(pl[i]==123)
104                     {
105                         k123 = i+1;
106                         for(i++,s=0;pl[i];i++)
107                         {
108                             if(pl[i]==123)s++;
109                             else if(pl[n]==125)s--;
110                             if(pl[i]==125 && s==0){k125 = i;break;}
111                         }
112                         if(k40&k41&k123&k125)//检查参数
113                         {
114                             
115                             
116                         }
117                         else
118                         {
119                             //函数定义有问题
120                         }
121                         //跳出一个函数块
122                         for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
123                         continue;
124                     }
125                     else
126                     {
127                         //判断是否是参数列表,这是另一种风格的写法
128                         
129                         //可能语法有问题
130                     }
131                 }
132                 else
133                 {
134                     //不正确的函数定义
135                 }
136             }
137             else
138             {
139                 //定义全局或其它类型
140                 //提取变量名称,判断是否重复定义
141             }
142         }
143         else
144         {
145             //输出行号并告知var未定义
146         }
147     }
148     close(fd);
149 }
150 
151 
152 void ListFolderContents(const char *root)
153 {
154     DIR *sdir;
155     DWORD attrib;
156     char subdir[512],dupdir[1024];
157     struct dirent *ptr;
158     struct stat stbuf;
159     
160     sdir = opendir(root);
161     
162     while((ptr = readdir(sdir))!=NULL)
163     {
164         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue;
165         //判断是目录还是文件或者链接
166         sprintf(subdir,"%s%s",root,ptr->d_name);
167         lstat(subdir,&stbuf);
168         //printf("[root=%s]\nsubdir: %s            stbuf.st_mode=%x            isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode));
169         //Sleep(3000);//调试
170         unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0);
171         if (len == 0) return printf("error ---001----------\n");
172         else
173         {
174             memset(dupdir,0,1024);
175             len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len);
176             if (len == 0) return printf("error ---002----------\n");
177         }
178         printf("%s\n",subdir);//遍历输出全部的文件
179         //Sleep(1000);
180         attrib = GetFileAttributesW(dupdir);
181         //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib);
182         //遍历子目录
183         //if(S_ISDIR(stbuf.st_mode)) //linux
184         if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
185         {
186             strcat(subdir,"\\");
187             ListFolderContents(subdir);
188         }
189     }
190     closedir(sdir);
191 }
192 
193 
194 int main(int argc, char *argv[])
195 {
196     int len;
197     static char ServicePath[512];
198     
199     memset(fC,0,sizeof(struct funChain));
200     fcP = &fC;
201     
202     if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) )  
203     {
204         return printf("Cannot install service (%d)\n", GetLastError());  
205     }
206     for(len=strlen(ServicePath);len>0;len--)
207         if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;}
208     
209     ListFolderContents(ServicePath);//windows
210     
211 }

 

posted @ 2020-09-17 16:46  一本正经-胡说八道  阅读(198)  评论(0编辑  收藏  举报