Beispiel #1
0
void diff_it(
  std::list<linet> &lines1,
  std::list<linet> &lines2)
{
  std::string tmp1_name=get_temporary_file("delta_diff1", "txt");
  std::string tmp2_name=get_temporary_file("delta_diff2", "txt");
  std::string tmp3_name=get_temporary_file("delta_diff3", "txt");

  {  
    std::ofstream out1(tmp1_name.c_str());
    std::ofstream out2(tmp2_name.c_str());
    
    for(std::list<linet>::const_iterator l_it=lines1.begin();
        l_it!=lines1.end(); l_it++)
      out1 << l_it->line << "\n";

    for(std::list<linet>::const_iterator l_it=lines2.begin();
        l_it!=lines2.end(); l_it++)
      out2 << l_it->line << "\n";
  }
  
  std::string cmdline="diff \""+tmp1_name+"\""+
                          " \""+tmp2_name+"\""+
                         "> \""+tmp3_name+"\"";
  
  int result=system(cmdline.c_str());

  // open output
  if(result>=0)
  {
    std::ifstream in(tmp3_name.c_str());
    std::string line;
    std::list<std::string> diff;
    while(std::getline(in, line)) diff.push_back(line);
    process_diff(lines1, lines2, diff);
  }
  
  // clean up
  unlink(tmp1_name.c_str());
  unlink(tmp2_name.c_str());
  unlink(tmp3_name.c_str());
}
Beispiel #2
0
static bool process_file(const char *path, FILE *fp, struct File ***files)
{
    struct File *file;
    bool        res, is_diff;
    const char  *error = NULL;
    InputStream *is;
    char        buf[512];
    size_t      len;

    assert(sizeof(buf) >= MAGIC_LEN);

    /* Allocate file entry: */
    file = malloc(sizeof(struct File));
    assert(file != NULL);
    file->next   = NULL;
    file->path   = strdup(path);
    file->usable = false;
    assert(file->path != NULL);
    **files = file;
    *files = &file->next;

    if (fp != NULL)
    {
        fprintf(fp, "%s: ", path);
        fflush(fp);
    }

    is = (strcmp(path, "-") == 0) ? OpenStdinInputStream()
                                  : OpenFileInputStream(path);
    if (is == NULL)
    {
        error = "failed to open file";
        res = false;
    }
    else
    {
        len = is->read(is, buf, MAGIC_LEN);
        is_diff = memcmp(buf, MAGIC_STR, len) == 0;
        if (is_diff)
        {
            /* File starts with prefix of signature */
            if (len < MAGIC_LEN)
            {
                if (len == 0)
                {
                    error = "unreadable or empty file";
                }
                else
                {
                    error = "incomplete signature -- file truncated?";
                }
                res = false;
            }
            else
            {
                /* Valid signature; process as differences file */
                if (fp != NULL)
                {
                    fprintf(fp, "diff: ");
                    fflush(stdout);
                }
                res = process_diff(is, file, fp, &error);
            }
        }
        else
        {
            /* File does NOT start with a prefix of the signature; assume this
               is not a differences file, but a regular data file instead. */
            if (fp != NULL)
            {
                fprintf(fp, "data: ");
                fflush(stdout);
            }
            res = process_data(is, buf, sizeof(buf), len, file, fp, &error);
        }
    }

    if (is != NULL) is->close(is);

    if (!res)
    {
        assert(error != NULL);
        file->type = FILE_INVALID;
        file->invalid.error = error;
        if (fp != NULL) fprintf(fp, "%s\n", error);
    }

    return res;
}
Beispiel #3
0
int main(int argc,char** argv)
{
    int ret;

    int sleep_time = 1;
    int loop_times = -1;
    string file_name;
    int limit_num = -1;
    int clear_flag = 0;
    int diff_flag = 0;
    int print_info = 0;

    int input;

    while ((input = getopt (argc, argv, "hvs:l:f:n:cdp")) != -1) 
    {
        if ( input == 'h' )
        {
            usage();
            return 0;
        }

        if ( input == 'v' )
        {
            printf("%s\n", TOOL_VERSION);
            return 0;
        }

        if ( input == 's' )
        {
            sleep_time = atoi(optarg);
            continue;
        }
        if ( input == 'l' )
        {
            loop_times = atoi(optarg);
            continue;
        }
        if ( input == 'f' )
        {
            file_name = optarg;
            continue;
        }
        if ( input == 'n' )
        {
            limit_num = atoi(optarg);
            continue;
        }
        if ( input == 'c' )
        {
            clear_flag = 1;
            continue;
        }
        if ( input == 'd' )
        {
            diff_flag = 1;
            continue;
        }

        if ( input == 'p' )
        {
            print_info = 1;
            continue;
        }
    }

    if (file_name.empty())
    {
        usage();
        return -1;
    }

    if (print_info)
    {
        printf("file_name:\t%s\n", file_name.c_str());
        printf("sleep_secs:\t%d\n", sleep_time);
        printf("loop_times:\t%d\n", loop_times);
        printf("limit_num:\t%d\n", limit_num);
        printf("diff_flag:\t%d\n", diff_flag);
        printf("clear_flag:\t%d\n", clear_flag);
        printf("\n");
    }

    EasyStat stat_obj;
    ret = stat_obj.Init(file_name.c_str(),&stat_desc[0],STAT_OVER, false);
    if (ret)
    {
        printf("stat_obj init fail, ret:%d, msg:%s\n", ret, stat_obj.GetErrMsg());
        return -2;
    }

    if (clear_flag)
    {
        stat_obj.ResetStat();
        // 直接返回
        return 0;
    }

    int index = 0;

    while(1)
    {
        printf("--------------------------------------------------------------------\n");

        if (diff_flag)
        {
            process_diff(stat_obj, sleep_time, limit_num);
        }
        else
        {
            stat_obj.ShowStatInfo(limit_num);
        }

        index += 1;
        if (loop_times >=0 and index >= loop_times)
        {
            break;
        }

        if (!diff_flag)
        {
            sleep(sleep_time);
        }
    }

    return 0;
}