int read_cat(t_opt opts) { int nl; int nb; char str[BUFF_SIZE + 1]; my_init(&nl, &nb); clean_str(&str[0]); while (read(0, &str, BUFF_SIZE) > 0) { str[BUFF_SIZE] = 0; exec_cat(str, opts, &nb, &nl); clean_str(&str[0]); } return (0); }
///////////////////////////////////////////////////////////////////////////////////// /// read raw text from a string and create a initial DOM tree. /// the paragraphs are separated by CR ("\r\n") ///////////////////////////////////////////////////////////////////////////////////// int XML4NLP::CreateDOMFromString(const string & str) { ClearDOM(); if (0 != BuildDOMFrame()) return -1; string strTmp = str; replace_char_by_char(strTmp, '\r', '\n'); // std::cout << strTmp << std::endl; istringstream in(strTmp); // How to use istringstream? int i = 0; while (getline(in, strTmp)) { clean_str(strTmp); if (strTmp.empty()) { continue; } if (0 != BuildParagraph(strTmp, i++)) { return -1; } } return 0; }
///////////////////////////////////////////////////////////////////////////////////// /// read a raw text file and create a initial DOM tree. /// the paragraphs are separated by CR ("\r\n") ///////////////////////////////////////////////////////////////////////////////////// int XML4NLP::CreateDOMFromFile(const char* fileName) { ClearDOM(); if (0 != BuildDOMFrame()) return -1; ifstream in; in.open(fileName); if ( !in.is_open() ) { cerr << "xml4nlp load file error: " << fileName << endl; return -1; } string line; int i = 0; while (getline(in, line)) { clean_str(line); // Zhenghua Li, 2007-8-31, 15:57 // remove_space_gbk(line); if (line.empty()) { continue; } if (0 != BuildParagraph(line, i++)) return -1; } return 0; }
void string2pair(const string& str, pair<string, string>& pairStr, const char separator) { string::size_type pos = str.find_last_of(separator); if (pos == string::npos) { string tmp = str + ""; clean_str(tmp); pairStr.first = tmp; pairStr.second = ""; } else { string tmp = str.substr(0, pos); clean_str(tmp); pairStr.first = tmp; tmp = str.substr(pos+1); clean_str(tmp); pairStr.second = tmp; } }
static t_mail * mailbox_read(t_mailbox * mailbox, unsigned int idx) { char const * dentry; unsigned int i; t_mail * rez; FILE * fd; char * filename; if (mailbox==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox"); return NULL; } if (mailbox->maildir==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir"); return NULL; } rez=xmalloc(sizeof(t_mail)); p_rewinddir(mailbox->maildir); dentry = NULL; /* if idx < 1 we should not crash :) */ for(i=0;i<idx && (dentry=p_readdir(mailbox->maildir))!=NULL;i++) if (dentry[0]=='.') i--; if (dentry==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"index out of range"); xfree(rez); return NULL; } rez->timestamp=atoi(dentry); filename=xmalloc(strlen(dentry)+1+strlen(mailbox->path)+1); sprintf(filename,"%s/%s",mailbox->path,dentry); if ((fd=fopen(filename,"rb"))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"error while opening message"); xfree(rez); xfree(filename); return NULL; } xfree(filename); rez->sender=xmalloc(256); fgets(rez->sender,256,fd); /* maybe 256 isnt the right value to bound a line but right now its all i have :) */ clean_str(rez->sender); rez->message=xmalloc(256); fgets(rez->message,256,fd); clean_str(rez->message); fclose(fd); rez->timestamp=atoi(dentry); return rez; }
char *fgets_clean_eof(char *s, FILE *stream) { char *ret_str; while (1) { if (fgets (s, TEMP_STR_SIZE, stream) == NULL) return NULL; s[TEMP_STR_SIZE-1] = '\0'; if (s[0] == '#') continue; ret_str = clean_str(s); if (ret_str[0] != '\0') break; } return ret_str; }
char *diga_ola(const char *nome) { /* A função `diga_ola` deve ser escrita de tal forma que receba como parâmetro um argumento *string*. Deve retornar a *string* "Olá, ", seguida do argumento recebido, mais um ponto final. A *string* recebida deve estar limpa, ou seja, sem caracteres de espaço no começo ou no fim. Se a *string* estiver vazia, retorna apenas "Olá!" diga_ola("") -> "Olá!" diga_ola(" ") -> "Olá!" diga_ola("Paulo") -> "Olá, Paulo." diga_ola(" Paulo ") -> "Olá, Paulo." */ /* coloque aqui o seu código */ char *sem_espacos, *masc, *gretting; char *empty = "Olá!"; char *no_empty = "Olá, %s."; sem_espacos = clean_str(nome); if(NULL==sem_espacos) { return NULL; } if(0==strlen(sem_espacos)) { masc = empty; } else { masc = no_empty; } gretting = (char *)calloc(strlen(masc)+strlen(sem_espacos)+1, sizeof(char)); if(NULL==gretting) { free(sem_espacos); return NULL; } sprintf(gretting, masc, sem_espacos); return gretting; }
static struct maillist_struct * mailbox_get_list(t_mailbox *mailbox) { char const * dentry; FILE * fd; struct maillist_struct *rez=NULL, *p=NULL,*q; char *sender,*filename; if (mailbox==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox"); return NULL; } if (mailbox->maildir==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir"); return NULL; } filename=xmalloc(strlen(mailbox->path)+1+15+1); p_rewinddir(mailbox->maildir); for(;(dentry=p_readdir(mailbox->maildir))!=NULL;) if (dentry[0]!='.') { q=xmalloc(sizeof(struct maillist_struct)); sprintf(filename,"%s/%s",mailbox->path,dentry); if ((fd=fopen(filename,"rb"))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"error while opening message file"); xfree(filename); xfree(q); return rez; } sender=xmalloc(256); fgets(sender,256,fd); clean_str(sender); fclose(fd); q->sender=sender; q->timestamp=atoi(dentry); q->next=NULL; if (p==NULL) rez=q; else p->next=q; p=q; } xfree(filename); return rez; }
static int get_input(guint8* out, gsize len, const gchar* const prompt) { guint8* raw; gsize newlen; raw = g_malloc0(len); fputs(prompt, stdout); if (fgets(raw, len, stdin) == NULL) { perror("get_input"); return -1; } g_strchomp(raw); clean_str(raw); if ((newlen = convert(out, raw, strlen(raw))) == -1) { g_fprintf(stderr, "Could not convert mesage\n"); return -1; } g_free(raw); return newlen; }
bool Cxml::get_node(char* xml_string) { int k = m_cursor; int j = 0; //second level cursor; bool bCDATA = false; bool bIsPI = false; //Set to true if the cursor is curently inside a processing instruction char cDelim = 0; char c = xml_string[k]; const char COPEN = '<'; const char CCLOSE = '>'; const char CSLASH = '/'; const char CSPACE = ' '; const char CQUOTE = '\''; const char CDQUOTE = '\"'; const char CEQUAL = '='; const char CNEW = '\n'; const char CTAB = '\t'; const char CEXCLAMATION = '!'; const char CMINUS = '-'; const char CSQRPO = '['; const char CSQRPC = ']'; const char SZCDATA[9] = "![CDATA["; const char CQM = '?'; const char CRET = 13; // carriage return char *szNodeNameBuff = (char *)calloc(256,sizeof(char)); char *szNodeValBuff = (char *)calloc(256,sizeof(char)); char *szAttrNameBuff = (char *)calloc(256,sizeof(char)); char *szAttrValBuff = (char *)calloc(256,sizeof(char)); if(k >= m_length) return false; m_root_node->set_name("XML_DOC"); m_root_node->set_value(""); // just in case we want to access it later... element* Current = m_root_node->add_child_element(); // We are going to march through the file, one character at a time // (k counts the file position, and sort the characters into XML // elements as they appear. while(k<m_length) { c = xml_string[k]; if(c == CNEW || c == CTAB || c == CRET) { k++; // This is white space. Eat it. continue; } if(c == COPEN) // Found an "open" character (<) { if(xml_string[k+1] == CEXCLAMATION && xml_string[k+2] == CMINUS && xml_string[k+3] == CMINUS) // this is a comment { //the comment section clean_str(szAttrValBuff); k+=4; c = xml_string[k]; while(!(xml_string[k] == CMINUS && xml_string[k+1] == CMINUS && xml_string[k+2] == CCLOSE)) // Find the end of the comment. { szAttrNameBuff = concat(szAttrNameBuff, c); c = xml_string[++k]; } k+=3; // MODIFIED BY TS // We don't really want to process comments at all, so just // discard them. //if(Current->get_name() != NULL) //we have set this node, navigate to a child of it // Current = Current->add_child_element(); //Current->set_comment(szAttrNameBuff); //set it as a comment node //Current = Current->get_parent(); //return to the previous level continue; } while(k+10 < m_length && j < 9) { if(xml_string[k+1+j] != SZCDATA[j]) break; j++; if(j==8) { // This is definitely a CDATA section k = k + j; int start = k; while((k + 3) < m_length && xml_string[k+1] != CSQRPC && xml_string[k+2] != CSQRPC && xml_string[k+3] != CCLOSE) { k++; } int stop = k; k+=5; char* buffer = (char*)calloc(stop - start + 2, sizeof(char)); copyx(buffer, xml_string, start, stop); Current->set_value(buffer); free(buffer); j = 0; bCDATA = true; break; } } if(bCDATA) { bCDATA = false; continue; } clean_str(szNodeNameBuff); if(xml_string[k+1] == CSLASH) {// This is a close tag, hopefully for the last opened node. Current = Current->get_parent(); k++; while(xml_string[k] != CCLOSE) { k++; } k++; continue; } if(xml_string[k+1] == CQM) { c = xml_string[++k]; bIsPI = true; } // If we are here, this is an open tag. So create a new node. c = xml_string[++k]; while(c != CSLASH && c != CSPACE && c != CCLOSE) {//Loop until the node name has been entirely read. if(c != CNEW && c != CTAB && c != CRET) szNodeNameBuff = concat(szNodeNameBuff,c); c = xml_string[++k]; } if(Current != NULL) // this node is set, navigate to a child of it if(Current->get_name() != NULL) Current = Current->add_child_element(); Current->set_name(szNodeNameBuff); // We are inside the element tag, though at the end of the // element name. Therefore, if there's a space here, // there must be an attribute coming. while(c == CSPACE) { c = xml_string[++k]; if(c == CSLASH) { break; } if(c == CQM && bIsPI) { break; } clean_str(szAttrNameBuff); clean_str(szAttrValBuff); // Get ready for a new attribute. attribute* pA = new attribute(); // Accumulate characters until there is an equal sign, when // we'll know the attribute name has been read. while(c != CEQUAL) { if (c != CNEW && c != CTAB && c != CRET) szAttrNameBuff = concat(szAttrNameBuff, c); c = xml_string[++k]; } // Is the attribute value in single or double quotes? c = xml_string[++k]; if (c == CQUOTE || c == CDQUOTE) { cDelim = c; c = xml_string[++k]; } // Accumulate characters until the next delimiter, when // we'll know the attribute value has been read. while(c != cDelim && cDelim != 0) { if(c != CNEW && c != CTAB && c != CRET) szAttrValBuff = concat(szAttrValBuff, c); c = xml_string[++k]; } // Reset the delimiter indicator, and advance one character. cDelim = 0; c = xml_string[++k]; // Set the name and value of our new attribute. pA->set_name(szAttrNameBuff); pA->set_value(szAttrValBuff); Current->add_attribute(pA); } // Repeat if the next character is a space. if(c == CSLASH) // A slash here indicates a singleton element. { Current = Current->get_parent(); c=xml_string[++k]; while(c != CCLOSE) { c = xml_string[++k]; } } if(c == CQM && bIsPI) { Current->set_as_pi(); Current = Current->get_parent(); c=xml_string[++k]; bIsPI = false; while(c != CCLOSE) { c = xml_string[++k]; } } if(c == CCLOSE) { ; } } // If there isn't an open character (<) here, this is the // contents of an element. Record the characters. Originally // (pre-TS), this would not allow leading spaces or leading // slashes (/) in an element. Don't know why, but these have // been tentatively commented out by TS 6/11/16 if(c != COPEN && c != CCLOSE /*&& c != CSLASH*//* && c != CSPACE*/) { clean_str(szNodeValBuff); while(c != COPEN) { if(c != CNEW && c != CTAB && c != CRET/* && c != CSPACE*/) szNodeValBuff =concat(szNodeValBuff,c); c = xml_string[++k]; } Current->set_value(szNodeValBuff); continue; } k++; } free(szNodeNameBuff); free(szNodeValBuff); free(szAttrNameBuff); free(szAttrValBuff); return true; }
static void mail_func_read(t_connection * c, const char * str) { t_account * user; t_mailbox * mailbox; const char *p; char tmp[256]; int i; if (c==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection"); return; } if (str==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string"); return; } for(i=0;str[i]==' ';i++); p=str+i; if ((user=conn_get_account(c))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL account"); return; } mailbox=mailbox_open(user, mbox_mode_read); if (*p=='\0') { /* user wants to see the mail summary */ struct maillist_struct *maill, *mp; unsigned int idx; if (!mailbox_count(mailbox)) { message_send_text(c,message_type_info,c,"You have no mail."); mailbox_close(mailbox); return; } if ((maill=mailbox_get_list(mailbox))==NULL) { eventlog(eventlog_level_error,__FUNCTION__,"got NULL maillist"); mailbox_close(mailbox); return; } sprintf(tmp,"You have %d messages. Your mail qouta is set to %d.",mailbox_count(mailbox),get_mail_quota(user)); message_send_text(c,message_type_info,c,tmp); message_send_text(c,message_type_info,c,"ID Sender Date"); message_send_text(c,message_type_info,c,"-------------------------------------"); for(mp=maill,idx=1;mp!=NULL;mp=mp->next,idx++) { sprintf(tmp,"%02u %-14s %s",idx,mp->sender,ctime(&mp->timestamp)); clean_str(tmp); /* ctime() appends an newline that we get cleaned */ message_send_text(c,message_type_info,c,tmp); } message_send_text(c,message_type_info,c,"Use /mail read <ID> to read the content of any message"); mailbox_unget_list(maill); } else { /* user wants to read a message */ int idx; t_mail * mail; for(i=0;p[i]>='0' && p[i]<='9' && p[i]!='\0';i++); if (p[i]!='\0' && p[i]!=' ') { message_send_text(c,message_type_error,c,"Invalid index. Please use /mail read <index> where <index> is a number."); mailbox_close(mailbox); return; } idx=atoi(p); if (idx<1 || idx>mailbox_count(mailbox)) { message_send_text(c,message_type_error,c,"That index is out of range."); mailbox_close(mailbox); return; } if ((mail=mailbox_read(mailbox,idx))==NULL) { message_send_text(c,message_type_error,c,"There was an error completing your request."); mailbox_close(mailbox); return; } sprintf(tmp,"Message #%d from %s on %s:",idx,mail->sender,clean_str(ctime(&mail->timestamp))); message_send_text(c,message_type_info,c,tmp); message_send_text(c,message_type_info,c,mail->message); mailbox_unread(mail); } mailbox_close(mailbox); }
static void display_line_log(log_stream_t *p_log, const char *tag, const char *format, va_list arglist) { char line_log[MAX_LINE_LEN]; int written; time_t now = time(NULL); unsigned int th = GetThreadIndex(); struct tm date; int would_print; if (log_initialized) { /* periodically check if log files have been renamed */ if (now - last_time_test > TIME_TEST_FILE) { test_file_names(); last_time_test = now; } } pthread_rwlock_rdlock(&p_log->f_lock); /* if logs are not initalized or the log is a NULL FILE*, * default logging to stderr */ if ((!log_initialized) || ((p_log->log_type != RBH_LOG_SYSLOG) && (p_log->f_log == NULL))) { localtime_r(&now, &date); written = snprintf(line_log, MAX_LINE_LEN, "%.4d/%.2d/%.2d %.2d:%.2d:%.2d %s[%lu/%u] %s%s", 1900 + date.tm_year, date.tm_mon + 1, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec, log_config.log_process ? "robinhood" : "", (unsigned long)getpid(), th, tag ? tag : "", tag ? " | " : ""); would_print = vsnprintf(line_log + written, MAX_LINE_LEN - written, format, arglist); clean_str(line_log); if (would_print >= MAX_LINE_LEN - written) fprintf(stderr, "%s... <Line truncated. Original size=%u>\n", line_log, would_print); else fprintf(stderr, "%s\n", line_log); } else if (p_log->log_type == RBH_LOG_SYSLOG) { /* add tag to syslog line */ char new_format[MAX_LINE_LEN]; if (tag) snprintf(new_format, MAX_LINE_LEN, "%s | %s", tag, format); else rh_strncpy(new_format, format, MAX_LINE_LEN); vsyslog(log_config.syslog_priority, new_format, arglist); } else { /* log to a file */ localtime_r(&now, &date); written = snprintf(line_log, MAX_LINE_LEN, "%.4d/%.2d/%.2d %.2d:%.2d:%.2d %s%s%s[%lu/%u] %s%s", 1900 + date.tm_year, date.tm_mon + 1, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec, log_config.log_process ? prog_name : "", log_config.log_host ? "@" : "", log_config.log_host ? machine_name : "", (unsigned long)getpid(), th, tag ? tag : "", tag ? " | " : ""); would_print = vsnprintf(line_log + written, MAX_LINE_LEN - written, format, arglist); clean_str(line_log); if (p_log->f_log != NULL) { if (would_print >= MAX_LINE_LEN - written) fprintf(p_log->f_log, "%s... <Line truncated. Original size=%u>\n", line_log, would_print); else fprintf(p_log->f_log, "%s\n", line_log); } } pthread_rwlock_unlock(&p_log->f_lock); }
bool Cxml::get_node(char* xml_string) { int k = m_cursor; int j = 0; //second level cursor; bool bCDATA = false; bool bIsPI = false; //Set to true if the cursor is curently inside a processing instruction char cDelim = 0; char c = xml_string[k]; const char COPEN = '<'; const char CCLOSE = '>'; const char CSLASH = '/'; const char CSPACE = ' '; const char CQUOTE = '\''; const char CDQUOTE = '\"'; const char CEQUAL = '='; const char CNEW = '\n'; const char CTAB = '\t'; const char CEXCLAMATION = '!'; const char CMINUS = '-'; const char CSQRPO = '['; const char CSQRPC = ']'; const char SZCDATA[9] = "![CDATA["; const char CQM = '?'; const char CRET = 13; // carriage return char *szNodeNameBuff = (char *)calloc(256,sizeof(char)); char *szNodeValBuff = (char *)calloc(256,sizeof(char)); char *szAttrNameBuff = (char *)calloc(256,sizeof(char)); char *szAttrValBuff = (char *)calloc(256,sizeof(char)); if(k >= m_length) return false; m_root_node->set_name("XML_DOC"); m_root_node->set_value(""); // just in case we want to access it later... element* Current = m_root_node->add_child_element(); while(k<m_length) { c = xml_string[k]; if(c == CNEW || c == CTAB || c == CRET) { k++; continue; } if(c == COPEN) { if(xml_string[k+1] == CEXCLAMATION && xml_string[k+2] == CMINUS && xml_string[k+3] == CMINUS) // this is a comment { //the comment section clean_str(szAttrValBuff); k+=4; c = xml_string[k]; while(!(xml_string[k] == CMINUS && xml_string[k+1] == CMINUS && xml_string[k+2] == CCLOSE)) { szAttrNameBuff = concat(szAttrNameBuff, c); c = xml_string[++k]; } k+=3; if(Current->get_name() != NULL) //we have set this node, navigate to a child of it Current = Current->add_child_element(); Current->set_comment(szAttrNameBuff); //set it as a comment node Current = Current->get_parent(); //return to the previous level continue; } while(k+10 < m_length && j < 9) { if(xml_string[k+1+j] != SZCDATA[j]) break; j++; if(j==8) { // definetly a CDATA section k = k + j; int start = k; while((k + 3) < m_length && xml_string[k+1] != CSQRPC && xml_string[k+2] != CSQRPC && xml_string[k+3] != CCLOSE) { k++; } int stop = k; k+=5; char* buffer = (char*)calloc(stop - start + 2, sizeof(char)); copyx(buffer, xml_string, start, stop); Current->set_value(buffer); free(buffer); j = 0; bCDATA = true; break; } } if(bCDATA) { bCDATA = false; continue; } clean_str(szNodeNameBuff); if(xml_string[k+1] == CSLASH) { // closing tag for the last opened node Current = Current->get_parent(); k++; while(xml_string[k] != CCLOSE) { k++; } k++; continue; } if(xml_string[k+1] == CQM) { c = xml_string[++k]; bIsPI = true; } // open tag. It means we have a node so we create it c = xml_string[++k]; while(c != CSLASH && c != CSPACE && c != CCLOSE) {//loops until the node name has been entirely read if(c != CNEW && c != CTAB && c != CRET) szNodeNameBuff = concat(szNodeNameBuff,c); c = xml_string[++k]; } if(Current != NULL) // this node is set, navigate to a child of it if(Current->get_name() != NULL) // this node is set, navigate to a child of it Current = Current->add_child_element(); Current->set_name(szNodeNameBuff); // If there's a space here, there must be an attribute coming. while(c == CSPACE) { c = xml_string[++k]; if(c == CSLASH) { break; } if(c == CQM && bIsPI) { break; } clean_str(szAttrNameBuff); clean_str(szAttrValBuff); // Get ready for a new attribute. attribute* pA = new attribute(); // Accumulate characters until there is an equal sign, when // we'll know the attribute name has been read. while(c != CEQUAL) { if (c != CNEW && c != CTAB && c != CRET) szAttrNameBuff = concat(szAttrNameBuff, c); c = xml_string[++k]; } // Is the attribute value in single or double quotes? c = xml_string[++k]; if (c == CQUOTE || c == CDQUOTE) { cDelim = c; c = xml_string[++k]; } // Accumulate characters until the next delimiter, when // we'll know the attribute value has been read. while(c != cDelim && cDelim != 0) { if(c != CNEW && c != CTAB && c != CRET) szAttrValBuff = concat(szAttrValBuff, c); c = xml_string[++k]; } // Reset the delimiter indicator, and advance one character. cDelim = 0; c = xml_string[++k]; // Set the name and value of our new attribute. pA->set_name(szAttrNameBuff); pA->set_value(szAttrValBuff); Current->add_attribute(pA); } // Repeat if the next character is a space. if(c == CSLASH) { Current = Current->get_parent(); c=xml_string[++k]; while(c != CCLOSE) { c = xml_string[++k]; } } if(c == CQM && bIsPI) { Current->set_as_pi(); Current = Current->get_parent(); c=xml_string[++k]; bIsPI = false; while(c != CCLOSE) { c = xml_string[++k]; } } if(c == CCLOSE) { ; } } if(c != COPEN && c != CCLOSE && c != CSLASH/* && c != CSPACE*/) { clean_str(szNodeValBuff); while(c != COPEN) { if(c != CNEW && c != CTAB && c != CRET/* && c != CSPACE*/) szNodeValBuff =concat(szNodeValBuff,c); c = xml_string[++k]; } Current->set_value(szNodeValBuff); continue; } k++; } free(szNodeNameBuff); free(szNodeValBuff); free(szAttrNameBuff); free(szAttrValBuff); return true; }