Esempio n. 1
0
LytSeqInfo* AceParser::addSeq(char* s, LytCtgData* ctg) {
    LytSeqInfo* seq;
    if (!startsWith(s, "AF ", 3))
        return NULL;
    s+=3;
    char* p=strchrs(s," \t");
    if (p==NULL) return NULL;
    p++;
    char c;
    int offs;
    if (sscanf(p,"%c %d", &c, &offs)!=2) return NULL;
    p--;
    *p='\0';
    if ((seq=seqinfo.Find(s))!=NULL) {
        GMessage("Sequence '%s' already found for contig '%s (%d nt)'\n"
                 " so it cannot be added for contig '%s (%d)'\n",
                 s, seq->contig->name, seq->contig->len,
                 ctg->name, ctg->len);
        return NULL;
    }
    seq = new LytSeqInfo(s, ctg, offs, (c=='C') ? 1 : 0);
    seqinfo.shkAdd(seq->name, seq);
    ctg->seqs.Add(seq);
    return seq;
}
Esempio n. 2
0
char* LytCtgData::readName(char* s, GHash<int>& names) {
   char* p=strchrs(s, " \t");
   if (p!=NULL) {
       char* tmp;
       char* tmp2;
       GMALLOC(tmp, (p-s+30)*sizeof(char));
       strncpy(tmp, s,p-s);
       tmp[p-s]='\0';
       GMALLOC(tmp2, (p-s+30)*sizeof(char));
       strcpy(tmp2, tmp);
       //make it unique (by simple versioning)
       int v=0;
       while (names.hasKey(tmp2)) {
         v++;
         sprintf(tmp2, "%s.%d", tmp, v);
         }
       name=Gstrdup(tmp2);
       GFREE(tmp);
       GFREE(tmp2);
       names.shkAdd(name, new int(1));
       p++;
       }
      else {
       GMessage("LytCtgData::readName: Cannot find the token delimiter in:\n%s\n", s);
       }
     return p;
     }
Esempio n. 3
0
File: url.c Progetto: N3X15/hexchat
int
url_check_word (const char *word)
{
	laststart = lastend = lasttype = 0;
	if (do_an_re (word, &laststart, &lastend, &lasttype))
	{
		switch (lasttype)
		{
			char *str;

			case WORD_NICK:
				if (strchrs (word[laststart], NICKPRE))
					laststart++;
				str = g_strndup (&word[laststart], lastend - laststart);
				if (!userlist_find (current_sess, str))
					lasttype = 0;
				g_free (str);
				return lasttype;
			case WORD_EMAIL:
				if (!isalnum (word[laststart]))
					laststart++;
				/* Fall through */
			case WORD_URL:
			case WORD_HOST:
			case WORD_HOST6:
			case WORD_CHANNEL:
			case WORD_PATH:
				return lasttype;
			default:
				return 0;	/* Should not occur */
		}
	}
	else
		return 0;
}
Esempio n. 4
0
void
wrapText( const char* const s, const int x, const int y, const int w,
	const int h, const int fc, const int bc )
	{
	int nCharsPerLine = w/8;
	int yPos = y;
//	debug( "width: [%d]  nCharsPerLine: [%d]", w, nCharsPerLine );
//	debug( "s: [%s]", s );

	int nCharsToPrint = 0;
	char szLineToPrint[ 132 ];
	char* szLine = (char*)s;
	char* szFollow = szLine;
	char* space;

	for ( /* ALL THE TEXT */ ;; )
		{
		while ( *szLine == ' ' )
			++szLine;

		for ( /* EACH LINE */ ;; )
			{
			space = strchrs( szFollow, " ." );

			if ( space && *space == '.' )
				++space;

			if ( !space || ( space - szLine > nCharsPerLine ) )
				break;

			nCharsToPrint = space - szLine;

			szFollow = space+1;

			if ( *space == '' )
				break;
			}

		if ( !*szLine )
			break;

		assert( nCharsToPrint < sizeof( szLineToPrint ) );
		strncpy( szLineToPrint, szLine, nCharsToPrint );
		szLineToPrint[ nCharsToPrint ] = '\0';

//		puts( szLineToPrint );
		gfx_8x16_text( szLineToPrint, x, yPos, fc, bc );
		yPos += 16;

		szLine += nCharsToPrint;
		if ( *(szFollow-1) == '' )
			++szLine;
		}
	}
