_EXPORT void extract_address(BString &address) { const char *string = address.String(); int32 first; // first, remove all quoted text if ((first = address.FindFirst('"')) >= 0) { int32 last = first + 1; while (string[last] && string[last] != '"') last++; if (string[last] == '"') address.Remove(first, last + 1 - first); } // try to extract the address now if ((first = address.FindFirst('<')) >= 0) { // the world likes us and we can just get the address the easy way... int32 last = address.FindFirst('>'); if (last >= 0) { address.Truncate(last); address.Remove(0, first + 1); return; } } // then, see if there is anything in parenthesis to throw away if ((first = address.FindFirst('(')) >= 0) { int32 last = first + 1; while (string[last] && string[last] != ')') last++; if (string[last] == ')') address.Remove(first, last + 1 - first); } // now, there shouldn't be much else left trim_white_space(address); }
_EXPORT void get_address_list(BList &list, const char *string, void (*cleanupFunc)(BString &)) { if (string == NULL || !string[0]) return; const char *start = string; while (true) { if (string[0] == '"') { const char *quoteEnd = ++string; while (quoteEnd[0] && quoteEnd[0] != '"') quoteEnd++; if (!quoteEnd[0]) // string exceeds line! quoteEnd = string; string = quoteEnd + 1; } if (string[0] == ',' || string[0] == '\0') { BString address(start, string - start); trim_white_space(address); if (cleanupFunc) cleanupFunc(address); list.AddItem(strdup(address.String())); start = string + 1; } if (!string[0]) break; string++; } }
_EXPORT void SubjectToThread (BString &string) { // a regex that matches a non-ASCII UTF8 character: #define U8C \ "[\302-\337][\200-\277]" \ "|\340[\302-\337][\200-\277]" \ "|[\341-\357][\200-\277][\200-\277]" \ "|\360[\220-\277][\200-\277][\200-\277]" \ "|[\361-\367][\200-\277][\200-\277][\200-\277]" \ "|\370[\210-\277][\200-\277][\200-\277][\200-\277]" \ "|[\371-\373][\200-\277][\200-\277][\200-\277][\200-\277]" \ "|\374[\204-\277][\200-\277][\200-\277][\200-\277][\200-\277]" \ "|\375[\200-\277][\200-\277][\200-\277][\200-\277][\200-\277]" #define PATTERN \ "^ +" \ "|^(\\[[^]]*\\])(\\<| +| *(\\<(\\w|" U8C "){2,3} *(\\[[^\\]]*\\])? *:)+ *)" \ "|^( +| *(\\<(\\w|" U8C "){2,3} *(\\[[^\\]]*\\])? *:)+ *)" \ "| *\\(fwd\\) *$" if (gRebuf == NULL && atomic_add(&gLocker,1) == 0) { // the idea is to compile the regexp once to speed up testing for (int i=0; i<256; ++i) gTranslation[i]=i; for (int i='a'; i<='z'; ++i) gTranslation[i]=toupper(i); gRe.translate = gTranslation; gRe.regs_allocated = REGS_FIXED; re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; const char *pattern = PATTERN; // count subexpressions in PATTERN for (unsigned int i=0; pattern[i] != 0; ++i) { if (pattern[i] == '\\') ++i; else if (pattern[i] == '(') ++gNsub; } const char *err = re_compile_pattern(pattern,strlen(pattern),&gRe); if (err == NULL) gRebuf = &gRe; else fprintf(stderr, "Failed to compile the regex: %s\n", err); } else { int32 tries = 200; while (gRebuf == NULL && tries-- > 0) snooze(10000); } if (gRebuf) { struct re_registers regs; // can't be static if this function is to be thread-safe regs.num_regs = gNsub; regs.start = (regoff_t*)malloc(gNsub*sizeof(regoff_t)); regs.end = (regoff_t*)malloc(gNsub*sizeof(regoff_t)); for (int start=0; (start=re_search(gRebuf, string.String(), string.Length(), 0, string.Length(), ®s)) >= 0; ) { // // we found something // // don't delete [bemaildaemon]... if (start == regs.start[1]) start = regs.start[2]; string.Remove(start,regs.end[0]-start); if (start) string.Insert(' ',1,start); } free(regs.start); free(regs.end); } // Finally remove leading and trailing space. Some software, like // tm-edit 1.8, appends a space to the subject, which would break // threading if we left it in. trim_white_space(string); }
_EXPORT void extract_address_name(BString &header) { BString name; const char *start = header.String(); const char *stop = start + strlen (start); // Find a string S in the header (email foo) that matches: // Old style name in brackets: [email protected] (S) // New style quotes: "S" <*****@*****.**> // New style no quotes if nothing else found: S <*****@*****.**> // If nothing else found then use the whole thing: S for (int i = 0; i <= 3; i++) { // Set p1 to the first letter in the name and p2 to just past the last // letter in the name. p2 stays NULL if a name wasn't found in this // pass. const char *p1 = NULL, *p2 = NULL; switch (i) { case 0: // [email protected] (S) if ((p1 = strchr(start,'(')) != NULL) { p1++; // Advance to first letter in the name. size_t nest = 1; // Handle nested brackets. for (p2 = p1; p2 < stop; ++p2) { if (*p2 == ')') --nest; else if (*p2 == '(') ++nest; if (nest <= 0) break; } if (nest != 0) p2 = NULL; // False alarm, no terminating bracket. } break; case 1: // "S" <*****@*****.**> if ((p1 = strchr(start, '\"')) != NULL) p2 = strchr(++p1, '\"'); break; case 2: // S <*****@*****.**> p1 = start; if (name.Length() == 0) p2 = strchr(start, '<'); break; case 3: // S p1 = start; if (name.Length() == 0) p2 = stop; break; } // Remove leading and trailing space-like characters and save the // result if it is longer than any other likely names found. if (p2 != NULL) { while (p1 < p2 && (isspace (*p1))) ++p1; while (p1 < p2 && (isspace (p2[-1]))) --p2; int newLength = p2 - p1; if (name.Length() < newLength) name.SetTo(p1, newLength); } } int32 lessIndex = name.FindFirst('<'); int32 greaterIndex = name.FindLast('>'); if (lessIndex == 0) { // Have an address of the form <address> and nothing else, so remove // the greater and less than signs, if any. if (greaterIndex > 0) name.Remove(greaterIndex, 1); name.Remove(lessIndex, 1); } else if (lessIndex > 0 && lessIndex < greaterIndex) { // Yahoo stupidly inserts the e-mail address into the name string, so // this bit of code fixes: "Joe <*****@*****.**>" <*****@*****.**> name.Remove(lessIndex, greaterIndex - lessIndex + 1); } trim_white_space(name); header = name; }
void shell() { current_database = -1; char entrada[1000], nomeBD[TAM_NOME_BANCO]; int resultado = 0, codDB = -1; nomeBD[0]='\0'; char *current_db_name = strdup(">");//inicializa com nenhum banco conectado char *start; start = strdup("dbms-start");//este comando posteriormente como start do banco, no momento ele é automatico printf("\nWelcome to the DBMS Interface.\nType 'help' '\\h' for help.\n\n"); /** * **************************** * * Comandos do shell * * **************************** */ using_history ();//função para usar o histórico read_history (".history_file"); while(1) { int nTokens; strcpy(entrada, readline(current_db_name)); /** * Adiciona ao histórico */ if (entrada[0]) { char *expansion; int result; result = history_expand (entrada, &expansion); if (result) fprintf (stderr, "%s", expansion); if (result < 0 || result == 2) { free (expansion); continue; } add_history (expansion); strncpy (entrada, expansion, sizeof (entrada) - 1); free (expansion); write_history (".history_file");//adiciona no histórico } char **tokens = tokenize( trim_white_space(remove_newline(entrada)),' ',&nTokens); /** * Opção para criar tabela e banco de dados */ if (strcmp(strtolower(tokens[0]),"create")==0) { if(strcmp(strtolower(tokens[1]),"table")==0) { if (current_database == -1) { printf("Not connected to any database.\n"); continue; } createTable(entrada,current_database); } else if(strcmp(strtolower(tokens[1]),"database")==0) { if (nTokens >= 5) { printf("Invalid command. Type help to show de interface usage.\n"); continue; } if (strlen(tokens[2]) > TAM_NOME_BANCO ) { printf("Database name too big.\n"); continue; } resultado = checkCreateDB( remove_semicolon(tokens[2]) );//verifica a existência do nome e grava-o no arquivo if(resultado==-1) { printf("Error creating database file.\n"); } if(resultado==-3) { printf("Database exists.\n"); } else { printf("Database created successfully.\n"); } } else { printf("Invalid command. Type help to show de interface usage.\n"); continue; } } /** * Conecta ao banco de dados passado como parâmetro */ else if(strcmp(strtolower(tokens[0]),"\\c") == 0){ if (nTokens != 2) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); continue; } char *name_db = remove_semicolon(tokens[1]); codDB = busca(name_db,1); //função chamada para conecção no banco, retorna o codigo do banco ao conectar if (codDB >= 0) { strcpy(nomeBD, name_db); //passa o nome do bd, para a variavel mostrar ao usuario qual o banco conectado free(current_db_name); current_db_name = (char*) malloc (sizeof(char)*(strlen(name_db)+3)); if (current_db_name == NULL) { printf("Out of memory.\nAborting...\n"); } strcpy(current_db_name,name_db); current_database = codDB; strcat(current_db_name,"=#"); current_db_name[strlen(current_db_name)] = '\0'; } else { printf("No such database '%s'.\n", name_db); continue; } } /** * Insere tuplas em uma tabela */ else if(strcmp(strtolower(tokens[0]),"insert")==0) { if (current_database == -1) { printf("Not connected to any database.\n"); continue; } insert(entrada,current_database); } /** * Imprime as tabelas do banco de dados atual * ou o esquema de uma tabela */ else if(strcmp(strtolower(tokens[0]),"\\d")==0) { if (current_database == -1) { printf("Not connected to any database.\n"); continue; } if (nTokens >= 3) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); continue; } else if (nTokens == 1) { //imprime tabelas do banco de dados listaTabelas(current_database); } else { //imprime o esquema de uma tabela char *t = table_name_real(remove_semicolon(tokens[1]),current_database); if(!verificaNomeTabela(t)){ printf("Invalid table name.\n"); free(t); continue; } struct fs_objects objeto = leObjeto(t);//para verificar se a tabela esta no banco show_schema(objeto,tokens[1]); free(t); } } /** * Comando temporário para imprimir tabela */ else if (strcmp(strtolower(tokens[0]),"select")==0) { if (current_database == -1) { printf("Not connected to any database.\n"); continue; } selectTable(entrada,current_database); } /** * Imprime os registros da tabela passada */ else if (strcmp(strtolower(tokens[0]),"show")==0) { if (nTokens != 2) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); continue; } if (current_database == -1) { printf("Not connected to any database.\n"); continue; } if (verificaNomeTabela(table_name_real(remove_semicolon(tokens[1]),current_database) ) == 0 ) { printf("Table %s doesn't exist.\n",remove_semicolon(tokens[1])); continue; } char *t = table_name_real(remove_semicolon(tokens[1]),current_database); char *file = table_name_real(remove_semicolon(tokens[1]),current_database); strcat(file,".dat"); if (existeArquivo(file) == 0) { printf("Table is empty.\n" ); continue; } imprime(t); free(file); free(t); } /** * Lista os bancos existentes */ else if(strcmp(strtolower(tokens[0]),"\\l")==0) { if (nTokens != 1) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); continue; } //LISTA os bancos existentes listaBancos(); } /** * Opção para deletar o banco de dados e tabelas */ else if(strcmp(strtolower(tokens[0]),"drop")==0) { if (nTokens != 3) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); continue; } else if(strcmp(strtolower(tokens[1]),"table") == 0){ if (current_database == -1) { printf("Not connected to any database.\n"); continue; } if (verificaNomeTabela(table_name_real(remove_semicolon(tokens[2]),current_database) ) == 0 ) { printf("Table %s doesn't exist.\n",remove_semicolon(tokens[2])); continue; } char *t = table_name_real(remove_semicolon(tokens[2]),current_database); char *exist = table_name_real(remove_semicolon(tokens[2]),current_database); int ok = excluirTabela(t); if (ok == SUCCESS) { printf("Table deleted successfully.\n"); } free(exist); free(t); } else if(strcmp(strtolower(tokens[1]),"database") == 0){ char *exist = table_name_real(remove_semicolon(tokens[2]),current_database); strcat(exist,".dat"); if (existeArquivo(exist) != 0) { printf("The database is not empty for drop, there are existing tables.\n" ); continue; } exist = remove_semicolon(tokens[2]); codDB = busca(exist,1); if(codDB == current_database) { printf("Cannot drop the currently open database.\n"); continue; } int drop = dropDatabase(remove_semicolon(tokens[2])); if(drop == 1)printf("Database deleted successfully.\n"); free(exist); } } /** * Ajuda ao usuário com exemplos da sintaxe dos comandos */ else if (strcmp(strtolower(tokens[0]),"help")==0 || strcmp(strtolower(tokens[0]),"\\h")==0) { if (nTokens != 1) { printf("Invalid number of arguments. Type help to show the interface usage.\n"); } help(); } /** * Imprime mensagem de copyright */ else if(strcmp(strtolower(remove_semicolon(tokens[0])),"\\copyright")==0) { printf("\nDatabase Management System\n"); printf("\nPermission to use, copy, modify, and distribute this software and its\ndocumentation for any purpose, without fee, and without a written agreement\nis hereby granted, provided that the above copyright notice and this\nparagraph and the following two paragraphs appear in all copies.\n"); printf("\nTHIS SOFTWARE IS BEING DEVELOPED BY STUDENTS OF DATABASE II CLASS AT UNIVERSIDADE FEDERAL DA FRONTEIRA SUL.\n\n"); } /** * Comando de saída */ else if(strcmp(strtolower(remove_semicolon(tokens[0])),"exit")==0) { break; } else if(strcmp(strtolower(remove_semicolon(tokens[0])),"quit")==0) { break; } else if(strcmp(strtolower(remove_semicolon(tokens[0])),"bye")==0) { break; } else if(strcmp(strtolower(remove_semicolon(tokens[0])),"\\q")==0) { break; } else { printf("Invalid command. Type help to show the interface usage.\n"); continue; } } free(start); free(current_db_name); }
Exp_info *exp_read_staden_info(mFILE *fp, char *filename) /* * Read a staden file into an Exp_info data structure */ { Exp_info *e; char *seq; char *fn; /* find last / in filename */ for(fn=filename+strlen(filename)-1;fn>filename && *fn!='/';fn--); if (*fn=='/') fn++; if ( (e = exp_create_info()) != NULL ) { char line[128]; /* an upper limit for the file size */ int max_seq_size = file_size(filename); int left, right, len, i; int lineno; int formatIsBap; int CS_from, CS_to; int SR; CS_from = CS_to = SR = 0; /*ID*/ (void)ArrayRef(e->entries[EFLT_ID],e->Nentries[EFLT_ID]++); exp_get_entry(e,EFLT_ID) = (char *)strdup(fn); /*EN*/ (void)ArrayRef(e->entries[EFLT_EN],e->Nentries[EFLT_EN]++); exp_get_entry(e,EFLT_EN) = (char *)strdup(fn); /*CC*/ (void)ArrayRef(e->entries[EFLT_CC],e->Nentries[EFLT_CC]++); exp_get_entry(e,EFLT_CC) = (char *)strdup("Created from a staden format sequence assembly file"); seq = (char *) xmalloc(max_seq_size+1); if (!seq) return NULL; left = 0; right = 0; len = 0; lineno = 0; formatIsBap = 1; while (mfgets(line,sizeof(line),fp)!=NULL) { char *s; lineno++; if (lineno == 1) { int pos, ret; char *cp; /* * This maybe a fasta format file. */ if (line[0] == '>') { if (cp = strchr(line, ' ')) *cp = 0; if (cp = strchr(line, '\t')) *cp = 0; if (cp = strchr(line, '\n')) *cp = 0; exp_set_entry(e, EFLT_ID, strdup(line+1)); exp_set_entry(e, EFLT_EN, strdup(line+1)); continue; } /* * This maybe a file created from 'output consensus', * in which case it'll have the Staden format style of: * " <-----T2.00047----->" header on the first line. * * We strip this off. Argh - why don't Suns have the * ANSI function memmove() ? */ ret = sscanf(line, " <%*18s>%n", &pos); if (ret && pos == 21) { int i, e = sizeof(line)-21; for (i=0; i < e; i++) line[i] = line[i+21]; /* memmove((void *)line, * (void *)(line + 21), * sizeof(line)-21); */ } } if (line[0] == ';') { /******************************************** * Title line parsing *******************************************/ if (lineno==1 && !(line[1] == ';' || line[1] == '<' || line[1] == '>') ) { /* format of line is: * <ln> ::= ;<i6><i6><i6><c4><c+> * <i6> ::= <s><s><s><s><s><d> | ... <d><d><d><d><d><d> * <c4> ::= <C><C><C><s> * <c+> ::= <a><c+> | <a> * <a> is [a-zA-Z0-9.] * <s> is ' ' * <d> is [0-9] * <C> is [A-Z] */ int d; if (sscanf(line,";%6d%6d%6d",&d,&d,&d)==3 && strlen(line)>23) { /* trim off trailing white space */ trim_white_space(line+23); /*LN*/ (void)ArrayRef(e->entries[EFLT_LN],e->Nentries[EFLT_LN]++); exp_get_entry(e,EFLT_LN) = (char *)strdup(line+23); line[23]='\0'; /*LT*/ (void)ArrayRef(e->entries[EFLT_LT],e->Nentries[EFLT_LT]++); /* trim off trace type white space */ trim_white_space(line+19); exp_get_entry(e,EFLT_LT) = (char *)strdup(line+19); } } else if (formatIsBap) { switch (line[1]) { case '<': for(s=&line[2];*s;s++) { if(!isspace(*s) && isprint(*s)) seq[left++] = *s; } break; case '>': for(s=&line[2];*s;s++) { if(!isspace(*s) && isprint(*s)) seq[max_seq_size-right++] = *s; } break; case ';': /*TG*/ #if 0 trim_white_space(line); (void)ArrayRef(e->entries[EFLT_TG],e->Nentries[EFLT_TG]++); /* convert format from Staden format to * Experiment file format */ { char *cp; int pos, len; char type[5]; cp = (char *)xmalloc(strlen(line)+20); if (cp == NULL) break; sscanf(line, ";;%4s %6d %6d", type, &pos, &len); /* * Need to add 'left' to each start position * in tag. ASSUMPTION: 'left' has already been * defined. Ie that the ;< lines are before * any ;; lines. */ pos += left; values2tag(cp, type, pos, pos + len - 1, 2, &line[20]); exp_get_entry(e,EFLT_TG) = cp; } if (strncmp(line+2,"IGNC",4)==0 || strncmp(line+2,"CVEC",4)==0) { CS_from = atoi(&line[2+4+1]); } else if (strncmp(line+2,"IGNS",4)== 0 || strncmp(line+2,"SVEC",4)== 0) { SR = 1; } #endif break; default: break; } } } else { /******************************************** * The actual sequence bit *******************************************/ formatIsBap = 0; /* turn off title line parsing stuff */ for (s=line;*s;s++) { if(!isspace(*s) && isprint(*s)) seq[left+len++] = *s; } } } /* * The right cutoff has been stashed into the end of the array * Move to correct place */ for(i=(max_seq_size-(left+len))/2;i>=0;i--) { char temp; /* swap */ temp = seq[left+len+i]; seq[left+len+i] = seq[max_seq_size-i]; seq[max_seq_size-i] = temp; } /* null terminate */ seq[left+len+right] = '\0'; /*SQ*/ (void)ArrayRef(e->entries[EFLT_SQ],e->Nentries[EFLT_SQ]++); exp_get_entry(e,EFLT_SQ) = seq; /*SL*/ sprintf(line,"%d",left); (void)ArrayRef(e->entries[EFLT_SL],e->Nentries[EFLT_SL]++); exp_get_entry(e,EFLT_SL) = (char *)strdup(line); /*SR*/ if (SR) { sprintf(line,"%d",left+len+1); (void)ArrayRef(e->entries[EFLT_SR],e->Nentries[EFLT_SR]++); exp_get_entry(e,EFLT_SR) = (char *)strdup(line); } /*CS*/ if (CS_from) { if (CS_from == 1) { CS_to = left; } else { CS_from = left + len + 1; CS_to = left + len + right; } sprintf(line,"%d..%d",CS_from,CS_to); (void)ArrayRef(e->entries[EFLT_CS],e->Nentries[EFLT_CS]++); exp_get_entry(e,EFLT_CS) = (char *)strdup(line); } /*QR*/ if (!SR && !CS_from) { sprintf(line,"%d",left+len+1); (void)ArrayRef(e->entries[EFLT_QR],e->Nentries[EFLT_QR]++); exp_get_entry(e,EFLT_QR) = (char *)strdup(line); } #if 0 /*TG*/ { int i; /* need to add LEFT to each start position in tag */ for(i=0;i<e->Nentries[EFLT_TG];i++) { sprintf(line,"%4.4s %6d%s", arr(char *,e->entries[EFLT_TG],i), atoi(arr(char *,e->entries[EFLT_TG],i)+5)+left, arr(char *,e->entries[EFLT_TG],i)+11); xfree(arr(char *,e->entries[EFLT_TG],i)); arr(char *,e->entries[EFLT_TG],i) = (char *)strdup(line); } } #endif }