📄 util.cpp
字号:
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include "Util.h"char *oct[8]={"000", "001", "010", "011", "100", "101", "110", "111"};char *hex[16]={"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001","1010","1011", "1100", "1101", "1110", "1111"};char *binary(char *s){ char *nrb; char *buf=(char*)calloc(10,sizeof(char)); char *output; t_BOOL special=t_FALSE; int i=0,nr_b,len; nrb=s; while(*nrb!='\''){nrb++;i++;} strncpy(buf,s,i); buf[i]='\0'; nr_b=atoi(buf); len=(i==0)?32:nr_b; output=(char*)calloc(len+4,sizeof(char)); output[0]='B'; output[1]='"'; output[len+2]='"'; output[len+3]='\0'; i=len+1; nrb=s; while(*nrb++!='\0'); nrb--; nrb--; if(*nrb=='x'||*nrb=='z'||*nrb=='X'||*nrb=='Z'||*nrb=='?'){ special=t_TRUE; nrb--; } while(i>1&&*nrb!='b'){ if(*nrb!='_') output[i--]=*nrb; nrb--; } while(i>1) output[i--]=(special)?'z':'0'; return output;} char *decimal(char *s){ int len = 0, i = 0, offset, j; char *ps, *output; char buf[10]=""; ps = s; while(*ps != '\'' && *ps != '\0' ){ ps++; i++; } if (i < strlen(ps)) strncpy(buf, s, i); buf[i + 1] = '\0'; if (!strcmp(buf, "")) len = 32; else len = atoi(buf); output = (char*)calloc(len + 4, sizeof(char)); output[0] = 'B'; output[1] = '"'; output[len + 2] = '"'; output[len + 3] = '\0'; char fill='0'; ps++; ps++; int value = atoi(ps); j = value % 2; for (i = len + 1 ; (i >= 2 && value > 0) ; i--, value /= 2, j = value % 2) output[i] = j + '0'; for (j = i; j >= 2; j--) output[j] = fill; return output;}char* clean(char *s){ int i, j; char* output = (char*)malloc((strlen(s)+1)*sizeof(char)); i = 0; j = 0; while(s[i] != '\0'){ if (s[i] != '_') output[j++] = s[i]; i++; } output[j] = '\0'; return output;} char *octal(char *s){ int len = 0, i = 0, offset, j; char *ps, *output; char buf[10]=""; ps = s; offset = strlen(ps); while(*ps != '\'' && *ps != '\0' ){ ps++; i++; } if (i < offset) strncpy(buf, s, i); buf[i + 1] = '\0'; if (!strcmp(buf, "")) len = 32; else len = atoi(buf); output = (char*)calloc(len + 4, sizeof(char)); output[0] = 'B'; output[1] = '"'; output[len + 2] = '"'; output[len + 3] = '\0'; char fill=ps[strlen(ps)-1]; ps++; ps++; j = strlen(ps) - 1; if(fill=='x'||fill=='z'||fill=='X'||fill=='Z'||fill=='?'){ fill='z'; //ps[strlen(ps)-1]='\0'; j--; } else fill='0'; int k = 2; for(i = len + 1; (i>=2 &&j>-1); i--){ if (ps[j] == '_'){ j--; i++; continue; } output[i] = oct[ps[j] - '0'][k]; k--; if (k == -1){ j--; k = 2; } } for (j = i; j >= 2; j--){ output[j] = fill; } return output;}char *hexa(char *s){ int len = 0, i = 0, offset, j; char *ps, *output; char *buf=(char*)calloc(10,sizeof(char)); ps = s; offset = strlen(ps); while(*ps != '\'' && *ps != '\0' ){ ps++; i++; } if (i < offset) strncpy(buf, s, i); buf[i + 1] = '\0'; if (!strcmp(buf, "")) len = 32; else len = atoi(buf); output = (char*)calloc(len + 4, sizeof(char)); output[0] = 'B'; output[1] = '"'; output[len + 2] = '"'; output[len + 3] = '\0'; char fill=ps[strlen(ps)-1]; ps++; ps++; int k = 3; j = strlen(ps) - 1; if(fill=='x'||fill=='z'||fill=='X'||fill=='Z'||fill=='?'){ fill='z'; //ps[strlen(ps)-1]='\0'; j--; } else fill = '0'; for(i = len + 1; (i>=2 &&j>-1); i--){ if(ps[j] == '_'){ j--; i++; continue; } if(ps[j]>='0'&&ps[j]<='9') output[i] = hex[ps[j] - '0'][k]; if (ps[j] == 'A' | ps[j] =='a') output[i] = hex[10][k]; if (ps[j] == 'B'| ps[j] =='b') output[i] = hex[11][k]; if (ps[j] == 'C'| ps[j]=='c') output[i] = hex[12][k]; if (ps[j] == 'D'| ps[j]=='d') output[i] = hex[13][k]; if (ps[j] == 'E'| ps[j]=='e') output[i] = hex[14][k]; if (ps[j] == 'F'| ps[j]=='f') output[i] = hex[15][k]; k--; if (k == -1){ j--; k = 3; } } for (j = i; j >= 2; j--){ output[j] = fill; } return output;}char *expres3(const char *$1,const char *$2,const char*$3){ char *ptr=(char*)calloc(strlen($1)+strlen($2)+strlen($3)+1,sizeof(char)); strcpy(ptr,$1);strcat(ptr,$2);strcat(ptr,$3); return ptr;}char *expres2(const char *$1,const char *$2){ char *ptr=(char*)calloc(strlen($1)+strlen($2)+1,sizeof(char)); strcpy(ptr,$1);strcat(ptr,$2); return ptr;}char *edge_event(char*s){ char *ptr; ptr=s; while(*ptr!='\''&&*ptr!='\0') ptr++; *ptr='\0'; ptr=(char*)calloc(strlen(s)+1,sizeof(char)); strcpy(ptr,s); return ptr;}void Tab::printTab(FILE *fp){ for(int j=0;j<i;j++) fprintf(fp," ");}void file2file(FILE *fp,char *file_name){ FILE *ftmp=fopen(file_name,"r"); if(ftmp==NULL) return; char buf[50]; int bread=50; while(bread==50){ bread=fread(buf,sizeof(char),50,ftmp); fwrite(buf,sizeof(char),bread,fp); } fclose(ftmp);}void dumm_test(char *s){ if(s[0]=='B'){ return; } fprintf(stderr,"\a\aParser PANIC... (unimplemented operation)??!!??\a\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -