コード例 #1
0
ファイル: os_regex_compile.c プロジェクト: ospatrol/ospatrol
/** int OSRegex_Compile(char *pattern, OSRegex *reg, int flags) v0.1
 * Compile a regular expression to be used later.
 * Allowed flags are:
 *      - OS_CASE_SENSITIVE
 *      - OS_RETURN_SUBSTRING
 * Returns 1 on success or 0 on error.
 * The error code is set on reg->error.
 */
int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
{
    int i = 0;
    int count = 0;
    int end_of_string = 0;
    int parenthesis = 0;
    int prts_size = 0;
    int max_prts_size = 0;

    char *pt;
    char *new_str;
    char *new_str_free = NULL;


    /* Checking for references not initialized */
    if(reg == NULL)
    {
        return(0);
    }


    /* Initializing OSRegex structure */
    reg->error = 0;
    reg->patterns = NULL;
    reg->flags = NULL;
    reg->prts_closure = NULL;
    reg->prts_str = NULL;
    reg->sub_strings = NULL;



    /* The pattern can't be null */
    if(pattern == NULL)
    {
        reg->error = OS_REGEX_PATTERN_NULL;
        goto compile_error;
    }

    /* Maximum size of the pattern */
    if(strlen(pattern) > OS_PATTERN_MAXSIZE)
    {
        reg->error = OS_REGEX_MAXSIZE;
        goto compile_error;
    }


    /* Duping the pattern for our internal work */
    new_str = strdup(pattern);
    if(!new_str)
    {
        reg->error = OS_REGEX_OUTOFMEMORY;
        goto compile_error;
    }
    new_str_free = new_str;
    pt = new_str;


    /* Getting the number of sub patterns */
    do
    {
        if(*pt == BACKSLASH)
        {
            pt++;
            if(!((*pt == 'w') ||
                 (*pt == 'W') ||
                 (*pt == 's') ||
                 (*pt == 'S') ||
                 (*pt == 'd') ||
                 (*pt == 'D') ||
                 (*pt == '.') ||
                 (*pt == '(') ||
                 (*pt == ')') ||
                 (*pt == 'p') ||
                 (*pt == 't') ||
                 (*pt == '$') ||
                 (*pt == '|') ||
                 (*pt == '<') ||
                 (*pt == '\\')))
            {
                reg->error = OS_REGEX_BADREGEX;
                goto compile_error;
            }

            /* Giving the new values for each regex */
            switch(*pt)
            {
                case 'd': *pt = 1;break;
                case 'w': *pt = 2;break;
                case 's': *pt = 3;break;
                case 'p': *pt = 4;break;
                case '(': *pt = 5;break;
                case ')': *pt = 6;break;
                case '\\':*pt = 7;break;
                case 'D': *pt = 8;break;
                case 'W': *pt = 9;break;
                case 'S': *pt = 10;break;
                case '.': *pt = 11;break;
                case 't': *pt = 12;break;
                case '$': *pt = 13;break;
                case '|': *pt = 14;break;
                case '<': *pt = 15;break;
            }
            pt++;

            continue;
        }
        else if(*pt == '(')
        {
            parenthesis++;
        }
        else if(*pt == ')')
        {
            /* Internally, open and closed are the same */
            *pt = '(';
            parenthesis--;
            prts_size++;
        }

        /* We only allow one level of parenthesis */
        if(parenthesis != 0 && parenthesis != 1)
        {
            reg->error = OS_REGEX_BADPARENTHESIS;
            goto compile_error;
        }

        /* The pattern must be always lower case if
         * case sensitive is set
         */
        if(!(flags & OS_CASE_SENSITIVE))
        {
            *pt = charmap[(uchar)*pt];
        }

        if(*pt == OR)
        {
            /* Each sub pattern must be closed on parenthesis */
            if(parenthesis != 0)
            {
                reg->error = OS_REGEX_BADPARENTHESIS;
                goto compile_error;
            }
            count++;
        }
        pt++;
    }while(*pt != '\0');


    /* After the whole pattern is read, the parenthesis must all be closed */
    if(parenthesis != 0)
    {
        reg->error = OS_REGEX_BADPARENTHESIS;
        goto compile_error;
    }


    /* Allocating the memory for the sub patterns */
    count++;
    reg->patterns = calloc(count +1, sizeof(char *));
    reg->flags = calloc(count +1, sizeof(int));


    /* For the substrings */
    if((prts_size > 0) && (flags & OS_RETURN_SUBSTRING))
    {
        reg->prts_closure = calloc(count +1, sizeof(char **));
        reg->prts_str = calloc(count +1, sizeof(char **));
        if(!reg->prts_closure || !reg->prts_str)
        {
            reg->error = OS_REGEX_OUTOFMEMORY;
            goto compile_error;
        }
    }


    /* Memory allocation error check */
    if(!reg->patterns || !reg->flags)
    {
        reg->error = OS_REGEX_OUTOFMEMORY;
        goto compile_error;
    }

    /* Initializing each sub pattern */
    for(i = 0; i<=count; i++)
    {
        reg->patterns[i] = NULL;
        reg->flags[i] = 0;

        /* The parenthesis closure if set */
        if(reg->prts_closure)
        {
            reg->prts_closure[i] = NULL;
            reg->prts_str[i] = NULL;
        }
    }
    i = 0;


    /* Reassigning pt to the beginning of the string */
    pt = new_str;


    /* Getting the sub patterns */
    do
    {
        if((*pt == OR) || (*pt == '\0'))
        {
            if(*pt == '\0')
            {
                end_of_string = 1;
            }

            *pt = '\0';

            /* If string starts with ^, set the BEGIN SET flag */
            if(*new_str == BEGINREGEX)
            {
                new_str++;
                reg->flags[i]|=BEGIN_SET;
            }

            /* If string ends with $, set the END_SET flag */
            if(*(pt-1) == ENDREGEX)
            {
                *(pt-1) = '\0';
                reg->flags[i]|=END_SET;
            }

            reg->patterns[i] = strdup(new_str);

            if(!reg->patterns[i])
            {
                reg->error = OS_REGEX_OUTOFMEMORY;
                goto compile_error;

            }


            /* Setting the parenthesis closures */
            /* The parenthesis closure if set */
            if(reg->prts_closure)
            {
                int tmp_int = 0;
                char *tmp_str;


                /* search the whole pattern for parenthesis */
                prts_size = 0;

                /* First loop we get the number of parenthesis.
                 * We allocate the memory and loop again setting
                 * the parenthesis closures.
                 */
                tmp_str = reg->patterns[i];
                while(*tmp_str != '\0')
                {
                    if(prts(*tmp_str))
                    {
                        prts_size++;
                    }
                    tmp_str++;
                }

                /* Getting the maximum number of parenthesis for
                 * all sub strings. We need that to set up the maximum
                 * number of substrings to be returned.
                 */
                if(max_prts_size < prts_size)
                {
                    max_prts_size = prts_size;
                }

                /* Allocating the memory */
                reg->prts_closure[i] = calloc(prts_size + 1, sizeof(char *));
                reg->prts_str[i] = calloc(prts_size + 1, sizeof(char *));
                if((reg->prts_closure[i] == NULL)||(reg->prts_str[i] == NULL))
                {
                    reg->error = OS_REGEX_OUTOFMEMORY;
                    goto compile_error;
                }

                /* Next loop to set the closures */
                tmp_str = reg->patterns[i];
                while(*tmp_str != '\0')
                {
                    if(prts(*tmp_str))
                    {
                        if(tmp_int >= prts_size)
                        {
                            reg->error = OS_REGEX_BADPARENTHESIS;
                            goto compile_error;
                        }

                        /* Setting to the pointer to the string */
                        reg->prts_closure[i][tmp_int] = tmp_str;
                        reg->prts_str[i][tmp_int] = NULL;

                        tmp_int++;
                    }

                    tmp_str++;
                }
            }


            if(end_of_string)
            {
                break;
            }

            new_str = ++pt;
            i++;
            continue;
        }
        pt++;

    }while(!end_of_string);

    /* Allocating sub string for the maximum number of parenthesis */
    reg->sub_strings = calloc(max_prts_size + 1, sizeof(char *));
    if(reg->sub_strings == NULL)
    {
        reg->error = OS_REGEX_OUTOFMEMORY;
        goto compile_error;
    }

    /* Success return */
    free(new_str_free);
    return(1);


    /* Error handling */
    compile_error:

    if(new_str_free)
    {
        free(new_str_free);
    }

    OSRegex_FreePattern(reg);

    return(0);
}
コード例 #2
0
/** int _OS_Regex(char *pattern, char *str, char **prts_closure,
              char **prts_str, int flags) v0.1
 * Perform the pattern matching on the pattern/string provided.
 * Returns 1 on success and 0 on failure.
 * If prts_closure is set, the parenthesis locations will be
 * written on prts_str (which must not be NULL)
 */              