Esempio n. 5
0
/* This function returns a pointer to the first token of the given string,
 *  seperated by the delimiter. The function does not alter the string. */
char *strntok(char *string, char delimiter, int length)
{
    int i, ret, lastCharDelimiter = 0;

    for (i = 0; i < length; i++) {
        ret = strchrs(delimiter, string + i);
        /* If the current character is a delimiter, set the corresponding flag */
        if (ret)
            lastCharDelimiter = 1;
        /* If the current character isn't a delimiter, but the last was, return the pointer.*/
        if (!ret && lastCharDelimiter)
            return string + i;
    }
    return NULL;

}
int GFastaIndex::loadIndex(const char* finame) { //load record info from existing fasta index
    if (finame==NULL) finame=fai_name;
    if (finame!=fai_name) {
      fai_name=Gstrdup(finame);
      }
    if (fai_name==NULL) GError("Error: GFastaIndex::loadIndex() called with no file name!\n");
    records.Clear();
    haveFai=false;
    FILE* fi=fopen(fai_name,"rb");
    if (fi==NULL) {
       GMessage("Warning: cannot open fasta index file: %s!\n",fai_name);
       return 0;
       }
    GLineReader fl(fi);
    char* s=NULL;
    while ((s=fl.nextLine())!=NULL) {
      if (*s=='#') continue;
      char* p=strchrs(s,"\t ");
      if (p==NULL) GError(ERR_FAIDXLINE,s);
      *p=0; //s now holds the genomic sequence name
      p++;
      uint len=0;
      int line_len=0, line_blen=0;
      #ifdef __WIN32__
         long offset=-1;
         sscanf(p, "%d%ld%d%d", &len, &offset, &line_len, &line_blen);
      #else
         long long offset=-1;
         sscanf(p, "%d%lld%d%d", &len, &offset, &line_len, &line_blen);
      #endif
      if (len==0 || line_len==0 || line_blen==0 || line_blen<line_len)
          GError(ERR_FAIDXLINE,p);
      addRecord(s,len,offset,line_len, line_blen);
      }
    fclose(fi);
    haveFai=(records.Count()>0);
    return records.Count();
    }
