第五遍遍历源代码,分析并定义出全局定义的变量

只是一种思路描述过程。

  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 pter;                /*1.常规名 2.指针 3.数组 ...*/
 19     char *bintype;            /*类型柜*/
 20     time_t identifier;        /*编号标识符*/
 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 localIty *liP,lI;
 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 //第五遍遍历源代码,分析并定义出全局定义的变量,struct abc{...} newabc,*newp;
 47 int bintype(char *file)
 48 {
 49     int fd;
 50     char *pl;
 51     time_t tsn;
 52     struct stat fst;
 53     unsigned long int i,n,s,k40,k41,k123,k125,ret;
 54     unsigned char var[64],funname[64],temp[4096],buf[3200000];
 55     
 56     fd = open(file,O_RDONLY|O_BINARY,00700);
 57     if(fd == -1)return printf("open file - %s error...\n",file);
 58     fstat( fd, &fst );
 59     
 60     lseek( fd, 0, SEEK_SET );//再定位文件指针到文件头
 61     
 62     if(fst.st_size == 0)return printf("file size is zeero...\n");
 63     else if(fst.st_size > 3200000)return printf("sory file size too large...\n");
 64     //直接读内存缓冲区
 65     ret=read(fd,&buf,fst.st_size);
 66     if(ret != fst.st_size)return printf("file read size is something the matter...\n");
 67     //开始循环提取一行数据
 68     for(i=0;i<ret;i++)
 69     {
 70         pl=&buf[i];
 71         //pl 就是一行的数据 ,函数定义有一个特点必须有['{']开始['}']结束,就是[返回类型 函数名称 参数表 定义块]
 72         for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];n++,i++)
 73             var[n] = pl[i];
 74         var[n] = 0;
 75         //循环查找类型表分析var是否在表内
 76         if(...)
 77         {
 78             k40=0;k41=0;k123=0;k125=0;
 79             //直接跳过Space tab...
 80             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 81             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=',' && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];n++,i++)
 82             {
 83                 if(pl[i]==40){k40=i+1;n++,i++;break;}
 84                 funname[n] = pl[i];
 85             }
 86             funname[n] = 0;
 87             //直接跳过Space tab...
 88             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 89             //如果是定义的话会有一个';'结束符号
 90             if(!k40)for(n=i;pl[n]!=';' && pl[n];n++)if(pl[n]==40)k40=n+1;
 91             //循环查找综合表,分析funname是否在表内
 92             if(... && k40)
 93             {
 94                 //...
 95             }
 96             else
 97             {
 98                 tsn = 0;
 99                 //定义全局或其它类型
100                 
101                 
102                 //提取变量名称,判断是否重复定义
103                 do
104                 {
105                     while(liP->next)
106                     {
107                         tsn ++;
108                         if(strcasecmp(liP->name,funname)==0)
109                             break;
110                         liP=liP->next;
111                     }
112                     //已经存在同名变量
113                     if(liP->next)
114                     {
115                         //同名变量所在行提示
116                     }
117                     else
118                     {
119                         while(liP->next)liP=liP->next;
120                         strcpy(liP->name,funname);
121                         liP->bintype = strdup(var);
122                         liP->identifier = tsn;//time((time_t*)0);
123                         liP->pter
124                         liP->next = malloc(sizeof(struct localIty));
125                         memset(liP->next,0,sizeof(struct localIty));
126                         if(pl[i]==';')break;
127                     }
128                     if(pl[i]==',')i++;
129                     for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=',' && pl[i]!=';' && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];n++,i++)
130                     {
131                         if(pl[i]==40){k40=i+1;n++,i++;break;}
132                         funname[n] = pl[i];
133                     }
134                     funname[n] = 0;
135                     for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
136                 }while(pl[i]);
137             }
138         }
139         else
140         {
141             //输出行号并告知var未定义
142         }
143     }
144     close(fd);
145 }
146 
147 
148 void ListFolderContents(const char *root)
149 {
150     DIR *sdir;
151     DWORD attrib;
152     char subdir[512],dupdir[1024];
153     struct dirent *ptr;
154     struct stat stbuf;
155     
156     sdir = opendir(root);
157     
158     while((ptr = readdir(sdir))!=NULL)
159     {
160         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue;
161         //判断是目录还是文件或者链接
162         sprintf(subdir,"%s%s",root,ptr->d_name);
163         lstat(subdir,&stbuf);
164         //printf("[root=%s]\nsubdir: %s            stbuf.st_mode=%x            isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode));
165         //Sleep(3000);//调试
166         unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0);
167         if (len == 0) return printf("error ---001----------\n");
168         else
169         {
170             memset(dupdir,0,1024);
171             len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len);
172             if (len == 0) return printf("error ---002----------\n");
173         }
174         printf("%s\n",subdir);//遍历输出全部的文件
175         //Sleep(1000);
176         attrib = GetFileAttributesW(dupdir);
177         //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib);
178         //遍历子目录
179         //if(S_ISDIR(stbuf.st_mode)) //linux
180         if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
181         {
182             strcat(subdir,"\\");
183             ListFolderContents(subdir);
184         }
185     }
186     closedir(sdir);
187 }
188 
189 
190 int main(int argc, char *argv[])
191 {
192     int len;
193     static char ServicePath[512];
194     
195     memset(lI,0,sizeof(struct localIty));
196     liP = &lI;
197     
198     if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) )  
199     {
200         return printf("Cannot install service (%d)\n", GetLastError());  
201     }
202     for(len=strlen(ServicePath);len>0;len--)
203         if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;}
204     
205     ListFolderContents(ServicePath);//windows
206     
207 }

 

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