Beispiel #1
0
bool Puzzle::setInitState(State* const s)
{
   if (initState != nullptr) initState->unref();
   initState = s;
   if (initState != nullptr) {
      initState->ref();
      // Add to our lists (to be processed)
      putHash(initState);
      putOpen(initState);
   }
   return true;
}
static int
readFontScale(HashTablePtr entries, char *dirname)
{
    int n = strlen(dirname);
    char *filename;
    FILE *in;
    int rc, count, i;
    char file[MAXFONTFILENAMELEN+1], font[MAXFONTNAMELEN+1];

    if(dirname[n - 1] == '/')
        filename = dsprintf("%sfonts.scale", dirname);
    else
        filename = dsprintf("%s/fonts.scale", dirname);
    if(filename == NULL)
        return -1;

    in = fopen(filename, "r");
    free(filename);
    if(in == NULL) {
        if(errno != ENOENT)
            perror("open(fonts.scale)");
        return -1;
    }

    rc = fscanf(in, "%d\n", &count);
    if(rc != 1) {
        fprintf(stderr, "Invalid fonts.scale in %s.\n", dirname);
        fclose(in);
        return -1;
    }

    for(i = 0; i < count; i++) {
        rc = fscanf(in,
		    "%" STRINGIFY(MAXFONTFILENAMELEN) "s "
		    "%" STRINGIFY(MAXFONTNAMELEN) "[^\n]\n",
		    file, font);
        if(rc != 2)
            break;
        putHash(entries, font, file, 100);
    }
    fclose(in);
    return 1;
}
Beispiel #3
0
int main(){
	HashTable * t = createH(10);
	Node * n = createNode(2);
	putHash(t,n);
	getHash(t,2);
}
static int
doDirectory(const char *dirname_given, int numEncodings, ListPtr encodingsToDo)
{
    char *dirname, *fontscale_name, *filename, *encdir;
    FILE *fontscale, *encfile;
    DIR *dirp;
    struct dirent *entry;
    FT_Error ftrc;
    FT_Face face;
    ListPtr encoding, xlfd, lp;
    HashTablePtr entries;
    HashBucketPtr *array;
    int i, n, found, rc;
    int isBitmap=0,xl=0;

    if (exclusionSuffix)
        xl = strlen (exclusionSuffix);

    i = strlen(dirname_given);
    if(i == 0)
        dirname = dsprintf("./");
    else if(dirname_given[i - 1] != '/')
        dirname = dsprintf("%s/", dirname_given);
    else
        dirname = dsprintf("%s", dirname_given);

    if(dirname == NULL) {
        perror("dirname");
        exit(1);
    }

    if (onlyEncodings)
	goto encodings;

    entries = makeHashTable();
    if(doBitmaps && !doScalable) {
        readFontScale(entries, dirname);
    }

    if(strcmp(outfilename, "-") == 0)
        fontscale_name = NULL;
    else {
        if(outfilename[0] == '/')
            fontscale_name = dsprintf("%s", outfilename);
        else
            fontscale_name = dsprintf("%s%s", dirname, outfilename);
        if(fontscale_name == NULL) {
            perror("fontscale_name");
            exit(1);
        }
    }

    dirp = opendir(dirname);
    if(dirp == NULL) {
        fprintf(stderr, "%s: ", dirname);
        perror("opendir");
        return 0;
    }

    if(fontscale_name == NULL)
        fontscale = stdout;
    else
        fontscale = fopen(fontscale_name, "wb");

    if(fontscale == NULL) {
        fprintf(stderr, "%s: ", fontscale_name);
        perror("fopen(w)");
        return 0;
    }

    while((entry = readdir(dirp)) != NULL) {
        int have_face = 0;
        char *xlfd_name = NULL;
	struct stat f_stat;
	int tprio = 1;

        xlfd = NULL;

	if (xl) {
	    int dl = strlen (entry->d_name);
	    if (strcmp (entry->d_name + dl - xl, exclusionSuffix) == 0)
		continue;
	}

        filename = dsprintf("%s%s", dirname, entry->d_name);

#define PRIO(x) ((x << 1) + tprio)
#ifdef DT_LNK
	if (entry->d_type != DT_UNKNOWN) {
	    if (entry->d_type == DT_LNK)
		tprio = 0;
	} else
#endif
#ifdef S_ISLNK
	{
#ifndef WIN32
	    if (lstat(filename, &f_stat))
		goto done;
	    if (S_ISLNK(f_stat.st_mode))
		tprio = 0;
#endif
	}
#else
	;
#endif
        if(doBitmaps)
            rc = bitmapIdentify(filename, &xlfd_name);
        else
            rc = 0;

        if(rc < 0)
            goto done;

        if(rc == 0) {
            ftrc = FT_New_Face(ft_library, filename, 0, &face);
            if(ftrc)
                goto done;
            have_face = 1;

            isBitmap = ((face->face_flags & FT_FACE_FLAG_SCALABLE) == 0);

            if(!isBitmap) {
                /* Workaround for bitmap-only SFNT fonts */
                if(FT_IS_SFNT(face) && face->num_fixed_sizes > 0 &&
                   strcmp(FT_Get_X11_Font_Format(face), "TrueType") == 0) {
                    TT_MaxProfile *maxp;
                    maxp = FT_Get_Sfnt_Table(face, ft_sfnt_maxp);
                    if(maxp != NULL && maxp->maxContours == 0)
                        isBitmap = 1;
                }
            }

            if(isBitmap) {
                if(!doBitmaps)
                    goto done;
            } else {
                if(!doScalable)
                    goto done;
            }

            if(isBitmap) {
                BDF_PropertyRec prop;
                rc = FT_Get_BDF_Property(face, "FONT", &prop);
                if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) {
                    xlfd_name = strdup(prop.u.atom);
                    if(xlfd_name == NULL)
                        goto done;
                }
            }
        }

        if(xlfd_name) {
            /* We know it's a bitmap font, and we know its XLFD */
            int n = strlen(xlfd_name);
            if(reencodeLegacy &&
               n >= 12 && strcasecmp(xlfd_name + n - 11, "-iso10646-1") == 0) {
                char *s;

                s = malloc(n - 10);
                memcpy(s, xlfd_name, n - 11);
                s[n - 11] = '\0';
                xlfd = listCons(s, xlfd);
            } else {
                /* Not a reencodable font -- skip all the rest of the loop body */
                putHash(entries, xlfd_name, entry->d_name, PRIO(filePrio(entry->d_name)));
                goto done;
            }
        }

        if(!have_face) {
            ftrc = FT_New_Face(ft_library, filename, 0, &face);
            if(ftrc)
                goto done;
            have_face = 1;
            isBitmap = ((face->face_flags & FT_FACE_FLAG_SCALABLE) == 0);

            if(!isBitmap) {
                if(face->num_fixed_sizes > 0) {
                    TT_MaxProfile *maxp;
                    maxp = FT_Get_Sfnt_Table(face, ft_sfnt_maxp);
                    if(maxp != NULL && maxp->maxContours == 0)
                        isBitmap = 1;
                }
            }
        }

        if(xlfd == NULL)
            xlfd = makeXLFD(entry->d_name, face, isBitmap);

        found = 0;

        for(lp = xlfd; lp; lp = lp->next) {
            char buf[MAXFONTNAMELEN];
            for(encoding = encodings; encoding; encoding = encoding->next) {
                if(checkEncoding(face, encoding->value)) {
                    found = 1;
                    snprintf(buf, MAXFONTNAMELEN, "%s-%s",
                            lp->value, encoding->value);
                    putHash(entries, buf, entry->d_name, PRIO(filePrio(entry->d_name)));
                }
            }
            for(encoding = extra_encodings; encoding;
                encoding = encoding->next) {
                if(checkExtraEncoding(face, encoding->value, found)) {
                    /* Do not set found! */
                    snprintf(buf, MAXFONTNAMELEN, "%s-%s",
                            lp->value, encoding->value);
                    putHash(entries, buf, entry->d_name, PRIO(filePrio(entry->d_name)));
                }
            }
        }
    done:
        if(have_face)
            FT_Done_Face(face);
        deepDestroyList(xlfd);
        xlfd = NULL;
        free(filename);
#undef PRIO
    }

    closedir(dirp);
    n = hashElements(entries);
    fprintf(fontscale, "%d\n", n);
    array = hashArray(entries, 1);
    for(i = 0; i < n; i++)
        fprintf(fontscale, "%s %s\n", array[i]->value, array[i]->key);
    destroyHashArray(array);
    entries = NULL;
    if(fontscale_name) {
        fclose(fontscale);
        free(fontscale_name);
    }

 encodings:
    encdir = dsprintf("%s%s", dirname, "encodings.dir");

    if(encdir == NULL) {
	perror("encodings");
	exit(1);
    }
    unlink(encdir);

    if (numEncodings) {
	encfile = fopen(encdir, "w");
	if(encfile == NULL) {
	    perror("open(encodings.dir)");
	    exit(1);
	}
        fprintf(encfile, "%d\n", numEncodings);
        encodingsToDo = sortList(encodingsToDo);
        for(lp = encodingsToDo; lp; lp = lp->next) {
            fprintf(encfile, "%s\n", lp->value);
        }
	fclose (encfile);
    }

    free(dirname);
    return 1;
}
Beispiel #5
0
void put(int* sock, char* filename, conf_t config, node_t* headptr) {
	FILE* fp = NULL, *f1 = NULL, *f2 = NULL, *f3 = NULL, *f4 = NULL;
	int i, rc;
	int invalid = 0;
	int hash;
	int size, f1size, f2size, f3size;//, f4size;
	pieces_t dfs[4]; // Denotes which piece we are putting in which file
	char buf;

    fp = fopen(filename, "rb");
    if (!fp) {
        fprintf(stderr, "Error opening file\n");
		return;
    }

    hash = getHash(fp);
    putHash(hash, filename, headptr);

   	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	f1size = size/4;
	f2size = size/4;
	f3size = size/4;
	//f4size = size/4 + size%4; // Give the extra bytes to the last file chunk

	/* Divide the file */
	f1 = tmpfile();
	f2 = tmpfile();
	f3 = tmpfile();
	f4 = tmpfile();

	char c;
	int k = 0;
	while((c = fgetc(fp)) != EOF) {
		if (k < f1size){
			fputc(c, f1);
		} else if (k < f1size+f2size) {
			fputc(c, f2);
		} else if (k < f1size+f2size+f3size) {
			fputc(c, f3);
		} else {
			fputc(c, f4);
		}
		k++;
	}

    /* Set up the hasing values */
    switch (hash) {
    	case 0:
    		dfs[0].p1 = 1; dfs[0].p2 = 2;
    		dfs[1].p1 = 2; dfs[1].p2 = 3;
    		dfs[2].p1 = 3; dfs[2].p2 = 4;
    		dfs[3].p1 = 4; dfs[3].p2 = 1;
    		break;
    	case 1:
    		dfs[0].p1 = 4; dfs[0].p2 = 1;
    		dfs[1].p1 = 1; dfs[1].p2 = 2;
    		dfs[2].p1 = 2; dfs[2].p2 = 3;
    		dfs[3].p1 = 3; dfs[3].p2 = 4;
    		break;
    	case 2:
    		dfs[0].p1 = 3; dfs[0].p2 = 4;
    		dfs[1].p1 = 4; dfs[1].p2 = 1;
    		dfs[2].p1 = 1; dfs[2].p2 = 2;
    		dfs[3].p1 = 2; dfs[3].p2 = 3;
    		break;
    	case 3:
    		dfs[0].p1 = 2; dfs[0].p2 = 3;
    		dfs[1].p1 = 3; dfs[1].p2 = 4;
    		dfs[2].p1 = 4; dfs[2].p2 = 1;
    		dfs[3].p1 = 1; dfs[3].p2 = 2;
    		break;
    	default:
    		printf("Oh no!");
    		return;
    }

	sendAuth(sock, config); // Send authorization to servers
	for (i = 0; i < 4; i++) {
		while((rc = read(sock[i], &buf, sizeof(buf))) > 0) {
			if (buf == 'I')
				invalid = 1;
			if (buf == '\x3')
				break;
		}
		if (invalid) {
			write(sock[i], "PUT", strlen("PUT"));
			printf("Invalid Username/Password. Please try again.\n");
			return;
		}
		if(rc > 0) {
			char request[BUF_SIZE], piece[BUF_SIZE], ch;
			strncpy(request, "PUT ", sizeof("PUT "));
			strcat(request, filename);
			strcat(request, " ");
			sprintf(piece, "%d %d", dfs[i].p1, dfs[i].p2);
			strcat(request, piece);
			write(sock[i], request, strlen(request));

			fseek(f1, 0, SEEK_SET);
			fseek(f2, 0, SEEK_SET);
			fseek(f3, 0, SEEK_SET);
			fseek(f4, 0, SEEK_SET);

			if(dfs[i].p1 == 1) {
				while((ch = fgetc(f1)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p1 == 2) {
				while((ch = fgetc(f2)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p1 == 3) {
				while((ch = fgetc(f3)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p1 == 4) {
				while((ch = fgetc(f4)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			}

			fseek(f1, 0, SEEK_SET);
			fseek(f2, 0, SEEK_SET);
			fseek(f3, 0, SEEK_SET);
			fseek(f4, 0, SEEK_SET);

			if(dfs[i].p2 == 1) {
				while((ch = fgetc(f1)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p2 == 2) {
				while((ch = fgetc(f2)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p2 == 3) {
				while((ch = fgetc(f3)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			} else if (dfs[i].p2 == 4) {
				while((ch = fgetc(f4)) != EOF) 
					write(sock[i], &ch, 1);
				char delim = '\x3';
			    write(sock[i], &delim, 1);
			}

			// while((rc = read(sock[i], &r, sizeof(r))) > 0) {
			// 	if (r == '\x3')
			// 		break;
			// 	printf("%c", r);
			// }
		}
		else {
			sock[i] = -1;
		}
	}

	if (fclose(fp)) {
        fprintf(stderr, "Error closing file\n");
    }
    if (fclose(f1)) {
        fprintf(stderr, "Error closing file\n");
    }
    if (fclose(f2)) {
        fprintf(stderr, "Error closing file\n");
    }
    if (fclose(f3)) {
        fprintf(stderr, "Error closing file\n");
    }
    if (fclose(f4)) {
        fprintf(stderr, "Error closing file\n");
    }
    unlink(filename);
}