File: test_texthash2.cpp

package info (click to toggle)
libphash 0.9.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,168 kB
  • sloc: sh: 18,059; cpp: 11,339; php: 1,870; xml: 619; java: 173; cs: 139; makefile: 74; awk: 70; ansic: 44
file content (133 lines) | stat: -rw-r--r-- 3,328 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*

    pHash, the open source perceptual hash library
    Copyright (C) 2009 Aetilius, Inc.
    All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Evan Klinger - eklinger@phash.org
    David Starkweather - dstarkweather@phash.org

*/

#include <stdio.h>
#include <stdlib.h>
#include "pHash.h"

void sort_names(char **names, int L1){

    for (int i=0;i<L1;i++){
	int min = i;
	for (int j=i+1;j<L1;j++){
	    if (strcmp(names[j], names[min]) <= 0)
		min = j;
	}
	if (i != min){
	    char *swap = names[i];
	    names[i] = names[min];
	    names[min] = swap;
	}

    }

}

int main(int argc, char **argv){
    if (argc < 3){
	printf("not enough input args\n");
	exit(1);
    }
    const char *dir1 = argv[1];
    const char *dir2 = argv[2];

    int N1 = 0;
    char **files1 = ph_readfilenames(dir1, N1);
    sort_names(files1, N1);
    int N2 = 0;
    char **files2 = ph_readfilenames(dir2, N2);
    sort_names(files2, N2);

    if (N1 != N2){
	printf("unequal number files in each directory\n");
	exit(1);
    }

    TxtHashPoint *hash1, *hash2;
    TxtMatch *matches;
    int nbhashes1, nbhashes2, count;
    for (int i=0;i<N1;i++){

	hash1 = ph_texthash(files1[i], &nbhashes1);
	if (!hash1){
	    printf("unable to get hash\n");
	    continue;
	}
	printf("file%d: %s, length %d\n", i, files1[i], nbhashes1);
	hash2 = ph_texthash(files2[i], &nbhashes2);
	if (!hash2){
	    printf("unable to get hash\n");
	    free(hash1);
	    continue;
	}
	printf("file%d: %s, length %d\n", i, files2[i], nbhashes2);

	matches = ph_compare_text_hashes(hash1, nbhashes1, hash2, nbhashes2, &count);
	if (!matches){
	    printf("no matches found\n");
	    free(hash1);
	    free(hash2);
	    continue;
	}
	int maxlength = 0;
        for (int j=0;j<count;j++){
	    if (matches[j].length > maxlength)
		maxlength = matches[j].length;
	}
	printf("     no. matches %d, max length %d\n", count, maxlength);

	free(hash1);
	free(hash2);
	free(matches);
    }
    getchar();
    printf("******************\n");
    for (int i=0;i<N1;i++){
	hash1 = ph_texthash(files1[i], &nbhashes1);
	if (!hash1) continue;
	   
	for (int j=i+1;j<N1;j++){
	    hash2 = ph_texthash(files1[j], &nbhashes2);
	    if (!hash2) continue;
	    matches = ph_compare_text_hashes(hash1, nbhashes1, hash2, nbhashes2, &count);
	    if (!matches) continue;
	    printf("file%d: %s\n", i, files1[i]);
	    printf("file%d: %s\n", j, files2[j]);
	    int maxlength = 0;
	    for (int k=0;k<count;k++){
		if (matches[k].length > maxlength)
		    maxlength = matches[k].length;
	    }
	    printf("  no. matches %d, max length %d\n\n", count,maxlength);

	    free(hash2);
	    free(matches);

	}
	free(hash1);
    }

    printf("done\n");
    return 0;
}