Exemple #1
0
AjBool ajReadlinePos(AjPFile file, AjPStr* Pdest, ajlong* Ppos)
{
    const char *cp;
    char *buff;
    ajint isize;
    ajint ilen;
    ajint jlen;
    ajint ipos;
    ajuint buffsize;
    size_t iread;
    const char* pnewline = NULL;
 
    MAJSTRDEL(Pdest);

    if(file->Buffsize)
        buffsize = file->Buffsize;
    else
        buffsize = ajFileValueBuffsize();

    if(!file->Buff)
      ajStrAssignResC(&file->Buff, buffsize, "");
    else if(buffsize > MAJSTRGETRES(file->Buff))
      ajStrSetRes(&file->Buff, buffsize);

    if(MAJSTRGETUSE(file->Buff) == 1)
      buff = MAJSTRGETPTR(file->Buff);
    else
      buff  = ajStrGetuniquePtr(&file->Buff);

    isize = MAJSTRGETRES(file->Buff);
    ilen  = 0;
    ipos  = 0;
    
    if(!file->fp)
	ajWarn("ajFileGets file not found");
    
    *Ppos = file->Filepos;

    while(buff)
    {
	if(file->End)
	{
	    ajStrAssignClear(Pdest);
	    ajDebug("at EOF: File already read to end %F\n", file);

	    return ajFalse;
	}
	

#ifndef __ppc__
        if(file->Readblock)
        {
            if(file->Blockpos >= file->Blocklen)
            {
                iread = fread(file->Readblock,
                              1, file->Blocksize,
                              file->fp);

                if(!iread && ferror(file->fp))
                    ajFatal("fread failed with error:%d '%s'",
                            ferror(file->fp), strerror(ferror(file->fp)));

                file->Blockpos = 0;
                file->Blocklen = iread;
                file->Readblock[iread] = '\0';
                /*ajDebug("++ fread %u Ppos:%Ld\n", iread, *Ppos);*/
             }

            if(file->Blockpos < file->Blocklen)
            {

                /* we know we have something in Readblock to process */

                pnewline = strchr(&file->Readblock[file->Blockpos], '\n');

                if(pnewline)
                    jlen = pnewline - &file->Readblock[file->Blockpos] + 1;
                else
                    jlen = file->Blocklen - file->Blockpos;

                /*ajDebug("ipos:%d jlen:%d pnewline:%p "
                          "Readblock:%p blockpos:%d blocklen:%d\n",
                          ipos, jlen, pnewline, file->Readblock,
                          file->Blockpos, file->Blocklen);*/
                memmove(&buff[ipos], &file->Readblock[file->Blockpos], jlen);
                buff[ipos+jlen]='\0';
                cp = &buff[ipos];
                file->Blockpos += jlen;
            }
            else
            {
                jlen = 0;
                cp = NULL;
            }
        }
        else
        {
            cp = fgets(&buff[ipos], isize, file->fp);
            jlen = strlen(&buff[ipos]);
        }
        
#else
	cp = ajSysFuncFgets(&buff[ipos], isize, file->fp);
	jlen = strlen(&buff[ipos]);
#endif

        if(!cp && !ipos)
	{
	    if(feof(file->fp))
	    {
		file->End = ajTrue;
		ajStrAssignClear(Pdest);
		ajDebug("EOF ajFileGetsL file %F\n", file);

		return ajFalse;
	    }
	    else
		ajFatal("Error reading from file '%S'\n", ajFileGetNameS(file));
	}

	ilen += jlen;
        file->Filepos += jlen;

	/*
	 ** We need to read again if:
	 ** We have read the entire buffer
	 ** and we don't have a newline at the end
	 ** (must be careful about that - we may just have read enough)
	 */

	if(((file->Readblock && !pnewline) ||(jlen == (isize-1))) &&
	   (buff[ilen-1] != '\n'))
	{
            MAJSTRSETVALIDLEN(&file->Buff, ilen); /* fix before resizing! */
	    ajStrSetResRound(&file->Buff, ilen+buffsize+1);
	    /*ajDebug("more to do: jlen: %d ipos: %d isize: %d ilen: %d "
		    "Size: %d\n",
		    jlen, ipos, isize, ilen, ajStrGetRes(file->Buff));*/
	    ipos += jlen;
	    buff = ajStrGetuniquePtr(&file->Buff);
	    isize = ajStrGetRes(file->Buff) - ipos;
	    /*ajDebug("expand to: ipos: %d isize: %d Size: %d\n",
              ipos, isize, ajStrGetRes(file>Buff));*/
	}
	else
	    buff = NULL;
    }
    
    MAJSTRSETVALIDLEN(&file->Buff, ilen);
    if (ajStrGetCharLast(file->Buff) != '\n')
    {
	/*ajDebug("Appending missing newline to '%S'\n", file->Buff);*/
	ajStrAppendK(&file->Buff, '\n');
    }
    ajStrAssignRef(Pdest, file->Buff);

/*
  if(file->Readblock)
        ajDebug("ajFileGetsL done blocklen:%d blockpos:%d readlen:%u\n",
                file->Blocklen, file->Blockpos, ajStrGetLen(file->Buff));
*/
    return ajTrue;
}
Exemple #2
0
int main(int argc, char **argv)
{
    void *value = NULL;

    ajulong length = 0;
    ajulong i = 0;
    ajuint j = 0;

    AjEnum client = ajESqlClientMySQL;

    AjPFile outf = NULL;

    AjPStr user = NULL;
    AjPStr password = NULL;
    AjPStr host = NULL;
    AjPStr port = NULL;
    AjPStr socket = NULL;
    AjPStr dbname = NULL;
    AjPStr statement = NULL;
    AjPStr result = NULL;

    AjPSqlconnection sqlc = NULL;
    AjPSqlstatement sqls = NULL;
    AjISqlrow sqli = NULL;
    AjPSqlrow row = NULL;

    embInit("sqltest", argc, argv);

    user = ajStrNewC("anonymous");
    password = ajStrNew();
    host = ajStrNewC("ensembldb.ensembl.org");
    port = ajStrNewC("3306");
    socket = ajStrNew();
    dbname = ajStrNew();
    statement = ajStrNewC("SHOW PROCESSLIST");
    result = ajStrNew();
    outf = ajAcdGetOutfile("outfile");

    if(!ajSqlInit())
        ajFatal("Library initialisation failed.");

    sqlc =
        ajSqlconnectionNewData(client, user, password, host, port, socket,
                               dbname);

    if(!sqlc)
        ajFatal("Could not connect as user '%S' to server '%S' at port '%S' "
                "to database '%S'", user, host, port, dbname);

    ajFmtPrintF(outf, "Connection to client %d\n",
                ajSqlconnectionGetClient(sqlc));

    sqls = ajSqlstatementNewRun(sqlc, statement);

    if(!sqls)
        ajFatal("SQL Statement did not complete: %S", statement);

    ajFmtPrintF(outf,
                "SQL Statement: %S\n",
                statement);

    ajFmtPrintF(outf,
                "Number of selected rows: %Lu\n",
                ajSqlstatementGetSelectedrows(sqls));

    ajFmtPrintF(outf,
                "Number of columns: %u\n",
                ajSqlstatementGetColumns(sqls));

    sqli = ajSqlrowiterNew(sqls);

    if(!sqli)
        ajFatal("Query Iterator not defined.");

    while(!ajSqlrowiterDone(sqli))
    {
        row = ajSqlrowiterGet(sqli);

        if(row)
        {
            ajDebug("main SQL Row %Lu Columns %u Current %u\n",
                    i, row->Columns, row->Current);

            for(j = 0; j < ajSqlstatementGetColumns(sqls); j++)
            {
                ajStrAssignClear(&result);

                if(ajSqlcolumnNumberGetValue(row, j, &value, &length))
                    ajDebug("main SQL Row %Lu Column %u Value %p Length %d\n",
                            i, j, value, length);

                ajSqlcolumnNumberToStr(row, j, &result);

                ajDebug("main SQL Row %Lu Column %u String '%S'\n",
                        i, j, result);

                ajFmtPrintF(outf, "Row: %Lu Column: %u String: '%S'\n", i, j,
                            result);
            }
        }

        else
            ajDebug("main No row returned!\n");

        i++;
    }

    ajSqlrowiterDel(&sqli);
    ajSqlstatementDel(&sqls);
    ajSqlconnectionDel(&sqlc);

    ajFileClose(&outf);

    ajStrDel(&user);
    ajStrDel(&password);
    ajStrDel(&host);
    ajStrDel(&port);
    ajStrDel(&socket);
    ajStrDel(&dbname);
    ajStrDel(&statement);
    ajStrDel(&result);

    embExit();

    return 0;
}