第三次遍历源代码,整理typedef

思路、待完善...,

这个逻辑点比较复杂。

 

  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 defType{
 16     struct defType *next;
 17     char effect;            /*常规、指针、数组、函数指针*/
 18     char otp[64];            /*旧类型名称*/
 19     char *nvar;                /*新变量名称*/
 20 };
 21 
 22 struct sueChain{
 23     struct defSue *next;
 24     char name[64];
 25     char mold;                /*1:struct 2:union 3:enum*/
 26     char *sue;                /*指向一个结构体字符串*/
 27 };
 28 
 29 struct sueChain *scP,sC;
 30 struct defType *dtP,dT;
 31 
 32 int lstat(const char *str, struct stat *sb)
 33 {
 34     int fd, rv;
 35 
 36     if ((fd = open(str, 0)) < 0)
 37         return (-1);
 38     rv = fstat(fd, sb);
 39     (void)close(fd);
 40     return (rv);
 41 }
 42 
 43 //第三次遍历源代码,整理typedef
 44 int typelist(char *file)
 45 {
 46     int fd;
 47     char *pl;
 48     struct stat fst;
 49     unsigned long int i,j,k,n,p,s,ret,mark;
 50     unsigned char temp[1024],buf[3200000];
 51     
 52     fd = open(file,O_RDONLY|O_BINARY,00700);
 53     if(fd == -1)return printf("open file - %s error...\n",file);
 54     fstat( fd, &fst );
 55     
 56     lseek( fd, 0, SEEK_SET );//再定位文件指针到文件头
 57     
 58     if(fst.st_size == 0)return printf("file size is zeero...\n");
 59     else if(fst.st_size > 3200000)return printf("sory file size too large...\n");
 60     //直接读内存缓冲区
 61     ret=read(fd,&buf,fst.st_size);
 62     if(ret != fst.st_size)return printf("file read size is something the matter...\n");
 63     //开始循环提取一行数据
 64     for(i=0;i<ret;i++)
 65     {
 66         pl=&buf[i];
 67         //pl 就是一行的数据 typedef 判断后面5个字母ypedef
 68         for(;pl[i]!='t' && pl[i];i++);
 69         if(pl[i]=='y' && pl[i+1]=='p' && pl[i+2]=='e' && pl[i+3]=='d' &&
 70         pl[i+4]=='e' && pl[i+5]=='f')
 71         {
 72             i+=6;
 73             //直接跳过Space tab...
 74             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 75             while(dtP->next)dtP=dtP->next;
 76             //常规、指针、数组、函数指针
 77             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i] && pl[i]!=';';i++,n++)
 78                 dtP->otp[n] = pl[i];
 79             if(pl[i]==0x20 || pl[i]==0x09)
 80             {
 81                 dtP->otp[n] = 0x20;
 82                 //struct union enum
 83                 //typedef struct abc newabc;
 84                 //typedef struct abc{...} newabc,*newp;
 85                 for(i++;pl[i]==0x20 || pl[i]==0x09;i++)mark = i;//跳过空格和tab
 86                 if(strncasecmp( dtP->otp[n], "struct",6) == 0 || 
 87                 strncasecmp( dtP->otp[n], "union",5) == 0 || 
 88                 strncasecmp( dtP->otp[n], "enum" ,4) == 0)
 89                 {
 90                     for(;pl[i]==0x20 || pl[i]==0x09;i++)mark = i;//mark点 ,跳过空格和tab
 91                     if(mark == (i-1)){n++;dtP->otp[n] = 0x20;}
 92                     for(n++;pl[i]!=0x20 && pl[i]!=0x09 && n<64 && pl[i]!='{' && pl[i]!=';' && pl[i];i++,n++)
 93                         dtP->otp[n] = pl[i];
 94                     dtP->otp[n] = 0;
 95                     for(;pl[i]==0x20 || pl[i]==0x09;i++);
 96                     //如果中间有类型定义 struct abc{...} newabc,*newp;
 97                     if(pl[i]=='{')
 98                     {
 99                         for(i++;pl[i]!='}' && pl[i]!=';' && pl[i];i++);
100                         //把这个结构定义加入到  链表中
101                         ...
102                         for(i++;pl[i]==0x20 || pl[i]==0x09;i++);
103                     }
104                     else
105                     {
106                         
107                         //for(i++;pl[i]==0x20 || pl[i]==0x09;i++);
108                     }
109                     else if(pl[i]==',')//[[typedef struct union BOOL,ZJ   ;]]
110                     {
111                         ...
112                         break;
113                     }
114                 }
115                 //typedef long double REAL;
116                 //typedef int BOOL   ;
117                 //typedef char * PCHAR; || typedef char* PCHAR; || typedef const char* PCHAR;
118                 //typedef unsigned __int64    size_t;
119                 //typedef int INT_ARRAY_100[100];
120                 //typedef int *(*PFun)(int,char*);
121                 else
122                 {
123                     mark = i;
124                     do
125                     {
126                         for(n++;pl[i]!=0x20 && pl[i]!=0x09 && n<64 && pl[i]!=';' && pl[i];n++,i++)dtP->otp[n] = pl[i];
127                         for(;pl[i]==0x20 || pl[i]==0x09;i++)mark = i;//mark点 ,跳过空格和tab
128                         if(pl[i]==';')
129                         {
130                             k = i;//初始化
131                             //在末尾使用了Space tab符号。 [[typedef char BOOL   ;]]
132                             if(mark == (i-1))
133                             {
134                                 while(pl[mark]==0x20 || pl[mark]==0x09)mark--;
135                                 k = mark+1;
136                                 while(mark && pl[mark]!=0x20 && pl[mark]!=0x09)mark--;
137                                 mark++;
138                             }
139                             for(s=0;mark<k;mark++,s++)temp[s] = pl[mark];
140                             dtP->nvar = strdup(temp);//新变量名称
141                             dtP->otp[n] = 0;//旧变量
142                             dtP->effect = 1;//常规
143                             i++;
144                             break;
145                         }
146                         else if(pl[i]==',')//[[typedef char BOOL,ZJ   ;]]
147                         {
148                             ...
149                             break;
150                         }
151                         else if(pl[i]=='*')
152                         {
153                             for(i++;pl[i]=='*' || pl[i]==0x20 || pl[i]==0x09;i++);
154                             if(pl[i]=='(')
155                             {
156                                 //goto ...
157                             }
158                             else
159                             {
160                                 
161                             }
162                         }
163                         else if(pl[i]=='(')
164                         {
165                             for(i++;pl[i]==0x20 || pl[i]==0x09;i++);
166                             提取名称
167                         }
168                         //如果遇到[typedef char * PCHAR;]要整理成[typedef char* PCHAR;]
169                         else if(mark == (i-1)){n++;dtP->otp[n] = 0x20;...}
170                     }while(n<64 && mark);
171                 }
172             }
173             
174             dtP->next = malloc(sizeof(struct defType));
175             memset(dtP->next,0,sizeof(struct defType));
176             
177         }
178         //跳过特殊符号
179         for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
180     }
181     close(fd);
182 }
183 
184 
185 void ListFolderContents(const char *root)
186 {
187     DIR *sdir;
188     DWORD attrib;
189     char subdir[512],dupdir[1024];
190     struct dirent *ptr;
191     struct stat stbuf;
192     
193     sdir = opendir(root);
194     
195     while((ptr = readdir(sdir))!=NULL)
196     {
197         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue;
198         //判断是目录还是文件或者链接
199         sprintf(subdir,"%s%s",root,ptr->d_name);
200         lstat(subdir,&stbuf);
201         //printf("[root=%s]\nsubdir: %s            stbuf.st_mode=%x            isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode));
202         //Sleep(3000);//调试
203         unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0);
204         if (len == 0) return printf("error ---001----------\n");
205         else
206         {
207             memset(dupdir,0,1024);
208             len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len);
209             if (len == 0) return printf("error ---002----------\n");
210         }
211         printf("%s\n",subdir);//遍历输出全部的文件
212         //Sleep(1000);
213         attrib = GetFileAttributesW(dupdir);
214         //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib);
215         //遍历子目录
216         //if(S_ISDIR(stbuf.st_mode)) //linux
217         if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
218         {
219             strcat(subdir,"\\");
220             ListFolderContents(subdir);
221         }
222     }
223     closedir(sdir);
224 }
225 
226 
227 int main(int argc, char *argv[])
228 {
229     int len;
230     static char ServicePath[512];
231 
232     memset(dT,0,sizeof(struct defType));
233     dtP = &dT;
234     
235     if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) )  
236     {
237         return printf("Cannot install service (%d)\n", GetLastError());  
238     }
239     for(len=strlen(ServicePath);len>0;len--)
240         if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;}
241     
242     ListFolderContents(ServicePath);//windows
243     
244 }

 

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