char *_OS_Regex(char *pattern, char *str, char **prts_closure, 
              char **prts_str, int flags)
{
    char *r_code = NULL;
    
    int ok_here;
    int _regex_matched = 0;
    
    int prts_int;

    char *st = str;
    char *st_error = NULL;
    
    char *pt = pattern;
    char *next_pt;

    char *pt_error[4] = {NULL, NULL, NULL, NULL};
    char *pt_error_str[4];
    

    /* Will loop the whole string, trying to find a match */
    do
    {
        switch(*pt)
        {
            case '\0':
                if(!(flags & END_SET) || (flags & END_SET && (*st == '\0')))
                    return(r_code);
                break;

                /* If it is a parenthesis do not match against the character */
            case '(':
                /* Find the closure for the parenthesis */
                if(prts_closure)
                {
                    prts_int = 0;
                    while(prts_closure[prts_int])
                    {
                        if(prts_closure[prts_int] == pt)
                        {
                            prts_str[prts_int] = st;
                            break;
                        }
                        prts_int++;
                    }
                }

                pt++;
                if(*pt == '\0')
                {
                    if(!(flags & END_SET) || (flags & END_SET && (*st == '\0')))
                        return(r_code);
                }
                break;
        }

        /* If it starts on Backslash (future regex) */
        if(*pt == BACKSLASH)
        {
            if(Regex((uchar)*(pt+1), (uchar)*st))
            {
                next_pt = pt+2;
                
                /* If we don't have a '+' or '*', we should skip
                 * searching using this pattern.
                 */
                if(!isPlus(*next_pt))
                {
                    pt = next_pt;
                    if(!st_error)
                    {
                        /* If st_error is not set, we need to set it here.
                         * In case of error in the matching later, we need
                         * to continue from here (it will be incremented in
                         * the while loop)
                         */
                        st_error = st;
                    }
                    r_code = st;
                    continue;
                }
                
                /* If it is a '*', we need to set the _regex_matched
                 * for the first pattern even.
                 */
                if(*next_pt == '*')
                {
                    _regex_matched = 1;
                }


                /* If our regex matches and we have a "+" set, we will
                 * try the next one to see if it matches. If yes, we 
                 * can jump to it, but saving our currently location
                 * in case of error.
                 * _regex_matched will set set to true after the first
                 * round of matches
                 */
                if(_regex_matched)
                {
                    next_pt++;
                    ok_here = -1;

                    /* If it is a parenthesis, jump to the next and write
                     * the location down if 'ok_here >= 0'
                     */
                    if(prts(*next_pt))
                    {
                        next_pt++;
                    }
                    
                    if(*next_pt == '\0')
                    {
                        ok_here = 1;
                    }
                    else if(*next_pt == BACKSLASH)
                    {
                        if(Regex((uchar)*(next_pt+1), (uchar)*st))
                        {
                            /* If the next one does not have
                             * a '+' or '*', we can set it as
                             * being read and continue.
                             */
                            if(!isPlus(*(next_pt+2)))
                            {
                                ok_here = 2;
                            }
                            else
                            {
                                ok_here = 0;
                            }
                        }
                    }
                    else if(*next_pt == charmap[(uchar)*st])
                    {
                        _regex_matched = 0;
                        ok_here = 1;
                    }

                    /* If the next character matches in here */
                    if(ok_here >= 0)
                    {
                        if(prts_closure && prts(*(next_pt - 1)))
                        {
                            prts_int = 0;
                            while(prts_closure[prts_int])
                            {
                                if(prts_closure[prts_int] == (next_pt -1))
                                {
                                    if(*(st+1) == '\0')
                                        prts_str[prts_int] = st+1;
                                    else    
                                        prts_str[prts_int] = st;
                                    break;
                                }
                                prts_int++;
                            }
                        }


                        /* If next_pt == \0, return the r_code */
                        if(*next_pt == '\0')
                        {
                            continue;
                        }

                            
                        /* Each "if" will increment the amount
                         * necessary for the next pattern in ok_here
                         */
                        if(ok_here) 
                            next_pt+=ok_here;
                        
                        
                        if(!pt_error[0])
                        {
                            pt_error[0] = pt;
                            pt_error_str[0] = st;
                        }
                        else if(!pt_error[1])
                        {
                            pt_error[1] = pt;
                            pt_error_str[1] = st;
                        }
                        else if(!pt_error[2])
                        {
                            pt_error[2] = pt;
                            pt_error_str[2] = st;

                        }
                        else if(!pt_error[3])
                        {
                            pt_error[3] = pt;
                            pt_error_str[3] = st;
                        }

                        pt = next_pt;
                    }
                }
                else
                {
                    next_pt++;

                    /* If it is a parenthesis, mark the location */
                    if(prts_closure && prts(*next_pt))
                    {
                        prts_int = 0;
                        while(prts_closure[prts_int])
                        {
                            if(prts_closure[prts_int] == next_pt)
                            {
                                if(*(st+1) == '\0')
                                    prts_str[prts_int] = st +1;
                                else
                                    prts_str[prts_int] = st;
                                break;
                            }
                            prts_int++;
                        }
                        next_pt++;
                    }

                    _regex_matched = 1;
                }
                
                r_code = st;
                continue;
            }
            
            else if((*(pt+3) == '\0') && (_regex_matched == 1)&&(r_code))
            {
                r_code = st;
                if(!(flags & END_SET) || (flags & END_SET && (*st == '\0')))
                    return(r_code);
            }
            
            /* If we didn't match regex, but _regex_matched == 1, jump
             * to the next available pattern
             */
            else if((*(pt+2) == '+') && (_regex_matched == 1))
            {
                pt+=3;
                st--;
                _regex_matched = 0;
                continue;
            }
            /* We may not match with '*' */
            else if(*(pt+2) == '*')
            {
                pt+=3;
                st--;
                r_code = st;
                _regex_matched = 0;
                continue;
            }

            _regex_matched = 0;
        }
        else if(*pt == charmap[(uchar)*st])
        {
            pt++;
            if(!st_error)
            {
                /* If st_error is not set, we need to set it here.
                 * In case of error in the matching later, we need
                 * to continue from here (it will be incremented in
                 * the while loop)
                 */
                st_error = st;
            }
            r_code = st;
            continue;
        }

        /* Error Handling */
            if(pt_error[3])
            {
                pt = pt_error[3];
                st = pt_error_str[3];
                pt_error[3] = NULL;
                continue;
            }
            else if(pt_error[2])
            {
                pt = pt_error[2];
                st = pt_error_str[2];
                pt_error[2] = NULL;
                continue;
            }
            else if(pt_error[1])
            {
                pt = pt_error[1];
                st = pt_error_str[1];
                pt_error[1] = NULL;
                continue;
            }
            else if(pt_error[0])
            {
                pt = pt_error[0];
                st = pt_error_str[0];
                pt_error[0] = NULL;
                continue;
            }
            else if(flags & BEGIN_SET)
            {
                /* If we get an error and the "^" option is
                 * set, we can return "not matched" in here.
                 */
                return(NULL);
            }
            else if(st_error)
            {
                st = st_error;
                st_error = NULL;
            }
            pt = pattern;
            r_code = NULL;
        
    }while(*(++st) != '\0');


    /* Matching for a possible last parenthesis */
    if(prts_closure)
    {
        while(!prts(*pt) && *pt != '\0')
        {
            if(*pt == BACKSLASH && *(pt+2) == '*')
                pt+=3;
            else
                break;    
        }
        
        if(prts(*pt))
        {
            prts_int = 0;
            while(prts_closure[prts_int])
            {
                if(prts_closure[prts_int] == pt)
                {
                    prts_str[prts_int] = st;
                    break;
                }
                prts_int++;
            }
        }
    }

    /* Cleaning up */
    if(ENDOFFILE(pt) || 
        (*pt == BACKSLASH && 
        _regex_matched && 
        (pt+=2) && 
        isPlus(*pt) && 
        (pt++) &&
        ((ENDOFFILE(pt)) || 
        ((*pt == BACKSLASH) && 
        (pt+=2) && 
        (*pt == '*') && 
        (pt++) && 
        (ENDOFFILE(pt)) ))) ||
        (*pt == BACKSLASH &&
        (pt+=2) &&
        (*pt == '*') &&
        (pt++) &&
        ENDOFFILE(pt))
        ) 
    {
        return(r_code);
    }
   
    return(NULL);
}
コード例 #3
0
void printPackingListBatchByShipvia::sPrint()
{
  QPrinter printer(QPrinter::HighResolution);
  bool     setupPrinter = true;

  if (!_dates->allValid())
  {
    QMessageBox::warning( this, tr("Enter a Valid Start and End Date"),
                          tr("You must enter a valid Start and End Date for this report.") );
    _dates->setFocus();
    return;
  }

  XSqlQuery prtd;
  QString prts("UPDATE pack SET pack_printed=true"
               " WHERE ((pack_head_id=<? value('head_id') ?>) "
	       "   AND  (pack_head_type=<? value('head_type') ?>)"
	       "<? if exists('shiphead_id') ?>"
	       "   AND  (pack_shiphead_id=<? value('shiphead_id') ?>)"
	       "<? else ?>"
	       "   AND (pack_shiphead_id IS NULL)"
	       "<? endif ?>"
	       ");" );


  XSqlQuery packq;
  ParameterList params;
  _dates->appendValue(params);
  _warehouse->appendValue(params);
  if (_metrics->boolean("MultiWhs"))
    params.append("MultiWhs");
  if (_shipvia->isValid())
    params.append("shipvia", _shipvia->currentText());
  MetaSQLQuery packm = mqlLoad("packingListBatchByShipVia", "print");
  packq = packm.toQuery(params);
  if (packq.lastError().type() != QSqlError::NoError)
  {
    systemError(this, packq.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  bool userCanceled = false;
  if (orReport::beginMultiPrint(&printer, userCanceled) == false)
  {
    if(!userCanceled)
      systemError(this, tr("Could not initialize printing system for multiple reports."));
    return;
  }
  while (packq.next())
  {
    ParameterList params;
    params.append("head_id",   packq.value("pack_head_id").toInt());
    params.append("head_type", packq.value("pack_head_type").toString());
    if (packq.value("pack_head_type").toString() == "SO")
      params.append("sohead_id", packq.value("pack_head_id").toInt());
    else if (packq.value("pack_head_type").toString() == "TO")
      params.append("tohead_id", packq.value("pack_head_id").toInt());
    if (! packq.value("pack_shiphead_id").isNull())
    {
      params.append("shiphead_id",  packq.value("pack_shiphead_id").toInt());
    }
    _warehouse->appendValue(params);
    if (_metrics->boolean("MultiWhs"))
      params.append("MultiWhs");

    if (packq.value("orderhead_status").toString() != "C")
    {
      bool usePickForm;
      if (_auto->isChecked())
        usePickForm = packq.value("pack_shiphead_id").isNull();
      else
        usePickForm = _pick->isChecked();
        
      orReport report(packq.value( usePickForm ? "pickform" : "packform").toString(), params);

      if (report.isValid())
      {
        if (report.print(&printer, setupPrinter))
        {
          setupPrinter = false;
          emit finishedPrinting(packq.value("pack_head_id").toInt(),
                                packq.value("pack_head_type").toString(),
                                packq.value("pack_shiphead_id").toInt());
        }
        else
        {
          orReport::endMultiPrint(&printer);
          return;
        }
      }
      else
      {
        report.reportError(this);
        orReport::endMultiPrint(&printer);
        return;
      }
    }

    MetaSQLQuery mql(prts);
    prtd = mql.toQuery(params);
    if (prtd.lastError().type() != QSqlError::NoError)
    {
      systemError(this, prtd.lastError().databaseText(), __FILE__, __LINE__);
      orReport::endMultiPrint(&printer);
      return;
    }

  }
  orReport::endMultiPrint(&printer);
  sPopulateShipVia();
}
コード例 #4
0
void printPackingListBatchByShipvia::sPrint()
{
  QPrinter printer(QPrinter::HighResolution);
  bool     setupPrinter = TRUE;

  XSqlQuery prtd;
  QString prts("UPDATE pack SET pack_printed=TRUE"
               " WHERE ((pack_head_id=<? value(\"head_id\") ?>) "
	       "   AND  (pack_head_type=<? value(\"head_type\") ?>)"
	       "<? if exists(\"shiphead_id\") ?>"
	       "   AND  (pack_shiphead_id=<? value(\"shiphead_id\") ?>)"
	       "<? else ?>"
	       "   AND (pack_shiphead_id IS NULL)"
	       "<? endif ?>"
	       ");" );


  XSqlQuery packq;
  ParameterList params;
  if (_metrics->boolean("MultiWhs"))
    params.append("MultiWhs");
  params.append("shipvia", _shipvia->currentText());
  MetaSQLQuery packm = mqlLoad(":/sr/forms/printPackingListBatchByShipvia/ToPrint.mql");
  packq = packm.toQuery(params);
  if (packq.lastError().type() != QSqlError::None)
  {
    systemError(this, packq.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  bool userCanceled = false;
  if (orReport::beginMultiPrint(&printer, userCanceled) == false)
  {
    if(!userCanceled)
      systemError(this, tr("Could not initialize printing system for multiple reports."));
    return;
  }
  while (packq.next())
  {
    // leave sohead_id and cosmisc_id for compatibility with existing reports
    ParameterList params;
    params.append("head_id",   packq.value("pack_head_id").toInt());
    params.append("head_type", packq.value("pack_head_type").toString());
    if (packq.value("pack_head_type").toString() == "SO")
      params.append("sohead_id", packq.value("pack_head_id").toInt());
    else if (packq.value("pack_head_type").toString() == "TO")
      params.append("tohead_id", packq.value("pack_head_id").toInt());
    if (! packq.value("pack_shiphead_id").isNull())
    {
      params.append("cosmisc_id", packq.value("pack_shiphead_id").toInt());
      params.append("shiphead_id",  packq.value("pack_shiphead_id").toInt());
    }
    if (_metrics->boolean("MultiWhs"))
      params.append("MultiWhs");

    orReport report(packq.value( packq.value("pack_shiphead_id").isNull() ?
			      "pickform" : "packform").toString(), params);
    if (report.isValid())
    {
      if (report.print(&printer, setupPrinter))
        setupPrinter = FALSE;
      else
      {
	orReport::endMultiPrint(&printer);
        return;
      }
    }
    else
    {
      report.reportError(this);
      orReport::endMultiPrint(&printer);
      return;
    }

    MetaSQLQuery mql(prts);
    prtd = mql.toQuery(params);
    if (prtd.lastError().type() != QSqlError::None)
    {
      systemError(this, prtd.lastError().databaseText(), __FILE__, __LINE__);
      orReport::endMultiPrint(&printer);
      return;
    }

  }
  orReport::endMultiPrint(&printer);
}