Exemple #1
0
int QgsDxfExport::writeToFile( QIODevice* d )
{
  if ( !d )
  {
    return 1;
  }

  if ( !d->isOpen() && !d->open( QIODevice::WriteOnly ) )
  {
    return 2;
  }

  mTextStream.setDevice( d );

  writeHeader();
  writeTables();
  writeBlocks();
  writeEntities();
  writeEndFile();
  return 0;
}
Exemple #2
0
int main(int argc, char **argv)
{
	atexit(cleanup);
	options = calloc(sizeof(struct opts_t), 1);
	tableList = calloc(sizeof(*tableList), 1);
	options->mode = -1;
	options->rating = RATING_UNDEFINED;

	if (argc < 2)
		exitmsg(1, "Try -help for usage information\n");

	parseArgs(argv + 1, options);

	if (!options->backlog_path[0])
		snprintf(options->backlog_path, 256, "%s/.backlog", getenv("HOME"));

	if (!modified && readTables(tableList, options->backlog_path))
		printf("Couldn't open '%s': %s\nYou can force the creation of this file by passing -force\n\n", options->backlog_path, strerror(errno));

	struct entry_t *entry = 0;
	struct table_t *table = 0;

	if (options->mode == RATING_UNDEFINED && !options->searchMode && !modified)
		exitmsg(1, "Must specify a mode\n");

	if (options->rating != RATING_UNDEFINED && (options->rating < -1 || options->rating > 10))
		exitmsg(1, "Rating must be in the range of -1 to 10\n");

	char *entrystr = "Entry not found\n";
	char *tablestr = "Table not found\n";
	char *err_dupe = "An entry with that name already exists in that table\n";
	char *err_dupe_table = "A table with that name already exists\n";

	char help_msg[] =
		"Available options:\n"
		" -t\n"
		"\tSwitch to table mode\n"
		" -help\n"
		"\tPrint this message\n"
		" -insert\n"
		"\tAdd entry/table\n"
		" -rm\n"
		"\tRemove entry/table\n"
		" -mv\n"
		"\tMove entry between tables\n"
		" -ls\n"
		"\tList tables, can be used with -name to print specific table\n"
		" -find\n"
		"\tSearch for entry, can be used with -name to search within table\n"
		" -rate <INT>\n"
		"\tSet rating on entry\n"
		" -rename\n"
		"\tRename entry/table\n"
		" -name <STRING>\n"
		"\tSet name\n"
		" -new <STRING>\n"
		"\tSet new name\n"
		" -src <STRING>\n"
		"\tSet source name\n"
		" -dest <STRING>\n"
		"\tSet destination name\n"
		" -roll\n"
		"\tReturn random entry\n"
		" -mal\n"
		"\tSearch MAL for entry\n"
		" -nyaa\n"
		"\tSearch Nyaa for entry\n"
		" -baka\n"
		"\tSearch BakaBT for entry (through Google)\n"
		" -anidb\n"
		"\tSearch AniDB for entry\n"
		" -force\n"
		"\tForce write-out to backlog file\n"
		" -path\n"
		"\tOverride the default backlog path (~/.backlog)\n"
		" -nobak\n"
		"\tDon't backup backlog file";

	if (options->mode == MODE_LIST) {	//List tables.
		if (options->sourceName) {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
		}
		if (table) {
			bklgPrintTable(table, stdout);
		} else {
			char buffer[32];
			struct tm *tm;
			time_t utime = time(0);

			tm = localtime(&utime);
			strftime(buffer, 32, "%d/%m/%Y\n", tm);
			puts(buffer);
			bklgPrintAllTables(tableList, stdout);
		}
	}

	else if (options->mode == MODE_ENTRY_ADD && options->tableMode) {	//Add table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");

		if (bklgGetTableExact(tableList, options->entryName))
			exitmsg(1, err_dupe_table);

		puts("Added table");
		bklgTableAdd(tableList, options->entryName);
		modified = 1;
	}

	else if (options->mode == MODE_ENTRY_REMOVE && options->tableMode) {	//Remove table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");
		struct table_t *table = bklgGetTable(tableList, options->entryName);

		if (!table)
			exitmsg(1, tablestr);
		printf("Remove \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Table removed");
			bklgLnodeDestroy(tableList, table->parent);
			bklgTableDestroy(table);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_ADD) {	//Add entry.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, rating optional\n");

		table = bklgGetTable(tableList, options->destinationName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetEntryExact(table->entries, options->entryName))
			exitmsg(1, err_dupe);

		printf("Add entry to \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Entry added");
			entry = bklgEntryAdd(table->entries, options->entryName, options->rating == -2 ? -1 : options->rating);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_REMOVE) {	//Remove entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		struct entry_t *entry = 0;

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Remove \"%s\" from \"%s\"? [Y,n]: ", entry->name, table->name);
		if (confirm()) {
			puts("Entry removed");
			bklgLnodeDestroy(table->entries, entry->parent);
			free(entry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_MOVE) {	//Move entry to another table.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, source optional\n");

		struct entry_t *oldEntry = 0;
		struct table_t *sourceTable = 0;

		if (!options->sourceName) {
			oldEntry = bklgGetEntryAllTables(tableList, options->entryName, &sourceTable);

		} else {
			sourceTable = bklgGetTable(tableList, options->sourceName);
			if (!sourceTable)
				exitmsg(1, "Source table not found\n");
			oldEntry = bklgGetEntry(sourceTable->entries, options->entryName);
		}
		if (!oldEntry)
			exitmsg(1, entrystr);

		struct table_t *destTable = bklgGetTable(tableList, options->destinationName);

		if (!destTable)
			exitmsg(1, "Destination table not found\n");

		if (bklgGetEntryExact(destTable->entries, oldEntry->name))
			exitmsg(1, err_dupe);

		printf("Move \"%s\" from \"%s\" to \"%s\"? [Y,n]: ", oldEntry->name, sourceTable->name, destTable->name);
		if (confirm()) {
			puts("Entry moved");
			entry = bklgEntryAdd(destTable->entries, oldEntry->name, (options->rating < -1 ? oldEntry->rating : options->rating));
			bklgLnodeDestroy(sourceTable->entries, oldEntry->parent);
			free(oldEntry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_FIND) {	//Find entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (entry) {
			char rating[8];

			snprintf(rating, 8, "%i/10", entry->rating);
			printf("Best match:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", entry->name, entry->rating == -1 ? "none" : rating, table->name);
		} else {
			exitmsg(1, entrystr);
		}
	}

	else if (options->rating != RATING_UNDEFINED) {	//Set a rating.
		if (!options->entryName)
			exitmsg(1, "Name required, rating and source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Change rating of \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Changed rating");
			entry->rating = options->rating;
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME && options->tableMode) {	//Rename table.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required\n");

		table = bklgGetTable(tableList, options->entryName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetTableExact(tableList, options->entryNewName))
			exitmsg(1, err_dupe_table);

		printf("Rename \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Renamed table");
			char tmp[64];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 64);
			tmpp = stripWhiteSpace(tmp);
			strncpy(table->name, tmpp, 64);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME) {	//Rename entry.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required, source optional\n");

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}

		if (!entry)
			exitmsg(1, entrystr);

		if (bklgGetEntryExact(table->entries, options->entryNewName))
			exitmsg(1, err_dupe);

		printf("Rename \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Renamed entry");
			char tmp[128];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 128);
			tmpp = stripWhiteSpace(tmp);
			strncpy(entry->name, tmpp, 128);
			modified = 1;
		}
	}

	else if (options->mode == MODE_RANDOM) {	//Choose random entry.
		if (!options->sourceName)
			exitmsg(1, "Source required\n");
		struct table_t *table = bklgGetTable(tableList, options->sourceName);

		if (!table)
			exitmsg(1, tablestr);
		srand(time(0) * getpid());
		size_t len = listlen(table->entries);
		int randomindex = rand() % len;
		struct lnode_t *current = table->entries->tail;

		for (int i = 0; i < randomindex; i++) {
			current = current->next;
		}
		entry = current->data;
		char rating[8];

		snprintf(rating, 8, "%i/10", entry->rating);
		printf("Random entry at index %i out of %zu:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", randomindex + 1, len, entry->name, entry->rating == -1 ? "none" : rating, table->name);
	}

	if (options->searchMode) {	//Search various sites for entry.
		if (!entry && !options->entryName) {
			exitmsg(1, "Name required, source optional\n");
		} else if (!entry) {
			struct table_t *table = 0;

			if (!options->sourceName) {
				entry = bklgGetEntryAllTables(tableList, options->entryName, &table);

			} else {
				if (!table) {
					table = bklgGetTable(tableList, options->sourceName);
					if (!table)
						exitmsg(1, tablestr);
				}
				entry = bklgGetEntry(table->entries, options->entryName);
			}
			if (!entry)
				exitmsg(1, entrystr);
		}
		printf("Searching web for \"%s\"\n", entry->name);
		webSearch(entry, options->searchMode);
	}

	else if (options->mode == MODE_HELP) {	//Print usage stuff.
		puts(help_msg);
	}

	if (modified) {
		if (backup) {
			char bakpath[256];

			snprintf(bakpath, 256, "%s.bak", options->backlog_path);
			if (rename(options->backlog_path, bakpath) == -1)
				exitmsg(1, "Failed to rename to '%s': %s\n", bakpath, strerror(errno));
		}
		if (writeTables(tableList, options->backlog_path))
			exitmsg(1, "Failed to open '%s' for writing: %s\n", options->backlog_path, strerror(errno));
	}
	exit(0);
}