example/thread-pool/psi.c
static int
strextract_ioreader(INKIOBufferReader reader, int offset, const char *end_pattern, char *buffer, int *buflen)
{
  int buf_idx = 0;
  int p_idx = 0;
  int nbytes_so_far = 0;
  int plen = strlen(end_pattern);
  const char *ptr;
  INKIOBufferBlock block = INKIOBufferReaderStart(reader);

  if (plen <= 0) {
    return STR_FAIL;
  }
  if (block == INK_ERROR_PTR) {
    INKError("[strextract_ioreader] Error while getting block from ioreader");
    return STR_FAIL;
  }

  /* Now start extraction */
  while ((block != NULL) && (p_idx < plen) && (buf_idx < PSI_FILENAME_MAX_SIZE)) {
    int blocklen;
    const char *blockptr = INKIOBufferBlockReadStart(block, reader, &blocklen);

    if (blockptr == INK_ERROR_PTR) {
      INKError("[strsearch_ioreader] Error while getting block pointer");
      break;
    }

    for (ptr = blockptr; ptr < blockptr + blocklen; ptr++, nbytes_so_far++) {
      if (nbytes_so_far >= offset) {

        /* Add a new character to the filename */
        buffer[buf_idx++] = *ptr;

        /* If we have reach the end of the filename, we're done */
        if (end_pattern[p_idx] == *ptr) {
          p_idx++;
          if (p_idx == plen) {
            break;
          }
        } else {
          p_idx = 0;
        }

        /* The filename is too long, something is fishy... let's abort extraction */
        if (buf_idx >= PSI_FILENAME_MAX_SIZE) {
          break;
        }
      }
    }

    block = INKIOBufferBlockNext(block);
    if (block == INK_ERROR_PTR) {
      INKError("[strextract_ioreader] Error while getting block from ioreader");
      return STR_FAIL;
    }
  }

  /* Error, could not read end of filename */
  if (buf_idx >= PSI_FILENAME_MAX_SIZE) {
    INKDebug(DBG_TAG, "strextract: filename too long");
    *buflen = 0;
    return STR_FAIL;
  }

  /* Full Match */
  if (p_idx == plen) {
    /* Nul terminate the filename, remove the end_pattern copied into the buffer */
    *buflen = buf_idx - plen;
    buffer[*buflen] = '\0';
    INKDebug(DBG_TAG, "strextract: filename = |%s|", buffer);
    return STR_SUCCESS;
  }
  /* End of filename not yet reached we need to read some more data */
  else {
    INKDebug(DBG_TAG, "strextract: partially extracted filename");
    *buflen = buf_idx - p_idx;
    return STR_PARTIAL;
  }
}
/*-------------------------------------------------------------------------
  strextract_ioreader
  Extract a string from an iobuffer.
  Start reading at position offset in iobuffer and extract until the
  string end_pattern is found.

  Input:
    reader      reader on a iobuffer
    offset      position to start reading
    end_pattern the termination string (nul terminated)
  Output:
    buffer      if success, contains the extracted string, nul terminated
    buflen      if success, contains the buffer length (excluding null char).
  Return Value:
    STR_SUCCESS if extraction successful
    STR_PARTIAL if extraction not yet completed
    STR_FAIL    if extraction failed
  -------------------------------------------------------------------------*/
strextract_ioreader
Extract a string from an iobuffer.
Start reading at position offset in iobuffer and extract until the
string end_pattern is found.

