void SpellHighlighter::flush()
{
    if (m_curWord.isEmpty())
        return;
    string ss;
    if (!m_curWord.isEmpty())
        ss = static_cast<string>(m_curWord.toLocal8Bit());
    log(L_DEBUG, ">> %s [%u %u %u]", ss.c_str(), m_index, m_curStart, m_pos);

    if ((m_index >= m_curStart) && (m_index <= m_pos)){
        if (m_bCheck){
            m_word       = m_curWord;
            m_bInError   = m_bError;
            m_start_word = m_curStart;
            m_curWord    = "";
            return;
        }
        if (m_bError){
            if (m_bDisable) {
                setFormat(m_curStart, m_pos - m_curStart, static_cast<TextEdit*>(textEdit())->defForeground());
            }else if (m_parag == m_paragraph){
                MAP_BOOL::iterator it = m_words.find(my_string(m_curWord.toUtf8()));
                if ((it == m_words.end()) || (*it).second)
                    setFormat(m_curStart, m_pos - m_curStart, static_cast<TextEdit*>(textEdit())->defForeground());
            }
        }
        m_curWord = "";
        return;
    }
    if (m_bCheck){
        m_curWord = "";
        return;
    }
    if (m_bDisable){
        if (m_bError)
            setFormat(m_curStart, m_pos - m_curStart, static_cast<TextEdit*>(textEdit())->defForeground());
        m_curWord = "";
        return;
    }
    MAP_BOOL::iterator it = m_words.find(my_string(m_curWord.toUtf8()));
    if (it != m_words.end()){
        if (!(*it).second){
            if (!m_bError)
                setFormat(m_curStart, m_pos - m_curStart, QColor(ErrorColor));
        }else if (m_bError){
            setFormat(m_curStart, m_pos - m_curStart, static_cast<TextEdit*>(textEdit())->defForeground());
        }
    }else{
        m_words.insert(MAP_BOOL::value_type(my_string(m_curWord.toUtf8()), true));
        if (m_plugin->m_ignore.find(my_string(m_curWord.toUtf8())) == m_plugin->m_ignore.end())
            emit check(m_curWord);
    }
    m_curWord = "";
}
示例#2
0
void SpellHighlighter::text(const QString &text)
{
    int i;
    for (i = 0; i < (int)(text.length());){
        for (; i < (int)(text.length()); i++, m_pos++){
            if (!text[i].isSpace() && !text[i].isPunct())
                break;
        }
        if (i >= (int)(text.length()))
            break;
        int start = m_pos;
        QString word;
        for (; i < (int)(text.length()); i++, m_pos++){
            if (text[i].isSpace() || text[i].isPunct())
                break;
            word += text[i];
        }
        if ((m_index >= start) && (m_index <= m_pos)){
            if (m_bCheck){
                m_word       = word;
                m_bInError   = m_bError;
                m_start_word = start;
            }else if (m_bError){
                if (m_bDisable) {
                    setFormat(start, m_pos - start, static_cast<TextEdit*>(textEdit())->defForeground());
                }else if (m_parag == m_paragraph){
					MAP_BOOL::iterator it = m_words.find(my_string(word.utf8()));
                    if ((it == m_words.end()) || (*it).second)
                        setFormat(start, m_pos - start, static_cast<TextEdit*>(textEdit())->defForeground());
                }
            }
        }else if (!m_bCheck){
            if (m_bDisable){
                if (m_bError)
                    setFormat(start, m_pos - start, static_cast<TextEdit*>(textEdit())->defForeground());
            }else{
				MAP_BOOL::iterator it = m_words.find(my_string(word.utf8()));
                if (it != m_words.end()){
                    if (!(*it).second){
                        setFormat(start, m_pos - start, QColor(ErrorColor));
                    }else if (m_bError){
                        setFormat(start, m_pos - start, static_cast<TextEdit*>(textEdit())->defForeground());
                    }
                }else{
                    m_words.insert(MAP_BOOL::value_type(my_string(word.utf8()), true));
                    if (m_plugin->m_ignore.find(my_string(word.utf8())) == m_plugin->m_ignore.end())
                        emit check(word);
                }
            }
        }
    }
}
示例#3
0
void SpellHighlighter::slotMisspelling(const QString &word)
{
	MAP_BOOL::iterator it = m_words.find(my_string(word.utf8()));
    if (it == m_words.end()){
		m_words.insert(MAP_BOOL::value_type(my_string(word.utf8()), false));
	}else{
		if (!(*it).second)
			return;
		(*it).second = false;
	}
    m_bDirty = true;
    QTimer::singleShot(300, this, SLOT(reformat()));
}
示例#4
0
my_string &my_string::operator=(my_string const &str)
{
    if (this != &str) {
        my_string(str).swap(*this);
    }
    return *this;
}
示例#5
0
void reverse(char* line, int len, int argc, char* argv[])
{
    std::vector<int> inds;
    int pos = 0;
    char* ind;
    while (pos < len)
    {
       ind = strstr(line + pos, delim2);
       if (ind)
       {
           pos = ind - line + strlen(delim2);
           inds.push_back(ind - line);
       } else
       {
           break;
       }
    }

    std::vector<my_string> fields;

    fields.push_back(my_string(buffer, inds.empty() ? len : inds[0]));
    for (int i = 0; i < inds.size(); i++)
    {
        int next_end = (i == inds.size() - 1 ? len : inds[i + 1]);
        int local_len = next_end - inds[i] - strlen(delim2);
        fields.push_back(my_string(buffer + inds[i] + strlen(delim2), local_len) );
    }

    char ** cmds;
    int len_cmd = argc - 3;
    len_cmd += fields.size();
    
    cmds = (char**)malloc((len_cmd + 1) * sizeof(char*) ); 
    cmds[len_cmd] = NULL;
    for (int i = 3; i < argc; i++)
    {
        cmds[i - 3] = argv[i];
    }

    int st = argc - 3;
    for (int i = 0; i < fields.size(); i++)
    {
        cmds[st + i] = fields[i].s;
    }
    int status = get_status(cmds);

    if (status)
    {
        write(1, buffer, inds.empty() ? len : inds[0]);
        for (int i = 0; i < inds.size(); i++)
        {
            int next_end = (i == inds.size() - 1 ? len : inds[i + 1]);
            int local_len = next_end - inds[i] - strlen(delim2);
            write(1, delim2, strlen(delim2));
            write(1, buffer + inds[i] + strlen(delim2), local_len);
        }

    } else
    {
        for (int i = inds.size() - 1; i >= 0; --i)
        {
            int next_end = (i == inds.size() - 1 ? len : inds[i + 1]);
            int local_len = next_end - inds[i] - strlen(delim2); 
            write(1, buffer + inds[i] + strlen(delim2), local_len);
            write(1, delim2, strlen(delim2) );
        }
        write(1, buffer, inds.empty() ? len : inds[0]);
   }

    free(cmds);
}
示例#6
0
void execute(command_t c)
{
    pid_t cpid;

	if (c->input || c->output) {
		if (DEBUG_EXPAND) {
			char* cmd = "";
			int i = 0;
			while (c->u.word[i])
			{
				cmd = my_string(cmd, " ");
				cmd = my_string(cmd, c->u.word[i]);
				i++;
			}

			if (c->input)
			{
				cmd = my_string(cmd, " < ");
				cmd = my_string(cmd, c->input);
			}
			else if(c->output)
			{
				cmd = my_string(cmd, " > ");
				cmd = my_string(cmd, c->output);
			}

			printf("+%s\n", cmd);
		}
	}

    if((cpid=fork()) == -1)
    {
        fprintf(stderr, "ERROR: Filed to create a new process.\n");
    }
    else if(cpid==0)//child
    {
        //open up file descriptors
        if(c->input != NULL){
            if (freopen(c->input, "r", stdin) == NULL){
                fprintf(stderr, "ERROR: File open failed.\n");
                exit(1);
            }
        }
        if(c->output != NULL){
            if(freopen(c->output, "w", stdout) == NULL){
                fprintf(stderr, "ERROR: File write failed.\n");
                exit(1);
            }
        }
        
		if (DEBUG_EXPAND) {
			char* cmd = "";
			int i = 0;
			while (c->u.word[i])
			{
				cmd = my_string(cmd, " ");
				cmd = my_string(cmd, c->u.word[i]);
				i++;
			}

			printf("+%s\n", cmd);
		}

        if(execvp(c->u.word[0], c->u.word) < 0){
            fprintf(stderr, "ERROR: Failed to execute the command.\n");
            exit(1);
        }
        fclose(stdin);
        fclose(stdout);
    }
    else//parent
    {
        int child_status;
        waitpid(cpid, &child_status, 0);
        c->status = WEXITSTATUS(child_status);
        return;
    }
}
示例#7
0
void *SpellHighlighter::processEvent(Event *e)
{
    if (e->type() == EventCheckState){
        CommandDef *cmd = (CommandDef*)(e->param());
        if (cmd->id == m_plugin->CmdSpell){
            MsgEdit *m_edit = (MsgEdit*)(cmd->param);
            if (m_edit->m_edit != textEdit())
                return NULL;
            m_index = textEdit()->charAt(static_cast<TextEdit*>(textEdit())->m_popupPos, &m_parag);
            m_pos = 0;
            m_bError   = false;
            m_bInError = false;
            while (!m_fonts.empty())
                m_fonts.pop();
            m_bCheck = true;
            parse(textEdit()->text(m_paragraph));
            m_bCheck = false;
            if (!m_bInError)
                return NULL;
            m_sug = m_plugin->suggestions(m_word);
            CommandDef *cmds = new CommandDef[m_sug.count() + 3];
            memset(cmds, 0, sizeof(CommandDef) * (m_sug.count() + 3));
            unsigned i = 0;
            for (QStringList::Iterator it = m_sug.begin(); it != m_sug.end(); ++it, i++){
                cmds[i].id   = m_plugin->CmdSpell + i + 2;
                cmds[i].text = "_";
                cmds[i].text_wrk = strdup((*it).utf8());
                if (i >= 10){
                    i++;
                    break;
                }
            }
            cmds[i].id   = m_plugin->CmdSpell;
            cmds[i].text = "_";
            cmds[i].text_wrk = strdup(i18n("Add '%1'") .arg(m_word) .utf8());
            i++;
            cmds[i].id   = m_plugin->CmdSpell + 1;
            cmds[i].text = "_";
            cmds[i].text_wrk = strdup(i18n("Ignore '%1'") .arg(m_word) .utf8());

            cmd->param  = cmds;
            cmd->flags |= COMMAND_RECURSIVE;
            return cmd;
        }
    }
    if (e->type() == EventCommandExec){
        CommandDef *cmd = (CommandDef*)(e->param());
        if (cmd->id == CmdSend){
            if (((MsgEdit*)(cmd->param))->m_edit == textEdit()){
                m_bDisable = true;
                rehighlight();
                QTimer::singleShot(50, this, SLOT(restore()));
            }
        }
        if ((cmd->id >= m_plugin->CmdSpell) && (cmd->id < m_plugin->CmdSpell + m_sug.count() + 1)){
            MsgEdit *m_edit = (MsgEdit*)(cmd->param);
            if (m_edit->m_edit != textEdit())
                return NULL;
            if (cmd->id == m_plugin->CmdSpell){
                m_plugin->add(m_word);
				MAP_BOOL::iterator it = m_words.find(my_string(m_word.utf8()));
                if (it == m_words.end()){
					m_words.insert(MAP_BOOL::value_type(my_string(m_word.utf8()), true));
				}else{
					if ((*it).second)
						return NULL;
					(*it).second = true;
				}
                m_bDirty = true;
                QTimer::singleShot(300, this, SLOT(reformat()));
            }else  if (cmd->id == m_plugin->CmdSpell + 1){
				MAP_BOOL::iterator it = m_plugin->m_ignore.find(my_string(m_word.utf8()));
                if (it == m_plugin->m_ignore.end())
                    m_plugin->m_ignore.insert(MAP_BOOL::value_type(my_string(m_word.utf8()), true));
                it = m_words.find(my_string(m_word.utf8()));
                if (it == m_words.end()){
					m_words.insert(MAP_BOOL::value_type(my_string(m_word.utf8()), true));
				}else{
					if ((*it).second)
						return NULL;
					(*it).second = true;
				}
                m_bDirty = true;
                QTimer::singleShot(300, this, SLOT(reformat()));
            }else{
                unsigned n = cmd->id - m_plugin->CmdSpell - 2;
                QString word = m_sug[n];
                textEdit()->setSelection(m_parag, m_start_word, m_parag, m_start_word + m_word.length());
                textEdit()->insert(word, true, true);
            }
        }
    }
    return NULL;
}
void GChannel::SendPacket(GeneralPacket& packet)
{
	if (!m_isOpen)
	{
		THROW_EXCEPTION1(ChannelException, "Channel not open!");
	}

	const util::NetworkOrder& network = util::NetworkOrder::Instance();

	packet.version = 3;  //we support version 3

	std::basic_ostringstream<boost::uint8_t> buffer;
	util::binary_write(buffer, (boost::uint8_t) packet.version, network);
	util::binary_write(buffer, (boost::uint8_t) packet.serviceType, network);

	if(packet.version == 3)
	{
		util::binary_write(buffer, (boost::uint32_t) packet.sessionID, network);
	}
	else
	{
		THROW_EXCEPTION1(ChannelException, "Invalid packet version (!= 3)!");
	}

	util::binary_write(buffer, packet.trackingID, network);
	util::binary_write(buffer, (boost::uint32_t) packet.dataSize, network);
	
	//CRC32C
	{
		std::basic_string<boost::uint8_t> my_string(buffer.str());
		packet.headerCRC = GenerateCRC32C(my_string.c_str(), my_string.size());
		packet.headerCRC = htonl(packet.headerCRC);
		buffer.write((boost::uint8_t*)&packet.headerCRC, sizeof(unsigned int));
	}

	if (packet.dataSize > 0)
		buffer.write(packet.data.c_str(), packet.dataSize);

	//
	std::basic_string<boost::uint8_t> string(buffer.str());
	try
	{
		m_socket->SendBytes((const char*) string.c_str(), string.size());

		if (LOG_DEBUG_ENABLED())
		{
			std::ostringstream dataBytes;
			dataBytes << std::hex << std::uppercase << std::setfill('0');
			for (int i = 0; i < (int) string.size(); i++)
			{
				dataBytes << std::setw(2) << (int) string.at(i) << ' ';
			}
			LOG_DEBUG("SendPacket:packet=" << packet.ToString() << ",  bytes=<" << dataBytes.str() << ">");
		}
	}
	catch (std::exception& ex)
	{
		LOG_ERROR("SendPacket: Failed to write on socket. error=" << ex.what());
		THROW_EXCEPTION1(ChannelException, "Failed to write on channel!");
	}
	catch (...)
	{
		LOG_ERROR("SendPacket: Failed to write on socket. unknown error!");
		THROW_EXCEPTION1(ChannelException, "Failed to write on channel!");
	}
}
示例#9
0
void getenc(char **fontname, char **encname, encoding ev, int width[256])
{  int i, len, SCMseen, Ccnt, wx, cc;
   FILE *afm;
   char *name, ns;
   
   for (i=0; i < 256; i++) { ev[i] = NULL; width[i] = -1000; }
   if (encfile) {
      enc = fopen(encfile, "r");
      if (enc == NULL) fatal("Can't open %s\n", encfile);
      if (nextsymbol() != '/') fatal("missing '/encoding' in %s\n", encfile);
      *encname = nextpsname();
      if (nextsymbol() != '[') fatal("missing '[' in %s\n", encfile);
      i = 0;
      while (i < 256 && (ns = nextsymbol()) == '/') {
         name = my_string(pline);
         if (strcmp(name, ".notdef") == 0) { 
            i++; remove_string();
            continue; 
         }
         addcc(name, i++);
      }
      if (i != 256) fatal("missing %d symbols in %s\n", 256-i, encfile);
      if (nextsymbol() != ']') fatal("missing ']' in %s\n", encfile);
      fclose(enc);
   }
   afm = fopen(afmfile, "r");
   if (afm == NULL) fatal("Can't open %s\n", afmfile);
   /*
    * Scan header of AFM file and take the FontName and EncodingScheme
    * (used for identification purposes in the PK postamble)
    * Stop after reading `StartCharMetrics'.
    */
   SCMseen = 0; Ccnt = 0;
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "FontName", 8) == 0) { 
	 pline = value_after(line, "FontName"); 
	 len = strlen(pline);
	 if (*(pline+len-1) == '\n') {
	    *(pline+len-1) = '\0'; len--;
	 }
	 *fontname = malloc(len + 1);
	 if (*fontname == NULL) fatal("Out of memory\n");
         strcpy(*fontname, pline);
      }
      else if (encname == NULL && strncmp(line, "EncodingScheme", 8) == 0) { 
	 pline = value_after(line, "EncodingScheme"); 
	 len = strlen(pline);
	 if (*(pline+len-1) == '\n') {
	    *(pline+len-1) = '\0'; len--;
	 }
	 *encname = malloc(len + 1);
	 if (*encname == NULL) fatal("Out of memory\n");
	 strcpy(*encname, pline);
      }
      else if (strncmp(line, "StartCharMetrics", 16) == 0) {
         SCMseen = 1; break;
      }
   }
   if (SCMseen == 0) fatal("%s: no metrics info\n", afmfile);
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "EndCharMetrics", 14) == 0) break;
      if (strncmp(line, "C ", 2) != 0) 
         fatal("%s: unexpected line\n", afmfile);

      /* Gracefully terminate when the AFM file is not valid.  Every line */
      /* in the AFM file should specify a "C"haracter, a "WX"idth, and    */
      /* a "N"ame. (ndw)
      */
      pline = value_after(line, "C"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no char):\n\t %s\n", afmfile, line);
      cc = decimal(pline);
      pline = value_after(pline, "WX"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no width):\n\t %s\n", afmfile, line);
      wx = decimal(pline);
      pline = value_after(pline, "N"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no name):\n\t %s\n", afmfile, line);
      name = my_string(pline);
      
      if (encfile) {
         if ((i = getcc(name)) == -1) {
            remove_string();
            continue;
	 }
         do { /* allow more occurences per name */
            ev[i] = name; width[i] = wx;
            i = getcc(name);
         }
         while (i >= 0);
      }
      else if (cc >= 0 && cc <= 255) { /* ndw: 10/13/92 */
	 ev[cc] = name; width[cc] = wx;
      }
      else if (cc > 255)
	 msg("Char code %d ignored (out of range).\n", cc);
      Ccnt++;
   }
   if (Ccnt == 0) fatal("%s: no characters selected\n", afmfile);   
}