Changes:
[repair.git] / Repair / RepairCompiler / structextract / common.c
1 #include<string.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include "common.h"
7
8 char * copystr(const char *buf) {
9   int i;
10   if (buf==NULL)
11     return NULL;
12   for(i=0;;i++)
13     if (buf[i]==0) {
14       char *ptr=(char*)malloc(i+1);
15       memcpy(ptr,buf,i+1);
16       return ptr;
17     }
18 }
19
20 char * escapestr(const char *buf) {
21   int i,j;
22   if (buf==NULL)
23     return NULL;
24   for(i=0;;i++)
25     if (buf[i]==0) {
26       char *ptr=(char*)malloc(i+1);
27       for(j=0;j<=i;j++) {
28         ptr[j]=buf[j];
29         if (ptr[j]=='.')
30           ptr[j]='_';
31       }
32       return ptr;
33     }
34 }
35
36
37 unsigned int hashstring(char *strptr) {
38   unsigned int hashcode=0;
39   int *intptr=(int *) strptr;
40   if(intptr==NULL)
41     return 0;
42   while(1) {
43     int copy1=*intptr;
44     if((copy1&0xFF000000)&&
45        (copy1&0xFF0000)&&
46        (copy1&0xFF00)&&
47        (copy1&0xFF)) {
48       hashcode^=*intptr;
49       intptr++;
50     } else {
51       if (!copy1&0xFF000000)
52         hashcode^=copy1&0xFF000000;
53       else if (!copy1&0xFF0000)
54         hashcode^=copy1&0xFF0000;
55       else if (!copy1&0xFF00)
56         hashcode^=copy1&0xFF00;
57       else if (!copy1&0xFF)
58         hashcode^=copy1&0xFF;
59       return hashcode;
60     }
61   }
62 }
63
64 int equivalentstrings(char *str1, char *str2) {
65   if ((str1!=NULL)&&(str2!=NULL)) {
66     if (strcmp(str1,str2)!=0)
67       return 0;
68     else
69       return 1;
70   } else if ((str1==NULL)&&(str2==NULL))
71     return 1;
72   else return 0;
73 }
74