Esempio n. 7
0
bool AceParser::loadContig(int ctgidx, fnLytSeq* seqfn, bool re_pos) {

    bool forgetCtg = false;
    if (ctgidx>=contigs.Count())
        GError("LayoutParser: invalid contig index '%d'\n", ctgidx);
    LytCtgData* ctgdata=contigs[ctgidx];
    if (re_pos && currentContig!=NULL) { //free previously loaded contig data
        currentContig->seqs.Clear();       // unless it was a parse() call
        seqinfo.Clear();
    }
    currentContig=ctgdata;
    int ctg_numSeqs=ctgdata->numseqs;

    if (re_pos) {
        seek(ctgdata->fpos); //position right where the contig definition starts
        char *r = linebuf->getLine(f,f_pos);
        if (r==NULL) return false;
    }

    if (seqfn!=NULL) { //process the contig sequence!
        char* ctgseq=readSeq();
        forgetCtg=(*seqfn)(numContigs, ctgdata, NULL, ctgseq);
        GFREE(ctgseq); //obviously the caller should have made a copy
    }
    //now look for all the component sequences
    if (fskipTo("AF ")<0) {
        GMessage("AceParser: error finding sequence offsets (AF)"
                 " for contig '%s' (%d)\n", ctgdata->name, ctgdata->len);
        return false;
    }
    int numseqs=0;
    while (startsWith(linebuf->chars(), "AF ",3)) {
        if (addSeq(linebuf->chars(), ctgdata)==NULL) {
            GMessage("AceParser: error parsing AF entry:\n%s\n",linebuf->chars());
            return false;
        }
        numseqs++;
        //read next line:
        linebuf->getLine(f,f_pos);
    }
    if (numseqs!=ctg_numSeqs) {
        GMessage("Invalid number of AF entries found (%d) for contig '%s' "
                 "(length %d, numseqs %d)\n", numseqs,
                 ctgdata->name, ctgdata->len, ctg_numSeqs);
        return false;
    }
    //now read each sequence entry
    off_t seqpos=fskipTo("RD ");
    numseqs=0; //count again, now the RD entries
    if (seqpos<0) {
        GMessage("AceParser: error locating first RD entry for contig '%s'\n",
                 ctgdata->name);
        return false;
    }
    //int numseqs=0;
    //reading the actual component sequence details
    while (startsWith(linebuf->chars(), "RD ",3)) {
        char* s=linebuf->chars()+3;
        char* p=strchrs(s, " \t");
        LytSeqInfo* seq;
        if (p==NULL) {
            GMessage("AceParser: Error parsing RD header line:\n%s\n", linebuf->chars());
            return false;
        }
        *p='\0';
        if ((seq=seqinfo.Find(s))==NULL) {
            GMessage("AceParser: unknown RD encountered: '%s'\n", s);
            return false;
        }
        p++; //now p is in linebuf after the RD name
        seq->fpos=seqpos;
        int len;
        if (sscanf(p, "%d", &len)!=1) {
            GMessage("AceParser: cannot parse RD length for '%s'\n", s);
            return false;
        }
        seq->setLength(len);
        //read the sequence data here if a callback fn was given:
        char* sseq=NULL;
        if (seqfn!=NULL)
            sseq=readSeq(seq); //read full sequence here
        if (fskipTo("QA ")<0) {
            GMessage("AceParser: Error finding QA entry for read %s! (fpos=%llu)\n", seq->name, (unsigned long long)f_pos);
            return false;
        }
        //parse QA entry:
        int tmpa, tmpb;
        if (sscanf(linebuf->chars()+3, "%d %d %d %d", &tmpa, &tmpb, &seq->left,&seq->right)!=4 ||
                seq->left<=0 || seq->right<=0) {
            GMessage("AceParser: Error parsing QA entry.\n");
            return false;
        }
        /*
        if (fskipTo("DS")<0) {
             GMessage("AceParser: Error closing RD entry ('DS' not found).\n");
             return false;
             }
             */
        seqpos=getFilePos()+1;
        bool forgetSeq=false;
        if (seqfn!=NULL) {
            forgetSeq=(*seqfn)(numContigs, ctgdata, seq, sseq);
            GFREE(sseq);
        }
        if (forgetSeq) { //parsing the whole stream -- aceconv)
            ctg_numSeqs--;
            seqinfo.Remove(seq->name);
            ctgdata->seqs.RemovePtr(seq);
        }
        numseqs++;
        if (numseqs<ctgdata->numseqs)
            seqpos=fskipTo("RD ", "CO "); //more sequences left to read
    }
    if (numseqs!=ctgdata->numseqs) {
        GMessage("Error: Invalid number of RD entries found (%d) for contig '%s' "
                 "(length %d, numseqs %d)\n", numseqs,
                 ctgdata->name, ctgdata->len, ctg_numSeqs);
        return false;
    }
    if (forgetCtg) {
        ctgIDs.Remove(ctgdata->name);
        ctgdata->seqs.Clear();
        seqinfo.Clear();
        contigs.RemovePtr(ctgdata);
    }
    return true;
}
Esempio n. 8
0
LytSeqInfo* LayoutParser::addSeq(char* s, LytCtgData* ctg) {
 LytSeqInfo* seq;
 //s must be the line with sequence data
 char* p=strchrs(s," \t");
 if (p==NULL) return NULL;
 p++;
 char c;
 int slen, soffs, clpL, clpR;
 clpL=0;clpR=0;
 if (sscanf(p,"%c %d %d %d %d", &c, &slen, &soffs, &clpL, &clpR)<3) return NULL;
 p--;
 *p='\0';
 if ((seq=seqinfo.Find(s))!=NULL) {
   GMessage("Sequence '%s' already found for contig '%s (%d nt)'\n"
      " so it cannot be added for contig '%s (%d nt)'\n",
     s, seq->contig->name, seq->contig->len,
     ctg->name, ctg->len);
   return NULL;
   }
 seq = new LytSeqInfo(s, ctg, soffs, (c=='-') ? 1 : 0, slen, clpL, clpR);
 seqinfo.shkAdd(seq->name, seq);
 ctg->seqs.Add(seq);
 //parse optional extensions, if any
 p+=strlen(s); //position p after the seqname
 char* m=NULL;
 int segEnd, segRclip,nextsegStart, nextsegLclip, prevSegStart;
 char segSplice, nextsegSplice;
 while ((m=strchr(p,':'))!=NULL) {
  switch (*(m-1)) {
    case 'G': //segmenting info
       prevSegStart=soffs+clpL-1;
       p=m+1;  //p to the beginning of G: data
       //accumulate the total length in lenSegs
       while (*p>='1' && *p<='9') {
         segEnd=0;
         segRclip=0;
         nextsegStart=0;
         nextsegLclip=0;
         segSplice=0;
         nextsegSplice=0;
         if (!parseInt(p,segEnd))
            GError("Error [segment] at LayoutParser for %s at: %s\n",
                 s, m-1);
         if (*p=='c') {
            p++;
            if (!parseInt(p,segRclip))
              GError("Error [segment] at LayoutParser for %s at: %s\n",
                      s, m-1);
            }
         if (*p=='S' || *p=='s') {
            segSplice=*p; p++;
            }
         if (*p!='-')
                GError("Error [segment] at LayoutParser for %s at: %s\n",
                      s, m-1);
            else p++;
         if (!parseInt(p,nextsegStart))
            GError("Error [segment] at LayoutParser for %s at: %s\n",
                 s, m-1);
         if (*p=='c') {
            p++;
            if (!parseInt(p,nextsegLclip))
              GError("Error [segment] at LayoutParser for %s at: %s\n",
                      s, m-1);
            }
         if (*p=='S' || *p=='s') {
            nextsegSplice=*p; p++;
            }
         seq->addInterSeg(segEnd,nextsegStart,segRclip,nextsegLclip,
                                         segSplice, nextsegSplice);
         prevSegStart=nextsegStart;
         //
         if (*p==',') p++;
             else break;
         } //while inter-segment parsing
       break; // 'G:' case
    case 'L': //clone mates list
       p=m+1; //p to the beginning of L: data
       break;
    case 'D': //difference sequence
       p=m+1; //p to the beginning of D: data
       break;
    case 'S': //actual sequence
       p=m+1; //p to the beginning of S: data
       break;
    default:
       p=m+1;//next attribute
    }
  }

 return seq;
}
Esempio n. 9
0
static void
add_clicked_cb (GtkWidget  *widget,
		DialogData *data)
{
	FrWindow   *window = data->window;
	char       *archive_name;
	char       *archive_dir;
	char       *archive_file;
	char       *tmp;
	const char *archive_ext;
	gboolean    do_not_add = FALSE;
	GError     *error = NULL;

	data->add_clicked = TRUE;

	/* Collect data */

	archive_name = g_uri_escape_string (gtk_entry_get_text (GTK_ENTRY (data->a_add_to_entry)), NULL, FALSE);

	/* Check whether the user entered a valid archive name. */

	if ((archive_name == NULL) || (*archive_name == '\0')) {
		GtkWidget *d;

		d = _gtk_error_dialog_new (GTK_WINDOW (window),
					   GTK_DIALOG_DESTROY_WITH_PARENT,
					   NULL,
					   _("Could not create the archive"),
					   "%s",
					   _("You have to specify an archive name."));
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));
		g_free (archive_name);

		return;
	}
	else if (strchrs (archive_name, BAD_CHARS)) {
		GtkWidget *d;
		char      *utf8_name = g_filename_display_name (archive_name);

		d = _gtk_error_dialog_new (GTK_WINDOW (window),
					   GTK_DIALOG_DESTROY_WITH_PARENT,
					   NULL,
					   _("Could not create the archive"),
					   _("The name \"%s\" is not valid because it cannot contain the characters: %s\n\n%s"),
					   utf8_name,
					   BAD_CHARS,
					   _("Please use a different name."));
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_free (utf8_name);
		g_free (archive_name);

		return;
	}

	/* Check directory existence. */

	archive_dir = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (data->a_location_filechooserbutton));
	if (archive_dir == NULL) {
		g_free (archive_dir);
		g_free (archive_name);
		return;
	}

	if (! check_permissions (archive_dir, R_OK|W_OK|X_OK)) {
		GtkWidget  *d;

		d = _gtk_error_dialog_new (GTK_WINDOW (window),
					   GTK_DIALOG_DESTROY_WITH_PARENT,
					   NULL,
					   _("Could not create the archive"),
					   "%s",
					   _("You don't have the right permissions to create an archive in the destination folder."));
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_free (archive_dir);
		g_free (archive_name);
		return;
	}

	if (! uri_is_dir (archive_dir)) {
		GtkWidget *d;
		int        r;
		char      *folder_name;
		char      *msg;

		folder_name = g_filename_display_name (archive_dir);
		msg = g_strdup_printf (_("Destination folder \"%s\" does not exist.\n\nDo you want to create it?"), folder_name);
		g_free (folder_name);

		d = _gtk_message_dialog_new (GTK_WINDOW (data->dialog),
					     GTK_DIALOG_MODAL,
					     GTK_STOCK_DIALOG_QUESTION,
					     msg,
					     NULL,
					     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					     _("Create _Folder"), GTK_RESPONSE_YES,
					     NULL);

		gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_YES);
		r = gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_free (msg);

		do_not_add = (r != GTK_RESPONSE_YES);
	}

	if (! do_not_add && ! ensure_dir_exists (archive_dir, 0755, &error)) {
		GtkWidget  *d;

		d = _gtk_error_dialog_new (GTK_WINDOW (window),
					   GTK_DIALOG_DESTROY_WITH_PARENT,
					   NULL,
					   _("Could not create the archive"),
					   _("Could not create the destination folder: %s."),
					   error->message);
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_error_free (error);
		g_free (archive_dir);
		g_free (archive_name);
		return;
	}

	if (do_not_add) {
		GtkWidget *d;

		d = _gtk_message_dialog_new (GTK_WINDOW (window),
					     GTK_DIALOG_DESTROY_WITH_PARENT,
					     GTK_STOCK_DIALOG_WARNING,
					     _("Archive not created"),
					     NULL,
					     GTK_STOCK_OK, GTK_RESPONSE_OK,
					     NULL);
		gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_OK);
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_free (archive_dir);
		g_free (archive_name);

		return;
	}

	/**/

	archive_ext = get_ext (data);
	tmp = archive_name;
	archive_name = g_strconcat (tmp, archive_ext, NULL);
	g_free (tmp);
	archive_file = g_strconcat (archive_dir, "/", archive_name, NULL);

	if (uri_is_dir (archive_file)) {
		GtkWidget  *d;

		d = _gtk_error_dialog_new (GTK_WINDOW (window),
					   GTK_DIALOG_DESTROY_WITH_PARENT,
					   NULL,
					   _("Could not create the archive"),
					   "%s",
					   _("You have to specify an archive name."));
		gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		g_free (archive_name);
		g_free (archive_dir);
		g_free (archive_file);

		return;
	}

	if (uri_exists (archive_file)) {
		GtkWidget *d;
		int        r;

		d = _gtk_message_dialog_new (GTK_WINDOW (data->dialog),
					     GTK_DIALOG_MODAL,
					     GTK_STOCK_DIALOG_QUESTION,
					     _("The archive is already present.  Do you want to overwrite it?"),
					     NULL,
					     GTK_STOCK_NO, GTK_RESPONSE_NO,
					     _("_Overwrite"), GTK_RESPONSE_YES,
					     NULL);

		gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_YES);
		r = gtk_dialog_run (GTK_DIALOG (d));
		gtk_widget_destroy (GTK_WIDGET (d));

		if (r == GTK_RESPONSE_YES) {
			GFile  *file;
			GError *err = NULL;

			/* FIXME: convert this code in a function in file-utils.c */
			file = g_file_new_for_uri (archive_file);
			g_file_delete (file, NULL, &err);
			if (err != NULL) {
				g_warning ("Failed to delete file %s: %s",
					   archive_file,
					   err->message);
				g_clear_error (&err);
			}
			g_object_unref (file);
		}
		else {
			g_free (archive_name);
			g_free (archive_dir);
			g_free (archive_file);
			return;
		}
	}
	set_archive_options (data);
	gtk_widget_destroy (data->dialog);

	fr_window_archive_new (window, archive_file);

	g_free (archive_name);
	g_free (archive_dir);
	g_free (archive_file);
}