reader      reader on a iobuffer
offset      position to start reading
end_pattern the termination string (nul terminated)
Output:
buffer      if success, contains the extracted string, nul terminated
buflen      if success, contains the buffer length (excluding null char).
Return Value:
STR_SUCCESS if extraction successful
STR_PARTIAL if extraction not yet completed
STR_FAIL    if extraction failed
strextract_ioreader Extract a string from an iobuffer . Start reading at position offset in iobuffer and extract until the string end_pattern is found . reader reader on a iobuffer offset position to start reading end_pattern the termination string ( nul terminated ) Output : buffer if success contains the extracted string nul terminated buflen if success contains the buffer length ( excluding null char ) . Return Value : STR_SUCCESS if extraction successful STR_PARTIAL if extraction not yet completed STR_FAIL if extraction failed
static int
strextract_ioreader(INKIOBufferReader reader, int offset, const char *end_pattern, char *buffer, int *buflen)
{
  int buf_idx = 0;
  int p_idx = 0;
  int nbytes_so_far = 0;
  int plen = strlen(end_pattern);
  const char *ptr;
  INKIOBufferBlock block = INKIOBufferReaderStart(reader);
  if (plen <= 0) {
    return STR_FAIL;
  }
  if (block == INK_ERROR_PTR) {
    INKError("[strextract_ioreader] Error while getting block from ioreader");
    return STR_FAIL;
  }
  while ((block != NULL) && (p_idx < plen) && (buf_idx < PSI_FILENAME_MAX_SIZE)) {
    int blocklen;
    const char *blockptr = INKIOBufferBlockReadStart(block, reader, &blocklen);
    if (blockptr == INK_ERROR_PTR) {
      INKError("[strsearch_ioreader] Error while getting block pointer");
      break;
    }
    for (ptr = blockptr; ptr < blockptr + blocklen; ptr++, nbytes_so_far++) {
      if (nbytes_so_far >= offset) {
        buffer[buf_idx++] = *ptr;
        if (end_pattern[p_idx] == *ptr) {
          p_idx++;
          if (p_idx == plen) {
            break;
          }
        } else {
          p_idx = 0;
        }
        if (buf_idx >= PSI_FILENAME_MAX_SIZE) {
          break;
        }
      }
    }
    block = INKIOBufferBlockNext(block);
    if (block == INK_ERROR_PTR) {
      INKError("[strextract_ioreader] Error while getting block from ioreader");
      return STR_FAIL;
    }
  }
  if (buf_idx >= PSI_FILENAME_MAX_SIZE) {
    INKDebug(DBG_TAG, "strextract: filename too long");
    *buflen = 0;
    return STR_FAIL;
  }
  if (p_idx == plen) {
    *buflen = buf_idx - plen;
    buffer[*buflen] = '\0';
    INKDebug(DBG_TAG, "strextract: filename = |%s|", buffer);
    return STR_SUCCESS;
  }
  else {
    INKDebug(DBG_TAG, "strextract: partially extracted filename");
    *buflen = buf_idx - p_idx;
    return STR_PARTIAL;
  }
}
static int strextract_ioreader ( INKIOBufferReader reader , int offset , const char * end_pattern , char * buffer , int * buflen ) { int buf_idx = 0 ; int p_idx = 0 ; int nbytes_so_far = 0 ; int plen = strlen ( end_pattern ) ; const char * ptr ; INKIOBufferBlock block = INKIOBufferReaderStart ( reader ) ; if ( plen <= 0 ) { return STR_FAIL ; } if ( block == INK_ERROR_PTR ) { INKError ( " " ) ; return STR_FAIL ; } while ( ( block != NULL ) && ( p_idx < plen ) && ( buf_idx < PSI_FILENAME_MAX_SIZE ) ) { int blocklen ; const char * blockptr = INKIOBufferBlockReadStart ( block , reader , & blocklen ) ; if ( blockptr == INK_ERROR_PTR ) { INKError ( " " ) ; break ; } for ( ptr = blockptr ; ptr < blockptr + blocklen ; ptr ++ , nbytes_so_far ++ ) { if ( nbytes_so_far >= offset ) { buffer [ buf_idx ++ ] = * ptr ; if ( end_pattern [ p_idx ] == * ptr ) { p_idx ++ ; if ( p_idx == plen ) { break ; } } else { p_idx = 0 ; } if ( buf_idx >= PSI_FILENAME_MAX_SIZE ) { break ; } } } block = INKIOBufferBlockNext ( block ) ; if ( block == INK_ERROR_PTR ) { INKError ( " " ) ; return STR_FAIL ; } } if ( buf_idx >= PSI_FILENAME_MAX_SIZE ) { INKDebug ( DBG_TAG , " " ) ; * buflen = 0 ; return STR_FAIL ; } if ( p_idx == plen ) { * buflen = buf_idx - plen ; buffer [ * buflen ] = ' \0 ' ; INKDebug ( DBG_TAG , " " , buffer ) ; return STR_SUCCESS ; } else { INKDebug ( DBG_TAG , " " ) ; * buflen = buf_idx - p_idx ; return STR_PARTIAL ; } }
strextract_ioreader
Extract a string from an iobuffer.
strextract_ioreader Extract a string from an iobuffer .
/* Now start extraction */ /* Add a new character to the filename */ /* If we have reach the end of the filename, we're done */ /* The filename is too long, something is fishy... let's abort extraction */ /* Error, could not read end of filename */ /* Full Match */ /* Nul terminate the filename, remove the end_pattern copied into the buffer */ /* End of filename not yet reached we need to read some more data */