text:
#include <errno.h>
#include <priv/alt_file.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "altera_up_sd_card_avalon_interface.h"

///////////////////////////////////////////////////////////////////////////
// Local Define Statements
///////////////////////////////////////////////////////////////////////////

#define CHAR_TO_UPPER(ch)	((char) (((ch >= 'a') && (ch <= 'z')) ? ((ch-'a')+'A'): ch))

// Data Buffer Address
#define SD_CARD_BUFFER(base, x)			(base + x)
// 128-bit Card Identification Number
#define SD_CARD_CID(base, x)			(base + 0x0200 + x)
// 128-bit Card Specific Data Register
#define SD_CARD_CSD(base, x)			(base + 0x0210 + x)
// 32-bit Operating Conditions Register
#define SD_CARD_OCR(base)				(base + 0x0220)
// 32-bit Card Status Register
#define SD_CARD_STATUS(base)			(base + 0x0224)
// 16-bit Relative Card Address Register
#define SD_CARD_RCA(base)				(base + 0x0228)
// 32-bit Card Argument Register
#define SD_CARD_ARGUMENT(base)			(base + 0x022C)
// 16-bit Card Command Register
#define SD_CARD_COMMAND(base)			(base + 0x0230)
// 16-bit Card Auxiliary Status Register
#define SD_CARD_AUX_STATUS(base)		(base + 0x0234)
// 32-bit R1 Response Register
#define SD_CARD_R1_RESPONSE(base)		(base + 0x0238)

#define CMD_READ_BLOCK					17
#define CMD_WRITE_BLOCK					24

// FAT 12/16 related stuff
//#define BOOT_SECTOR_DATA_SIZE			0x005A
#define MAX_FILES_OPENED				20

/******************************************************************************/
/******  LOCAL DATA STRUCTURES  ***********************************************/
/******************************************************************************/

typedef struct s_FAT_12_16_boot_sector {
	unsigned char jump_instruction[3];
	char OEM_name[8];
	unsigned short int sector_size_in_bytes;
	unsigned char sectors_per_cluster;
	unsigned short int reserved_sectors;
	unsigned char number_of_FATs;
	unsigned short int max_number_of_dir_entires;
	unsigned short int number_of_sectors_in_partition;
	unsigned char media_descriptor;
	unsigned short int number_of_sectors_per_table;
	unsigned short int number_of_sectors_per_track;
	unsigned short int number_of_heads;
	unsigned int number_of_hidden_sectors;
	unsigned int total_sector_count_if_above_32MB;
	unsigned char drive_number;
	unsigned char current_head;
	unsigned char boot_signature;
	unsigned char volume_id[4];
	char volume_label[11];
	unsigned char file_system_type[8];
	unsigned char bits_for_cluster_index;
	unsigned int first_fat_sector_offset;
	unsigned int second_fat_sector_offset;
	unsigned int root_directory_sector_offset;
	unsigned int data_sector_offset;
} t_FAT_12_16_boot_sector;

typedef struct s_file_record {
	unsigned char name[8];
	unsigned char extension[3];
	unsigned char attributes;
	unsigned short int create_time;
	unsigned short int create_date;
	unsigned short int last_access_date;
	unsigned short int last_modified_time;
	unsigned short int last_modified_date;
	unsigned short int start_cluster_index;
	unsigned int file_size_in_bytes;
	/* The following fields are only used when a file has been created or opened. */
	unsigned int current_cluster_index;
	unsigned int current_sector_in_cluster;
	unsigned int current_byte_position;
	// Absolute location of the file record on the SD Card.
	unsigned int file_record_cluster;
	unsigned int file_record_sector_in_cluster;
	short int file_record_offset;
	// Is this record in use and has the file been modified.
	unsigned int home_directory_cluster;
	bool modified;
	bool in_use;
} t_file_record;

typedef struct s_find_data {
	unsigned int directory_root_cluster; // 0 means root directory.
	unsigned int current_cluster_index;
	unsigned int current_sector_in_cluster;
	short int file_index_in_sector;
	bool valid;
} t_find_data;

///////////////////////////////////////////////////////////////////////////
// Local Variables
///////////////////////////////////////////////////////////////////////////

bool initialized = false;
bool is_sd_card_formated_as_FAT16 = false;
volatile short int *aux_status_register = NULL;
volatile int *status_register = NULL;
volatile short int *CSD_register_w0 = NULL;
volatile short int *command_register = NULL;
volatile int *command_argument_register = NULL;
volatile char *buffer_memory = NULL;
int fat_partition_offset_in_512_byte_sectors = 0;
int fat_partition_size_in_512_byte_sectors = 0;

t_FAT_12_16_boot_sector boot_sector_data;

alt_up_sd_card_dev *device_pointer = NULL;

// Pointers to currently opened files.
t_file_record active_files[MAX_FILES_OPENED];
bool current_sector_modified = false;
unsigned int current_sector_index = 0;

t_find_data search_data;

///////////////////////////////////////////////////////////////////////////
// Local Functions
///////////////////////////////////////////////////////////////////////////
bool Read_Sector_Data_Attempt_CRC(int sector_index, int partition_offset);

bool Write_Sector_Data(int sector_index, int partition_offset)
// This function writes a sector at the specified address on the SD Card.
{
	bool result = false;

	if (alt_up_sd_card_is_Present()) {
		short int reg_state = 0xff;

		/* Multiply sector offset by sector size to get the address. Sector size is 512. Also,
		 * the SD card reads data in 512 byte chunks, so the address must be a multiple of 512. */
		IOWR_32DIRECT(command_argument_register, 0,
				(sector_index + partition_offset)*512);
		IOWR_16DIRECT(command_register, 0, CMD_WRITE_BLOCK);
		do {
			reg_state = (short int) IORD_16DIRECT(aux_status_register,0);
		} while ((reg_state & 0x04) != 0);
		// Make sure the request did not time out.
		if ((reg_state & 0x10) == 0) {
			result = true;
			current_sector_modified = false;
			current_sector_index = sector_index + partition_offset;
		}
	}
	return result;
}

bool Save_Modified_Sector()
// If the sector has been modified, then save it to the SD Card.
{
	bool result = true;
	if (current_sector_modified) {
		result = Write_Sector_Data(current_sector_index, 0);
	}
	return result;
}

bool Read_Sector_Data(int sector_index, int partition_offset)
// This function reads a sector at the specified address on the SD Card.
{
	bool result = false;

	if (alt_up_sd_card_is_Present()) {
		short int reg_state = 0xff;

		/* Write data to the SD card if the current buffer is out of date. */
		if (current_sector_modified) {
			if (Write_Sector_Data(current_sector_index, 0) == false) {
				return false;
			}
		}
		/* Multiply sector offset by sector size to get the address. Sector size is 512. Also,
		 * the SD card reads data in 512 byte chunks, so the address must be a multiple of 512. */
		IOWR_32DIRECT(command_argument_register, 0,
				(sector_index + partition_offset)*512);
		IOWR_16DIRECT(command_register, 0, CMD_READ_BLOCK);
		do {
			reg_state = (short int) IORD_16DIRECT(aux_status_register,0);
		} while ((reg_state & 0x04) != 0);
		// Make sure the request did not time out.
		if ((reg_state & 0x10) == 0) {
			result = true;
			current_sector_modified = false;
			current_sector_index = sector_index + partition_offset;
		}
	}
	return result;
}

bool get_cluster_flag(unsigned int cluster_index, unsigned short int *flag)
// Read a cluster flag.
{
	unsigned int sector_index = (cluster_index / 256)
			+ fat_partition_offset_in_512_byte_sectors;

	sector_index = sector_index + boot_sector_data.first_fat_sector_offset;

	if (sector_index != current_sector_index) {
		if (Read_Sector_Data(sector_index, 0) == false) {
			return false;
		}
	}
	*flag =
			(unsigned short int) IORD_16DIRECT(device_pointer->base, 2*(cluster_index % 256));
	return true;
}

bool mark_cluster(unsigned int cluster_index, short int flag, bool first_fat)
// Place a marker on the specified cluster in a given FAT.
{
	unsigned int sector_index = (cluster_index / 256)
			+ fat_partition_offset_in_512_byte_sectors;

	if (first_fat) {
		sector_index = sector_index + boot_sector_data.first_fat_sector_offset;
	} else {
		sector_index = sector_index + boot_sector_data.second_fat_sector_offset;
	}

	if (sector_index != current_sector_index) {
		if (Read_Sector_Data(sector_index, 0) == false) {
			return false;
		}
	}
	IOWR_16DIRECT(device_pointer->base, 2*(cluster_index % 256), flag);
	current_sector_modified = true;
	return true;
}

bool Check_for_Master_Boot_Record(void)
// This function reads the first 512 bytes on the SD Card. This data should
// contain the Master Boot Record. If it does, then print
// relevant information and return true. Otherwise, return false.
{
	bool result = false;
	int index;
	int end, offset, partition_size;

	/* Load the first 512 bytes of data from SD card. */
	if (Read_Sector_Data_Attempt_CRC(0, 0)) {
		end = (short int) IORD_16DIRECT(device_pointer->base,0x1fe);

		// Check if the end of the sector contains an end string 0xaa55.
		if ((end & 0x0000ffff) == 0x0000aa55) {
			// Check four partition entries and see if any are valid
			for (index = 0; index < 4; index++) {
				int partition_data_offset = (index * 16) + 0x01be;
				char type;

				// Read Partition type
				type =
						(unsigned char) IORD_8DIRECT(device_pointer->base,partition_data_offset + 0x04);

				// Check if this is an FAT parition
				if ((type == 1) || (type == 4) || (type == 6) || (type == 14)) {
					// Get partition offset and size.
					offset =
							(((unsigned short int) IORD_16DIRECT(device_pointer->base,partition_data_offset + 0x0A))
									<< 16)
									| ((unsigned short int) IORD_16DIRECT(device_pointer->base,partition_data_offset + 0x08));
					partition_size =
							(((unsigned short int) IORD_16DIRECT(device_pointer->base,partition_data_offset + 0x0E))
									<< 16)
									| ((unsigned short int) IORD_16DIRECT(device_pointer->base,partition_data_offset + 0x0C));

					// Check if the partition is valid
					if (partition_size > 0) {
						result = true;
						fat_partition_size_in_512_byte_sectors = partition_size;
						fat_partition_offset_in_512_byte_sectors = offset;
						break;
					}
				}
			}
		}
	}

	return result;
}

bool Read_File_Record_At_Offset(int offset, t_file_record *record,
		unsigned int cluster_index, unsigned int sector_in_cluster)
// This function reads a file record
{
	bool result = false;
	if (((offset & 0x01f) == 0) && (alt_up_sd_card_is_Present())
			&& (is_sd_card_formated_as_FAT16)) {
		int counter;

		for (counter = 0; counter < 8; counter++) {
			record->name[counter] =
					(char) IORD_8DIRECT(device_pointer->base, offset+counter);
		}
		for (counter = 0; counter < 3; counter++) {
			record->extension[counter] =
					(char) IORD_8DIRECT(device_pointer->base, offset+counter+8);
		}
		record->attributes =
				(char) IORD_8DIRECT(device_pointer->base, offset+11);
		/* Ignore reserved bytes at locations 12 and 13. */
		record->create_time =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+14);
		record->create_date =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+16);
		record->last_access_date =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+18);
		/* Ignore reserved bytes at locations 20 and 21. */
		record->last_modified_time =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+22);
		record->last_modified_date =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+24);
		record->start_cluster_index =
				(unsigned short int) IORD_16DIRECT(device_pointer->base, offset+26);
		record->file_size_in_bytes =
				(unsigned int) IORD_32DIRECT(device_pointer->base, offset+28);
		record->file_record_cluster = cluster_index;
		record->file_record_sector_in_cluster = sector_in_cluster;
		record->file_record_offset = offset;
		result = true;
	}
	return result;
}

bool Write_File_Record_At_Offset(int offset, t_file_record *record)
// This function writes a file record at a given offset. The offset is given in bytes.
{
	bool result = false;
	if (((offset & 0x01f) == 0) && (alt_up_sd_card_is_Present())
			&& (is_sd_card_formated_as_FAT16)) {
		int counter;

		for (counter = 0; counter < 8; counter = counter + 2) {
			short int two_chars = (short int) record->name[counter + 1];
			two_chars = two_chars << 8;
			two_chars = two_chars | record->name[counter];
			IOWR_16DIRECT(device_pointer->base, offset+counter, two_chars);
		}
		for (counter = 0; counter < 3; counter++) {
			IOWR_8DIRECT(device_pointer->base, offset+counter+8,
					record->extension[counter]);
		}
		IOWR_8DIRECT(device_pointer->base, offset+11, record->attributes);
		/* Ignore reserved bytes at locations 12 and 13. */
		IOWR_16DIRECT(device_pointer->base, offset+14, record->create_time);
		IOWR_16DIRECT(device_pointer->base, offset+16, record->create_date);
		IOWR_16DIRECT(device_pointer->base, offset+18,
				record->last_access_date);
		/* Ignore reserved bytes at locations 20 and 21. */
		IOWR_16DIRECT(device_pointer->base, offset+22,
				record->last_modified_time);
		IOWR_16DIRECT(device_pointer->base, offset+24,
				record->last_modified_date);
		IOWR_16DIRECT(device_pointer->base, offset+26,
				record->start_cluster_index);
		IOWR_32DIRECT(device_pointer->base, offset+28,
				record->file_size_in_bytes);
		current_sector_modified = true;
		result = true;
	}
	return result;
}

bool Check_for_DOS_FAT(int FAT_partition_start_sector)
// This function reads the boot sector for the FAT file system on the SD Card.
// The offset_address should point to the sector on the card where the boot sector is located.
// The sector number is specified either in the master Boot Record, or is 0 by default for a purely FAT
// based file system. If the specified sector contains a FAT boot sector, then this function prints the
// relevant information and returns 1. Otherwise, it returns 0.
{
	bool result = false;
	int counter = 0;
	short int end;

	result = Read_Sector_Data_Attempt_CRC(0, FAT_partition_start_sector);
	end = (short int) IORD_16DIRECT(device_pointer->base, 0x1fe);
	if (((end & 0x0000ffff) == 0x0000aa55) && (result)) {
		int num_clusters = 0;

		boot_sector_data.jump_instruction[0] =
				(char) IORD_8DIRECT(device_pointer->base, 0);
		boot_sector_data.jump_instruction[1] =
				(char) IORD_8DIRECT(device_pointer->base, 1);
		boot_sector_data.jump_instruction[2] =
				(char) IORD_8DIRECT(device_pointer->base, 2);
		for (counter = 0; counter < 8; counter++) {
			boot_sector_data.OEM_name[counter] =
					(char) IORD_8DIRECT(device_pointer->base, 3+counter);
		}
		boot_sector_data.sector_size_in_bytes =
				(((unsigned char) IORD_8DIRECT(device_pointer->base, 12)) << 8)
						| ((char) IORD_8DIRECT(device_pointer->base, 11));
		boot_sector_data.sectors_per_cluster =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 13));
		boot_sector_data.reserved_sectors =
				((unsigned short int) IORD_16DIRECT(device_pointer->base, 14));
		boot_sector_data.number_of_FATs =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 16));
		boot_sector_data.max_number_of_dir_entires =
				(((unsigned short int) (((unsigned char) IORD_8DIRECT(device_pointer->base, 18))))
						<< 8)
						| ((unsigned char) IORD_8DIRECT(device_pointer->base, 17));
		boot_sector_data.number_of_sectors_in_partition =
				(((unsigned short int) (((unsigned char) IORD_8DIRECT(device_pointer->base, 20))))
						<< 8)
						| ((unsigned char) IORD_8DIRECT(device_pointer->base, 19));
		boot_sector_data.media_descriptor =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 21));
		boot_sector_data.number_of_sectors_per_table =
				((unsigned short int) IORD_16DIRECT(device_pointer->base, 22));
		boot_sector_data.number_of_sectors_per_track =
				((unsigned short int) IORD_16DIRECT(device_pointer->base, 24));
		boot_sector_data.number_of_heads =
				((unsigned short int) IORD_16DIRECT(device_pointer->base, 26));
		boot_sector_data.number_of_hidden_sectors =
				((unsigned int) IORD_32DIRECT(device_pointer->base, 28));
		boot_sector_data.total_sector_count_if_above_32MB =
				((unsigned int) IORD_32DIRECT(device_pointer->base, 32));
		boot_sector_data.drive_number =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 36));
		boot_sector_data.current_head =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 37));
		boot_sector_data.boot_signature =
				((unsigned char) IORD_8DIRECT(device_pointer->base, 38));
		boot_sector_data.first_fat_sector_offset =
				boot_sector_data.reserved_sectors;
		boot_sector_data.second_fat_sector_offset =
				boot_sector_data.first_fat_sector_offset
						+ boot_sector_data.number_of_sectors_per_table;
		boot_sector_data.root_directory_sector_offset =
				boot_sector_data.second_fat_sector_offset
						+ boot_sector_data.number_of_sectors_per_table;
		boot_sector_data.data_sector_offset =
				boot_sector_data.root_directory_sector_offset
						+ (32 * boot_sector_data.max_number_of_dir_entires
								/ boot_sector_data.sector_size_in_bytes);

		if (boot_sector_data.number_of_sectors_in_partition > 0) {
			num_clusters = (boot_sector_data.number_of_sectors_in_partition
					/ boot_sector_data.sectors_per_cluster);
		} else {
			num_clusters = (boot_sector_data.total_sector_count_if_above_32MB
					/ boot_sector_data.sectors_per_cluster);
		}
		if (num_clusters < 4087) {
			boot_sector_data.bits_for_cluster_index = 12;
		} else if (num_clusters <= 65517) {
			boot_sector_data.bits_for_cluster_index = 16;
		} else {
			boot_sector_data.bits_for_cluster_index = 32;
		}

		for (counter = 0; counter < 4; counter++) {
			boot_sector_data.volume_id[counter] =
					((char) IORD_8DIRECT(device_pointer->base, 39+counter));
		}
		for (counter = 0; counter < 11; counter++) {
			boot_sector_data.volume_label[counter] =
					((char) IORD_8DIRECT(device_pointer->base, 43+counter));
		}
		for (counter = 0; counter < 8; counter++) {
			boot_sector_data.file_system_type[counter] =
					((char) IORD_8DIRECT(device_pointer->base, 54+counter));
		}
		// Clear file records
		for (counter = 0; counter < MAX_FILES_OPENED; counter++) {
			active_files[counter].in_use = false;
		}
		result = true;
	} else {
		result = false;
	}
	return result;
}

bool Look_for_FAT16(void)
// Read the SD card to determine if it contains a FAT16 partition.
{
	bool result = false;

	if (alt_up_sd_card_is_Present()) {
		Read_Sector_Data_Attempt_CRC(0, 0);

		short int csd_file_format = *CSD_register_w0;

		fat_partition_offset_in_512_byte_sectors = 0;
		fat_partition_size_in_512_byte_sectors = 0;

		if (((csd_file_format & 0x8000) == 0)
				&& ((csd_file_format & 0x0c00) != 0x0c00)) {
			if ((csd_file_format & 0x0c00) == 0x0400) {
				/* SD Card contains files stored in a DOS FAT (floppy like) file format, without a partition table */
				result = Check_for_DOS_FAT(0);
			}
			if ((csd_file_format & 0x0c00) == 0x0000) {
				/* SD Card contains files stored in a Hard disk-like file format that contains a partition table */
				if (Check_for_Master_Boot_Record()) {
					result = Check_for_DOS_FAT(
							fat_partition_offset_in_512_byte_sectors);
				}
			}
			if (result == true) {
				// Accept only FAT16, not FAT12.
				if (boot_sector_data.bits_for_cluster_index != 16) {
					result = false;
				} else {
					fat_partition_size_in_512_byte_sectors =
							boot_sector_data.number_of_sectors_in_partition;
				}
			}
		}
	}
	return result;
}

void filename_to_upper_case(char *file_name)
// Change file name to upper case.
{
	int index;
	int length = strlen(file_name);

	for (index = 0; index < length; index++) {
		if ((file_name[index] >= 'a') && (file_name[index] <= 'z')) {
			file_name[index] = (file_name[index] - 'a') + 'A';
		}
	}
}

bool check_file_name_for_FAT16_compliance(char *file_name)
// Check if the file complies with FAT16 naming convention.
{
	int length = strlen(file_name);
	int index;
	int last_dir_break_position = -1;
	int last_period = -1;
	bool result = true;

	for (index = 0; index < length; index++) {
		if ((file_name[index] == ' ')
				|| ((last_dir_break_position == (index - 1))
						&& ((file_name[index] == '\\')
								|| (file_name[index] == '/')))
				|| ((index - last_period == 9) && (file_name[index] != '.'))
				|| ((last_dir_break_position != last_period)
						&& (index - last_period > 3)
						&& (file_name[index] != '\\')
						&& (file_name[index] != '/'))) {
			result = false;
			break;
		}
		if ((file_name[index] == '\\') || (file_name[index] == '/')) {
			last_period = index;
			last_dir_break_position = index;
		}
		if (file_name[index] == '.') {
			last_period = index;
		}
	}
	if ((file_name[length - 1] == '\\') || (file_name[length - 1] == '/')) {
		result = false;
	}
	return result;
}

int get_dir_divider_location(char *name)
// Find a directory divider location.
{
	int index = 0;
	int length = strlen(name);

	for (index = 0; index < length; index++) {
		if ((name[index] == '\\') || (name[index] == '/')) {
			break;
		}
	}

	if (index == length) {
		index = -1;
	}

	return index;
}

bool match_file_record_to_name_ext(t_file_record *file_record, char *name,
		char *extension)
/* See if the given name and extension match the file record. Return true if this is so, false otherwise. */
{
	bool match = true;
	int index;

	for (index = 0; index < 8; index++) {
		if (CHAR_TO_UPPER(file_record->name[index])
				!= CHAR_TO_UPPER(name[index])) {
			match = false;
			break;
		}
	}
	for (index = 0; index < 3; index++) {
		if (CHAR_TO_UPPER(file_record->extension[index])
				!= CHAR_TO_UPPER(extension[index])) {
			match = false;
			break;
		}
	}
	return match;
}

bool get_home_directory_cluster_for_file(char *file_name,
		int *home_directory_cluster, t_file_record *file_record)
// Scan the directories in given in the file name and find the root directory for the file.
{
	bool result = false;
	int home_dir_cluster = 0;
	int location, index;
	int start_location = 0;

	/* Get Next Directory. */
	location = get_dir_divider_location(file_name);
	while (location > 0) {
		char name[8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
		char extension[3] = { ' ', ' ', ' ' };
		int ext_index = -1;
		int new_cluster = home_dir_cluster;

		// Get the name of the directory in name/extension format.
		for (index = 0; index < location; index++) {
			if (file_name[index + start_location] == '.') {
				ext_index = index;
			} else if (ext_index < 0) {
				name[index] = file_name[index + start_location];
			} else {
				extension[index - ext_index] =
						file_name[index + start_location];
			}
		}

		if (home_dir_cluster == 0) {
			/* We are in the root directory. Scan the directory (of predefined size) and see if you can find the specified file. */
			int max_root_dir_sectors = ((32
					* boot_sector_data.max_number_of_dir_entires)
					/ boot_sector_data.sector_size_in_bytes);
			int sector_index;

			for (sector_index = 0; sector_index < max_root_dir_sectors;
					sector_index++) {
				if (Read_Sector_Data(
						sector_index
								+ boot_sector_data.root_directory_sector_offset,
						fat_partition_offset_in_512_byte_sectors)) {
					int file_counter;

					for (file_counter = 0; file_counter < 16; file_counter++) {

						// Read file record.
						Read_File_Record_At_Offset(file_counter * 32,
								file_record, 0, sector_index);
						if ((file_record->name[0] != 0xe5)
								&& (file_record->name[0] != 0x00)) {
							bool match = match_file_record_to_name_ext(
									file_record, name, extension);
							if (match) {
								new_cluster = file_record->start_cluster_index;
								file_record->file_record_cluster = 1; // Home directory is a subdirectory in the root directory.
								break;
							}
						}
					}
				} else {
					break;
				}
				if (new_cluster != home_dir_cluster) {
					break;
				}
			}
			if (new_cluster != home_dir_cluster) {
				// A valid directory is found, so go to it.
				home_dir_cluster = new_cluster;
				start_location = start_location + location + 1;
			} else {
				// Directory path is invalid.
				return false;
			}
		} else {
			// This is a subdirectory that can have any number of elements. So scan through it as though it was a file
			// and see if you can find the directory of interest.
			int cluster = home_dir_cluster;

			do {
				int start_sector = (cluster - 2)
						* (boot_sector_data.sectors_per_cluster)
						+ boot_sector_data.data_sector_offset;
				int sector_index;

				for (sector_index = 0;
						sector_index < boot_sector_data.sectors_per_cluster;
						sector_index++) {
					if (Read_Sector_Data(sector_index + start_sector,
							fat_partition_offset_in_512_byte_sectors)) {
						int file_counter;

						for (file_counter = 0; file_counter < 16;
								file_counter++) {
							// Read file record.
							Read_File_Record_At_Offset(file_counter * 32,
									file_record, cluster, sector_index);
							if ((file_record->name[0] != 0xe5)
									&& (file_record->name[0] != 0x00)) {
								bool match = match_file_record_to_name_ext(
										file_record, name, extension);
								if (match) {
									new_cluster =
											file_record->start_cluster_index;
									break;
								}
							}
						}
					} else {
						break;
					}
					if (new_cluster != home_dir_cluster) {
						break;
					}
				}
				// If this is the end of the cluster and the directory has not been found, then see if there is another cluster
				// that holds data for the current directory.
				if (new_cluster == home_dir_cluster) {
					unsigned short int next_cluster;

					if (get_cluster_flag(new_cluster, &next_cluster)) {
						// The directory needs to be expanded to store more files.
						if ((next_cluster & 0x0000fff8) == 0x0000fff8) {
							return false;
						}
						new_cluster = (next_cluster & 0x0000fff8);
					} else {
						// Directory path is invalid.
						return false;
					}
				}
			} while ((cluster < 0x0000fff8) && (new_cluster == home_dir_cluster));
			if (new_cluster != home_dir_cluster) {
				// A valid directory is found, so go to it.
				home_dir_cluster = new_cluster;
				start_location = start_location + location + 1;
			} else {
				// Directory path is invalid.
				return false;
			}
		}
		location = get_dir_divider_location(&(file_name[start_location]));
		if (location < 0) {
			// Directory has been located.
			result = true;
		}
	}

	*home_directory_cluster = home_dir_cluster;
	if (home_dir_cluster == 0) {
		file_record->file_record_cluster = 0; // Home directory is the root directory.
		result = true;
	}
	return result;
}

bool find_file_in_directory(int directory_start_cluster, char *file_name,
		t_file_record *file_record)
// Given a cluster and a file name, check if the file already exists. Return the file record if the file is found.
{
	int location = get_dir_divider_location(file_name);
	int last_dir_separator = 0;
	char name[8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
	char extension[3] = { ' ', ' ', ' ' };
	int ext_index = -1;
	int cluster = directory_start_cluster;
	int index;
	int length = strlen(file_name);
	bool result = false;

	// Skip through all directory separators.
	while (location > 0) {
		last_dir_separator = last_dir_separator + location + 1;
		location = get_dir_divider_location(&(file_name[last_dir_separator]));
	}

	// Get the name of the file in name/extension format.
	for (index = last_dir_separator; index < length; index++) {
		if (file_name[index] == '.') {
			ext_index = index;
		} else if (ext_index < 0) {
			name[index - last_dir_separator] = file_name[index];
		} else {
			extension[index - ext_index - 1] = file_name[index];
		}
	}

	// Look for the file.
	if (directory_start_cluster == 0) {
		/* We are in the root directory. Scan the directory (of predefined size) and see if you can find the specified file. */
		int max_root_dir_sectors = ((32
				* boot_sector_data.max_number_of_dir_entires)
				/ boot_sector_data.sector_size_in_bytes);
		int sector_index;

		for (sector_index = 0; sector_index < max_root_dir_sectors;
				sector_index++) {
			if (Read_Sector_Data(
					sector_index
							+ boot_sector_data.root_directory_sector_offset,
					fat_partition_offset_in_512_byte_sectors)) {
				int file_counter;

				for (file_counter = 0; file_counter < 16; file_counter++) {
					// Read file record.
					Read_File_Record_At_Offset(file_counter * 32, file_record,
							0, sector_index);
					if ((file_record->name[0] != 0xe5)
							&& (file_record->name[0] != 0x00)) {
						bool match = match_file_record_to_name_ext(file_record,
								name, extension);

						if (match) {
							result = true;
							break;
						}
					}
				}
			} else {
				break;
			}
			if (result) {
				break;
			}
		}
	} else {
		do {
			int start_sector = (cluster - 2)
					* (boot_sector_data.sectors_per_cluster)
					+ boot_sector_data.data_sector_offset;
			int sector_index;

			for (sector_index = 0;
					sector_index < boot_sector_data.sectors_per_cluster;
					sector_index++) {
				if (Read_Sector_Data(sector_index + start_sector,
						fat_partition_offset_in_512_byte_sectors)) {
					int file_counter;

					for (file_counter = 0; file_counter < 16; file_counter++) {
						// Read file record.
						Read_File_Record_At_Offset(file_counter * 32,
								file_record, cluster, sector_index);
						if ((file_record->name[0] != 0xe5)
								&& (file_record->name[0] != 0x00)) {
							bool match = match_file_record_to_name_ext(
									file_record, name, extension);

							if (match) {
								result = true;
								break;
							}
						}
					}
				} else {
					break;
				}
				if (result) {
					break;
				}
			}
			// If this is the end of the cluster and the file has not been found, then see if there is another cluster
			// that holds data for the current directory.
			if (result == false) {
				unsigned short int new_cluster;

				if (get_cluster_flag(cluster, &new_cluster)) {
					// The directory needs to be expanded to store more files.
					if ((new_cluster & 0x0000fff8) == 0x0000fff8) {
						return false;
					}
					cluster = (new_cluster & 0x0000fff8);
				} else {
					// Directory path is invalid.
					return false;
				}
			}
		} while ((cluster < 0x0000fff8) && (result == false));
	}

	return result;
}

bool find_first_empty_cluster(unsigned int *cluster_number)
// Find the first empty cluster. It will be marked by a 0 entry in the File Allocation Table.
{
	unsigned int sector = boot_sector_data.first_fat_sector_offset;
	unsigned int cluster_index = 2;
	short int cluster = -1;
	bool result = false;
	unsigned max_cluster_index = 0;
	unsigned int non_data_sectors = boot_sector_data.data_sector_offset;
	unsigned int less_than_32 = boot_sector_data.number_of_sectors_in_partition;
	unsigned int greater_than_32 =
			boot_sector_data.total_sector_count_if_above_32MB;

	if (less_than_32 > greater_than_32) {
		max_cluster_index = ((less_than_32 - non_data_sectors)
				/ boot_sector_data.sectors_per_cluster) + 1;
	} else {
		max_cluster_index = ((greater_than_32 - non_data_sectors)
				/ boot_sector_data.sectors_per_cluster) + 1;
	}
	// Find an empty cluster for the file.
	while (sector != boot_sector_data.second_fat_sector_offset) {
		if (Read_Sector_Data(sector,
				fat_partition_offset_in_512_byte_sectors)) {
			do {
				cluster =
						((unsigned short int) IORD_16DIRECT(device_pointer->base, 2*(cluster_index % 256)));
				if (cluster == 0) {
					// Free cluster found.
					break;
				} else {
					cluster_index++;
				}
			} while ((cluster_index % 256) != 0);
		}
		if (cluster == 0) {
			break;
		}
		sector++;
	}
	if ((cluster == 0) && (cluster <= max_cluster_index)) {
		*cluster_number = cluster_index;
		result = true;
	}
	return result;
}

int find_first_empty_record_in_a_subdirectory(int start_cluster_index)
// Search for a free spot in a subdirectory. Return an encoded location for the file record.
{
	int result = -1;
	int cluster = start_cluster_index;
	do {
		int start_sector = (cluster - 2)
				* (boot_sector_data.sectors_per_cluster)
				+ boot_sector_data.data_sector_offset;
		int sector_index;

		for (sector_index = 0;
				sector_index < boot_sector_data.sectors_per_cluster;
				sector_index++) {
			if (Read_Sector_Data(sector_index + start_sector,
					fat_partition_offset_in_512_byte_sectors)) {
				int file_counter;

				for (file_counter = 0; file_counter < 16; file_counter++) {
					unsigned short int leading_char;

					// Read file record.
					leading_char =
							((unsigned char) IORD_8DIRECT(device_pointer->base, file_counter*32));
					if ((leading_char == 0x00e5) || (leading_char == 0)) {
						result = (cluster)
								| ((sector_index * 16 + file_counter) << 16);
						return result;
					}
				}
			} else {
				break;
			}
		}
		// If this is the end of the cluster and the file has not been found, then see if there is another cluster
		// that holds data for the current directory.
		if (result < 0) {
			unsigned short int new_cluster;
			if (get_cluster_flag(cluster, &new_cluster)) {
				// The directory needs to be expanded to store more files.
				if ((new_cluster & 0x0000fff8) == 0x0000fff8) {
					int new_dir_cluster;
					if (find_first_empty_cluster(&new_dir_cluster)) {
						// Add the new cluster to the linked list of the given directory.
						if (mark_cluster(cluster,
								((short int) (new_dir_cluster)), true)
								&& mark_cluster(new_dir_cluster,
										((short int) (0xffff)), true)
								&& mark_cluster(cluster,
										((short int) (new_dir_cluster)), false)
								&& mark_cluster(new_dir_cluster,
										((short int) (0xffff)), false)) {
							Save_Modified_Sector();
							// The new file will begin at the first entry of the directory.
							result = new_dir_cluster;
						}
					}
					cluster = (new_cluster & 0x0000fff8);
				}
			} else {
				// Error encountered.
				result = -1;
			}
		}
	} while ((cluster < 0x0000fff8) && (result == -1));
	return result;
}

int find_first_empty_record_in_root_directory()
// Find a first unused record location to use. Return -1 if none is found.
{
	int max_root_dir_sectors =
			((32 * boot_sector_data.max_number_of_dir_entires)
					/ boot_sector_data.sector_size_in_bytes);
	int sector_index;
	int result = -1;

	for (sector_index = 0; sector_index < max_root_dir_sectors;
			sector_index++) {
		if (Read_Sector_Data(
				sector_index + boot_sector_data.root_directory_sector_offset,
				fat_partition_offset_in_512_byte_sectors)) {
			int file_counter;

			for (file_counter = 0; file_counter < 16; file_counter++) {
				unsigned short int leading_char;

				// Read first character of the file record.
				leading_char =
						((unsigned char) IORD_8DIRECT(device_pointer->base, file_counter*32));
				if ((leading_char == 0x00e5) || (leading_char == 0)) {
					result = (sector_index * 16 + file_counter) << 16;
					return result;
				}
			}
		} else {
			break;
		}
	}
	return result;
}

void convert_filename_to_name_extension(char *filename, char *name,
		char *extension)
// This function converts the file name into a name . extension format.
{
	int counter;
	int local = 0;

	for (counter = 0; counter < 8; counter++) {
		if (filename[local] != '.') {
			name[counter] = filename[local];
			if (filename[local] != 0)
				local++;
		} else {
			name[counter] = ' ';
		}
	}
	if (filename[local] == '.')
		local++;
	for (counter = 0; counter < 3; counter++) {
		if (filename[local] != 0) {
			extension[counter] = filename[local];
			local++;
		} else {
			extension[counter] = ' ';
		}
	}

}

bool create_file(char *name, t_file_record *file_record,
		t_file_record *home_dir)
// Create a file in a given directory. Expand the directory if needed.
{
	unsigned int cluster_number;
	bool result = false;

	if (find_first_empty_cluster(&cluster_number)) {
		int record_index;

		if (home_dir->file_record_cluster == 0) {
			// Put a file in the root directory.
			record_index = find_first_empty_record_in_root_directory();
		} else {
			// Put a file in a subdirectory.
			record_index = find_first_empty_record_in_a_subdirectory(
					home_dir->start_cluster_index);
		}
		if (record_index >= 0) {
			unsigned int file_record_sector;
			int location = get_dir_divider_location(name);
			int last_dir_separator = 0;

			// Skip through all directory separators.
			while (location > 0) {
				last_dir_separator = last_dir_separator + location + 1;
				location = get_dir_divider_location(
						&(name[last_dir_separator]));
			}

			convert_filename_to_name_extension(&(name[last_dir_separator]),
					file_record->name, file_record->extension);

			file_record->attributes = 0;
			file_record->create_time = 0;
			file_record->create_date = 0;
			file_record->last_access_date = 0;
			file_record->last_modified_time = 0;
			file_record->last_modified_date = 0;
			file_record->start_cluster_index = cluster_number;
			file_record->file_size_in_bytes = 0;
			file_record->current_cluster_index = cluster_number;
			file_record->current_sector_in_cluster = 0;
			file_record->current_byte_position = 0;
			file_record->file_record_cluster = record_index & 0x0000ffff;
			file_record->file_record_sector_in_cluster = ((record_index >> 16)
					& 0x0000ffff) / 16;
			file_record->file_record_offset = (((record_index >> 16)
					& 0x0000ffff) % 16) * 32;
			file_record->home_directory_cluster = home_dir->start_cluster_index;
			file_record->in_use = true;
			file_record->modified = true;
			// Now write the record at the specified location.
			file_record_sector =
					(file_record->file_record_cluster == 0) ?
							(boot_sector_data.root_directory_sector_offset
									+ file_record->file_record_sector_in_cluster) :
							(boot_sector_data.data_sector_offset
									+ (file_record->file_record_cluster - 2)
											* boot_sector_data.sectors_per_cluster
									+ file_record->file_record_sector_in_cluster);

			if (Read_Sector_Data(file_record_sector,
					fat_partition_offset_in_512_byte_sectors)) {
				if (Write_File_Record_At_Offset(file_record->file_record_offset,
						file_record)) {
					Save_Modified_Sector();
					// Mark the first cluster of the file as the last cluster at first.
					mark_cluster(cluster_number, ((short int) (0xffff)), true);
					if (mark_cluster(cluster_number, ((short int) (0xffff)),
							false)) {
						result = true;
					}
				}
			}
		}

	}
	return result;
}

void copy_file_record_name_to_string(t_file_record *file_record,
		char *file_name)
/* Copy a file name from the file record to a given string */
{
	int index;
	int flength = 0;

	/* Copy file name.*/
	for (index = 0; index < 8; index++) {
		if (file_record->name[index] != ' ') {
			file_name[flength] = file_record->name[index];
			flength = flength + 1;
		}
	}
	if (file_record->extension[0] != ' ') {
		file_name[flength] = '.';
		flength = flength + 1;
		for (index = 0; index < 3; index++) {
			if (file_record->extension[index] != ' ') {
				file_name[flength] = file_record->extension[index];
				flength = flength + 1;
			}
		}
	}
	file_name[flength] = 0;
}

///////////////////////////////////////////////////////////////////////////
// Direct functions
///////////////////////////////////////////////////////////////////////////

alt_up_sd_card_dev* alt_up_sd_card_open_dev(const char* name) {
	// find the device from the device list
	// (see altera_hal/HAL/inc/priv/alt_file.h
	// and altera_hal/HAL/src/alt_find_dev.c
	// for details)
	alt_up_sd_card_dev *dev = (alt_up_sd_card_dev *) alt_find_dev(name,
			&alt_dev_list);

	if (dev != NULL) {
		aux_status_register = ((short int *) SD_CARD_AUX_STATUS(dev->base));
		status_register = ((int *) SD_CARD_STATUS(dev->base));
		CSD_register_w0 = ((short int *) SD_CARD_CSD(dev->base, 0));
		command_register = ((short int *) SD_CARD_COMMAND(dev->base));
		command_argument_register = ((int *) SD_CARD_ARGUMENT(dev->base));
		buffer_memory = (char *) SD_CARD_BUFFER(dev->base, 0);
		device_pointer = dev;
		initialized = false;
		is_sd_card_formated_as_FAT16 = false;
		search_data.valid = false;
	}
	return dev;
}

bool alt_up_sd_card_is_Present(void)
// Check if there is an SD Card insterted into the SD Card socket.
{
	bool result = false;

	if ((device_pointer != NULL)
			&& ((IORD_16DIRECT(aux_status_register,0) & 0x02) != 0)) {
		result = true;
	} else if (initialized == true) {
		int index;

		initialized = false;
		search_data.valid = false;
		is_sd_card_formated_as_FAT16 = false;

		for (index = 0; index < MAX_FILES_OPENED; index++) {
			active_files[index].in_use = false;
			active_files[index].modified = false;
		}
	}
	return result;
}

bool alt_up_sd_card_is_FAT16(void)
/* This function reads the SD card data in an effort to determine if the card is formated as a FAT16
 * volume. Please note that FAT12 has a similar format, but will not be supported by this driver.
 * If the card contains a FAT16 volume, the local data structures will be initialized to allow reading and writing
 * to the SD card as though it was a hard drive.
 */
{
	bool result = false;

	if (alt_up_sd_card_is_Present()) {
		// Check if an SD Card is in the SD Card slot.
		if (initialized == false) {
			// Now determine if the card is formatted as FAT 16.
			is_sd_card_formated_as_FAT16 = Look_for_FAT16();
			initialized = is_sd_card_formated_as_FAT16;
			search_data.valid = false;
		}
		result = is_sd_card_formated_as_FAT16;
	} else {
		// If not then you may as well not open the device.
		initialized = false;
		is_sd_card_formated_as_FAT16 = false;
	}

	return result;
}

short int alt_up_sd_card_find_first(char *directory_to_search_through,
		char *file_name)
/* This function sets up a search algorithm to go through a given directory looking for files.
 * If the search directory is valid, then the function searches for the first file it finds.
 * Inputs:
 *		directory_to_search_through - name of the directory to search through
 *		file_name - an array to store a name of the file found. Must be 13 bytes long (12 bytes for file name and 1 byte of NULL termination).
 * Outputs:
 *		0 - success
 *		1 - invalid directory
 *		2 - No card or incorrect card format.
 *
 * To specify a directory give the name in a format consistent with the following regular expression:
 * [{[valid_chars]+}/]*.
 *
 * In other words, give a path name starting at the root directory, where each directory name is followed by a '/'.
 * Then, append a '.' to the directory name. Examples:
 * "." - look through the root directory
 * "first/." - look through a directory named "first" that is located in the root directory.
 * "first/sub/." - look through a directory named "sub", that is located within the subdirectory named "first". "first" is located in the root directory.
 * Invalid examples include:
 * "/.", "/////." - this is not the root directory.
 * "/first/." - the first character may not be a '/'.
 */
{
	short int result = 2;
	if ((alt_up_sd_card_is_Present()) && (is_sd_card_formated_as_FAT16)) {
		int home_directory_cluster;
		t_file_record file_record;

		if (get_home_directory_cluster_for_file(directory_to_search_through,
				&home_directory_cluster, &file_record)) {
			search_data.directory_root_cluster = home_directory_cluster;
			search_data.current_cluster_index = home_directory_cluster;
			search_data.current_sector_in_cluster = 0;
			search_data.file_index_in_sector = -1;
			search_data.valid = true;
			result = alt_up_sd_card_find_next(file_name);
		} else {
			result = 1;
		}
	}
	return result;
}

short int alt_up_sd_card_find_next(char *file_name)
/* This function searches for the next file in a given directory, as specified by the find_first function.
 * Inputs:
 *		file_name - an array to store a name of the file found. Must be 13 bytes long (12 bytes for file name and 1 byte of NULL termination).
 * Outputs:
 *		-1 - end of directory.
 *		0 - success
 *		2 - No card or incorrect card format.
 *		3 - find_first has not been called successfully.
 */
{
	short int result = 2;
	if ((alt_up_sd_card_is_Present()) && (is_sd_card_formated_as_FAT16)) {
		if (search_data.valid) {
			t_file_record file_record;
			int cluster = search_data.current_cluster_index;

			if (cluster == 0) {
				// Searching through the root directory
				int max_root_dir_sectors = ((32
						* boot_sector_data.max_number_of_dir_entires)
						/ boot_sector_data.sector_size_in_bytes);
				int sector_index = search_data.current_sector_in_cluster;
				int file_counter = search_data.file_index_in_sector + 1;

				for (; sector_index < max_root_dir_sectors; sector_index++) {
					if (Read_Sector_Data(
							sector_index
									+ boot_sector_data.root_directory_sector_offset,
							fat_partition_offset_in_512_byte_sectors)) {
						for (; file_counter < 16; file_counter++) {
							if (Read_File_Record_At_Offset(file_counter * 32,
									&file_record, 0, sector_index)) {
								if ((file_record.name[0] != 0)
										&& (file_record.name[0] != 0xe5)) {
									/* Update search structure. */
									search_data.file_index_in_sector =
											file_counter;
									search_data.current_sector_in_cluster =
											sector_index;

									/* Copy file name.*/
									copy_file_record_name_to_string(
											&file_record, file_name);
									return 0;
								}
							}
						}
						file_counter = 0;
					} else {
						break;
					}
				}
				result = -1;
			} else {
				int file_counter = search_data.file_index_in_sector + 1;
				do {
					int start_sector = (cluster - 2)
							* (boot_sector_data.sectors_per_cluster)
							+ boot_sector_data.data_sector_offset;
					int sector_index = search_data.current_sector_in_cluster;

					for (; sector_index < boot_sector_data.sectors_per_cluster;
							sector_index++) {
						if (Read_Sector_Data(sector_index + start_sector,
								fat_partition_offset_in_512_byte_sectors)) {
							for (; file_counter < 16; file_counter++) {
								if (Read_File_Record_At_Offset(
										file_counter * 32, &file_record,
										cluster, sector_index)) {
									if ((file_record.name[0] != 0)
											&& (file_record.name[0] != 0xe5)) {
										/* Update search structure. */
										search_data.current_cluster_index =
												cluster;
										search_data.file_index_in_sector =
												file_counter;
										search_data.current_sector_in_cluster =
												sector_index;

										/* Copy file name.*/
										copy_file_record_name_to_string(
												&file_record, file_name);
										return 0;
									}
								}
							}
							file_counter = 0;
						} else {
							break;
						}
					}
					// If this is the end of the cluster and the file has not been found, then see if there is another cluster
					// that holds data for the current directory.
					if (sector_index >= boot_sector_data.sectors_per_cluster) {
						short int new_cluster;

						if (get_cluster_flag(cluster, &new_cluster)) {
							if ((new_cluster & 0x0000fff8) == 0x0000fff8) {
								result = -1;
								search_data.valid = false;
							}
							cluster = ((new_cluster) & 0x0000fff8);
						} else {
							// Error encountered.
							result = -1;
						}
					}
				} while (cluster < 0x0000fff8);
			}
		} else {
			// Call Find_First first.
			result = 3;
		}
	}
	return result;
}

short int alt_up_sd_card_fopen(char *name, bool create)
/* This function reads the SD card data in an effort to determine if the card is formated as a FAT16
 * volume. Please note that FAT12 has a similar format, but will not be supported by this driver.
 *
 * Inputs:
 *      name - a file name including a directory, relative to the root directory
 *      create - a flag set to true to create a file if it does not already exist
 * Output:
 *      An index to the file record assigned to the specified file. -1 is returned if the file could not be opened.
 *		Return -2 if the specified file has already been opened previously.
 */
{
	short int file_record_index = -1;

	if ((alt_up_sd_card_is_Present()) && (is_sd_card_formated_as_FAT16)) {
		unsigned int home_directory_cluster = 0;
		t_file_record home_dir;

		/* First check the file name format. It should not be longer than 12 characters, including a period and the extension.
		 * Rules:
		 *  - no spaces
		 *  - at most 12 chatacters per name, with a period in 9th position.
		 *  - a / or a \ every at most 12 characters.
		 */
		filename_to_upper_case(name);
		if (check_file_name_for_FAT16_compliance(name)) {
			int index;

			/* Get home directory cluster location for the specified file. 0 means root directory. */
			if (!get_home_directory_cluster_for_file(name,
					&home_directory_cluster, &home_dir)) {
				return file_record_index;
			}

			/* Find a free file slot to store file specs in. */
			for (index = 0; index < MAX_FILES_OPENED; index++) {
				if (active_files[index].in_use == false) {
					file_record_index = index;
					break;
				}
			}
			if (file_record_index >= 0) {
				/* If file record is found, then look for the specified file. If the create flag is set to true
				 * and the file is not found, then it should be created in the current directory.
				 */

				if (find_file_in_directory(home_directory_cluster, name,
						&(active_files[file_record_index]))) {
					if (create) {
						/* Do not allow overwriting existing files for now. */
						return -1;
					}
					active_files[file_record_index].current_cluster_index =
							active_files[file_record_index].start_cluster_index;
					active_files[file_record_index].current_sector_in_cluster =
							0;
					active_files[file_record_index].current_byte_position = 0;
					active_files[file_record_index].in_use = true;
					active_files[file_record_index].modified = false;

					/* Check if the file has already been opened. */
					for (index = 0; index < MAX_FILES_OPENED; index++) {
						if ((file_record_index != index)
								&& (active_files[index].in_use == true)) {
							if ((active_files[file_record_index].file_record_cluster
									== active_files[index].file_record_cluster)
									&& (active_files[file_record_index].file_record_sector_in_cluster
											== active_files[index].file_record_sector_in_cluster)
									&& (active_files[file_record_index].file_record_offset
											== active_files[index].file_record_offset)) {
								// file already in use.
								file_record_index = -2;
								break;
							}
						}
					}

				} else if (create) {
					/* Create file if needed. */
					if (create_file(name, &(active_files[file_record_index]),
							&home_dir)) {
						active_files[file_record_index].in_use = true;
						active_files[file_record_index].modified = true;
					} else {
						/* If file creation fails then return an invalid file handle. */
						file_record_index = -1;
					}
				} else {
					/* Otherwise the file could not be opened.*/
					file_record_index = -1;
				}
			}
		}
	}

	return file_record_index;
}

void alt_up_sd_card_set_attributes(short int file_handle, short int attributes)
/* Return file attributes, or -1 if the file_handle is invalid.
 */
{
	if ((file_handle >= 0) && (file_handle < MAX_FILES_OPENED)) {
		if (active_files[file_handle].in_use) {
			active_files[file_handle].attributes =
					((char) (attributes & 0x00ff));
		}
	}
}

short int alt_up_sd_card_get_attributes(short int file_handle)
/* Return file attributes, or -1 if the file_handle is invalid.
 */
{
	short int result = -1;
	if ((file_handle >= 0) && (file_handle < MAX_FILES_OPENED)) {
		if (active_files[file_handle].in_use) {
			result = ((active_files[file_handle].attributes) & 0x00ff);
		}
	}
	return result;
}

short int alt_up_sd_card_read(short int file_handle)
/* Read a single character from a given file. Return -1 if at the end of a file. Any other negative number
 * means that the file could not be read. A number between 0 and 255 is an ASCII character read from the SD Card. */
{
	short int ch = -1;

	if ((file_handle >= 0) && (file_handle < MAX_FILES_OPENED)) {
		if (active_files[file_handle].in_use) {
			if (active_files[file_handle].current_byte_position
					< active_files[file_handle].file_size_in_bytes) {
				int data_sector = boot_sector_data.data_sector_offset
						+ (active_files[file_handle].current_cluster_index - 2)
								* boot_sector_data.sectors_per_cluster
						+ active_files[file_handle].current_sector_in_cluster;

				if ((active_files[file_handle].current_byte_position > 0)
						&& ((active_files[file_handle].current_byte_position
								% 512) == 0)) {
					// Read in a new sector of data.
					if (active_files[file_handle].current_sector_in_cluster
							== boot_sector_data.sectors_per_cluster - 1) {
						// Go to the next cluster.
						unsigned short int next_cluster;
						if (get_cluster_flag(
								active_files[file_handle].current_cluster_index,
								&next_cluster)) {
							if ((next_cluster & 0x0000fff8) == 0x0000fff8) {
								/* End of file */
								return -1;
							} else {
								active_files[file_handle].current_cluster_index =
										next_cluster;
								active_files[file_handle].current_sector_in_cluster =
										0;
								data_sector =
										boot_sector_data.data_sector_offset
												+ (active_files[file_handle].current_cluster_index
														- 2)
														* boot_sector_data.sectors_per_cluster
												+ active_files[file_handle].current_sector_in_cluster;
							}
						} else {
							return -2;
						}
					} else {
						active_files[file_handle].current_sector_in_cluster =
								active_files[file_handle].current_sector_in_cluster
										+ 1;
						data_sector = data_sector + 1;
					}
				}
				// Reading te first byte of the file.
				if (current_sector_index
						!= (data_sector
								+ fat_partition_offset_in_512_byte_sectors)) {
					if (!Read_Sector_Data(data_sector,
							fat_partition_offset_in_512_byte_sectors)) {
						return -2;
					}
				}

				ch =
						(unsigned char) IORD_8DIRECT(buffer_memory, (active_files[file_handle].current_byte_position % 512));
				active_files[file_handle].current_byte_position =
						active_files[file_handle].current_byte_position + 1;
			}
		}
	}

	return ch;
}

bool alt_up_sd_card_write(short int file_handle, char byte_of_data)
/* Write a single character to a given file. Return true if successful, and false otherwise. */
{
	bool result = false;

	if ((file_handle >= 0) && (file_handle < MAX_FILES_OPENED)) {
		if (active_files[file_handle].in_use) {
			int data_sector = boot_sector_data.data_sector_offset
					+ (active_files[file_handle].current_cluster_index - 2)
							* boot_sector_data.sectors_per_cluster
					+ active_files[file_handle].current_sector_in_cluster;
			short int buffer_offset =
					active_files[file_handle].current_byte_position
							% boot_sector_data.sector_size_in_bytes;

			if (active_files[file_handle].current_byte_position
					< active_files[file_handle].file_size_in_bytes) {
				if ((active_files[file_handle].current_byte_position > 0)
						&& (buffer_offset == 0)) {
					// Read in a new sector of data.
					if (active_files[file_handle].current_sector_in_cluster
							== boot_sector_data.sectors_per_cluster - 1) {
						// Go to the next cluster.
						unsigned short int next_cluster;
						if (get_cluster_flag(
								active_files[file_handle].current_cluster_index,
								&next_cluster)) {
							if (next_cluster < 0x0000fff8) {
								active_files[file_handle].current_cluster_index =
										next_cluster;
								active_files[file_handle].current_sector_in_cluster =
										0;
								data_sector =
										boot_sector_data.data_sector_offset
												+ (active_files[file_handle].current_cluster_index
														- 2)
														* boot_sector_data.sectors_per_cluster
												+ active_files[file_handle].current_sector_in_cluster;
							}
						} else {
							return false;
						}
					} else {
						active_files[file_handle].current_sector_in_cluster
								= active_files[file_handle].current_sector_in_cluster
										+ 1;
						data_sector = data_sector + 1;
					}
				}
			} else {
				/* You are adding data to the end of the file, so increment its size and look for an additional data cluster if needed. */
				if ((active_files[file_handle].current_byte_position > 0)
						&& (buffer_offset == 0)) {
					if (active_files[file_handle].current_sector_in_cluster
							== boot_sector_data.sectors_per_cluster - 1) {
						/* Find a new cluster if possible. */
						unsigned int cluster_number;

						if (find_first_empty_cluster(&cluster_number)) {
							// mark clusters in both File Allocation Tables.
							mark_cluster(
									active_files[file_handle].current_cluster_index,
									((unsigned short int) (cluster_number
											& 0x0000ffff)), true);
							mark_cluster(cluster_number, 0xffff, true);
							mark_cluster(
									active_files[file_handle].current_cluster_index,
									((unsigned short int) (cluster_number
											& 0x0000ffff)), false);
							mark_cluster(cluster_number, 0xffff, false);
							// Change cluster index and sector index to compute a new data sector.
							active_files[file_handle].current_cluster_index =
									cluster_number;
							active_files[file_handle].current_sector_in_cluster =
									0;
						} else {
							return false;
						}
					} else {
						/* Read the next sector in the cluster and modify it. We only need to change the data_sector value. The actual read happens a few lines below. */
						active_files[file_handle].current_sector_in_cluster =
								(active_files[file_handle].current_byte_position
										/ boot_sector_data.sector_size_in_bytes) % boot_sector_data.sectors_per_cluster;
					}
					data_sector =
							boot_sector_data.data_sector_offset
									+ (active_files[file_handle].current_cluster_index
											- 2)
											* boot_sector_data.sectors_per_cluster
									+ active_files[file_handle].current_sector_in_cluster;
				}
			}
			// Reading a data sector into the buffer. Note that changes to the most recently modified sector will be saved before
			// a new sector is read from the SD Card.
			if (current_sector_index
					!= data_sector + fat_partition_offset_in_512_byte_sectors) {
				if (!Read_Sector_Data_Attempt_CRC(data_sector,
						fat_partition_offset_in_512_byte_sectors)) {
					return false;
				}
			}
			// Write a byte of data to the buffer.
			IOWR_8DIRECT(buffer_memory, buffer_offset, byte_of_data);
			active_files[file_handle].current_byte_position =
					active_files[file_handle].current_byte_position + 1;

			// Modify the file record only when necessary.
			if (active_files[file_handle].current_byte_position
					> active_files[file_handle].file_size_in_bytes) {
				active_files[file_handle].file_size_in_bytes =
						active_files[file_handle].file_size_in_bytes + 1;

			}
            active_files[file_handle].modified = true;

			// Invaldiate the buffer to ensure that the buffer contents are written to the SD card whe nthe file is closed.
			current_sector_modified = true;
			result = true;
		}
	}

	return result;
}

bool alt_up_sd_card_fclose(short int file_handle)
// This function closes an opened file and saves data to SD Card if necessary.
{
	assert(file_handle >= 0);

	bool result = false;
	if ((alt_up_sd_card_is_Present()) && (is_sd_card_formated_as_FAT16)) {
		if (active_files[file_handle].in_use) {
			if (active_files[file_handle].modified) {
				unsigned int record_sector =
						active_files[file_handle].file_record_sector_in_cluster;
				if (active_files[file_handle].file_record_cluster == 0) {
					record_sector = record_sector
							+ boot_sector_data.root_directory_sector_offset;
				} else {
					record_sector =
							record_sector + boot_sector_data.data_sector_offset
									+ (active_files[file_handle].file_record_cluster
											- 2)
											* boot_sector_data.sectors_per_cluster;
				}
				if (Read_Sector_Data(record_sector,
						fat_partition_offset_in_512_byte_sectors)) {
					if (Write_File_Record_At_Offset(
							active_files[file_handle].file_record_offset,
							&(active_files[file_handle]))) {
						// Make sure that the Data has been saved to the SD Card.
						result = Save_Modified_Sector();
					}
				}
			}
			active_files[file_handle].in_use = false;
			result = true;
		}
	}

	return result;
}

bool Read_Sector_Data_CRC(int sector_index, int partition_offset)
// This function reads a sector at the specified address on the SD Card.
{
	bool result = false;

	if (alt_up_sd_card_is_Present()) {
		short int reg_state = 0xff;

		/* Write data to the SD card if the current buffer is out of date. */
		if (current_sector_modified) {
			if (Write_Sector_Data(current_sector_index, 0) == false) {
				return false;
			}
		}
		/* Multiply sector offset by sector size to get the address. Sector size is 512. Also,
		 * the SD card reads data in 512 byte chunks, so the address must be a multiple of 512. */
		IOWR_32DIRECT(command_argument_register, 0,
				(sector_index + partition_offset)*512);
		IOWR_16DIRECT(command_register, 0, CMD_READ_BLOCK);
		do {
			reg_state = (short int) IORD_16DIRECT(aux_status_register,0);
		} while ((reg_state & 0x04) != 0);
		// Make sure the request did not time out.
		if ((reg_state & 0x30) == 0) {
			result = true;
			current_sector_modified = false;
			current_sector_index = sector_index + partition_offset;
		}
	}
	return result;
}

bool Read_Sector_Data_Attempt_CRC(int sector_index, int partition_offset) {
	int i;
	bool correct;
	const int attempts = 100;

	for (i = 0; i < attempts; i++) {
		correct = Read_Sector_Data_CRC(sector_index, partition_offset);
		if (correct) {
			return true;
		}
	}
	return Read_Sector_Data(sector_index, partition_offset);
}


text:
package tests.interfaces.conflict;

import main.interfaces.conflict.Named;
import main.interfaces.conflict.OtherNamed;

public class Conflict3 implements Named, OtherNamed {

    // Overloading
    @Override
    public String getName() {
        return null;
    }

    // Overloading
    @Override
    public String getName(StringBuilder builder) {
        return null;
    }

    public static void main(String[] args) {
        System.out.println(new Conflict3().getName());
    }

}

redpajama_set_name: RedPajamaGithub


text:
import 'vs/css!../browser/media/debug.contribution';
import 'vs/css!../browser/media/debugHover';
import * as nls from 'vs/nls';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
import { SyncActionDescriptor } from 'vs/platform/actions/common/actions';
import { Registry } from 'vs/platform/platform';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { KeybindingsRegistry, IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
import { IWorkbenchActionRegistry, Extensions as WorkbenchActionRegistryExtensions } from 'vs/workbench/common/actionRegistry';
import { ToggleViewletAction, Extensions as ViewletExtensions, ViewletRegistry, ViewletDescriptor } from 'vs/workbench/browser/viewlet';
import { TogglePanelAction, Extensions as PanelExtensions, PanelRegistry, PanelDescriptor } from 'vs/workbench/browser/panel';
import { DebugViewRegistry } from 'vs/workbench/parts/debug/browser/debugViewRegistry';
import { VariablesView, WatchExpressionsView, CallStackView, BreakpointsView } from 'vs/workbench/parts/debug/electron-browser/debugViews';
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IDebugService, VIEWLET_ID, REPL_ID, CONTEXT_NOT_IN_DEBUG_MODE, CONTEXT_IN_DEBUG_MODE } from 'vs/workbench/parts/debug/common/debug';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { DebugEditorModelManager } from 'vs/workbench/parts/debug/browser/debugEditorModelManager';
import {
	StepOverAction, ClearReplAction, FocusReplAction, StepIntoAction, StepOutAction, StartAction, RestartAction, ContinueAction, StopAction, DisconnectAction, PauseAction, AddFunctionBreakpointAction,
	ConfigureAction, DisableAllBreakpointsAction, EnableAllBreakpointsAction, RemoveAllBreakpointsAction, RunAction, ReapplyBreakpointsAction, SelectAndStartAction
} from 'vs/workbench/parts/debug/browser/debugActions';
import { DebugActionsWidget } from 'vs/workbench/parts/debug/browser/debugActionsWidget';
import * as service from 'vs/workbench/parts/debug/electron-browser/debugService';
import { DebugContentProvider } from 'vs/workbench/parts/debug/browser/debugContentProvider';
import 'vs/workbench/parts/debug/electron-browser/debugEditorContribution';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import * as debugCommands from 'vs/workbench/parts/debug/electron-browser/debugCommands';
import { IQuickOpenRegistry, Extensions as QuickOpenExtensions, QuickOpenHandlerDescriptor } from 'vs/workbench/browser/quickopen';
import { StatusBarColorProvider } from 'vs/workbench/parts/debug/electron-browser/statusbarColorProvider';

class OpenDebugViewletAction extends ToggleViewletAction {
	public static ID = VIEWLET_ID;
	public static LABEL = nls.localize('toggleDebugViewlet', "Show Debug");

	constructor(
		id: string,
		label: string,
		@IViewletService viewletService: IViewletService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, VIEWLET_ID, viewletService, editorService);
	}
}

class OpenDebugPanelAction extends TogglePanelAction {
	public static ID = 'workbench.debug.action.toggleRepl';
	public static LABEL = nls.localize('toggleDebugPanel', "Debug Console");

	constructor(
		id: string,
		label: string,
		@IPanelService panelService: IPanelService,
		@IPartService partService: IPartService
	) {
		super(id, label, REPL_ID, panelService, partService);
	}
}

// register viewlet
Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets).registerViewlet(new ViewletDescriptor(
	'vs/workbench/parts/debug/browser/debugViewlet',
	'DebugViewlet',
	VIEWLET_ID,
	nls.localize('debug', "Debug"),
	'debug',
	40
));

const openViewletKb: IKeybindings = {
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_D
};
const openPanelKb: IKeybindings = {
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Y
};

// register repl panel
Registry.as<PanelRegistry>(PanelExtensions.Panels).registerPanel(new PanelDescriptor(
	'vs/workbench/parts/debug/electron-browser/repl',
	'Repl',
	REPL_ID,
	nls.localize({ comment: ['Debug is a noun in this context, not a verb.'], key: 'debugPanel' }, 'Debug Console'),
	'repl',
	30,
	OpenDebugPanelAction.ID
));
Registry.as<PanelRegistry>(PanelExtensions.Panels).setDefaultPanelId(REPL_ID);

// Register default debug views
DebugViewRegistry.registerDebugView(VariablesView, 10, 40);
DebugViewRegistry.registerDebugView(WatchExpressionsView, 20, 10);
DebugViewRegistry.registerDebugView(CallStackView, 30, 30);
DebugViewRegistry.registerDebugView(BreakpointsView, 40, 20);

// register action to open viewlet
const registry = Registry.as<IWorkbenchActionRegistry>(WorkbenchActionRegistryExtensions.WorkbenchActions);
registry.registerWorkbenchAction(new SyncActionDescriptor(OpenDebugPanelAction, OpenDebugPanelAction.ID, OpenDebugPanelAction.LABEL, openPanelKb), 'View: Debug Console', nls.localize('view', "View"));
registry.registerWorkbenchAction(new SyncActionDescriptor(OpenDebugViewletAction, OpenDebugViewletAction.ID, OpenDebugViewletAction.LABEL, openViewletKb), 'View: Show Debug', nls.localize('view', "View"));

Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(DebugEditorModelManager);
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(DebugActionsWidget);
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(DebugContentProvider);
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(StatusBarColorProvider);

const debugCategory = nls.localize('debugCategory', "Debug");
registry.registerWorkbenchAction(new SyncActionDescriptor(
	StartAction, StartAction.ID, StartAction.LABEL, { primary: KeyCode.F5 }, CONTEXT_NOT_IN_DEBUG_MODE), 'Debug: Start Debugging', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(StepOverAction, StepOverAction.ID, StepOverAction.LABEL, { primary: KeyCode.F10 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Step Over', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(StepIntoAction, StepIntoAction.ID, StepIntoAction.LABEL, { primary: KeyCode.F11 }, CONTEXT_IN_DEBUG_MODE, KeybindingsRegistry.WEIGHT.workbenchContrib(1)), 'Debug: Step Into', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(StepOutAction, StepOutAction.ID, StepOutAction.LABEL, { primary: KeyMod.Shift | KeyCode.F11 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Step Out', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(RestartAction, RestartAction.ID, RestartAction.LABEL, { primary: KeyMod.Shift | KeyMod.CtrlCmd | KeyCode.F5 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Restart', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(StopAction, StopAction.ID, StopAction.LABEL, { primary: KeyMod.Shift | KeyCode.F5 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Stop', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(DisconnectAction, DisconnectAction.ID, DisconnectAction.LABEL), 'Debug: Disconnect', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(ContinueAction, ContinueAction.ID, ContinueAction.LABEL, { primary: KeyCode.F5 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Continue', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(PauseAction, PauseAction.ID, PauseAction.LABEL, { primary: KeyCode.F6 }, CONTEXT_IN_DEBUG_MODE), 'Debug: Pause', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(ConfigureAction, ConfigureAction.ID, ConfigureAction.LABEL), 'Debug: Open launch.json', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(AddFunctionBreakpointAction, AddFunctionBreakpointAction.ID, AddFunctionBreakpointAction.LABEL), 'Debug: Add Function Breakpoint', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(ReapplyBreakpointsAction, ReapplyBreakpointsAction.ID, ReapplyBreakpointsAction.LABEL), 'Debug: Reapply All Breakpoints', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(RunAction, RunAction.ID, RunAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.F5 }, CONTEXT_NOT_IN_DEBUG_MODE), 'Debug: Start Without Debugging', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(RemoveAllBreakpointsAction, RemoveAllBreakpointsAction.ID, RemoveAllBreakpointsAction.LABEL), 'Debug: Remove All Breakpoints', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(EnableAllBreakpointsAction, EnableAllBreakpointsAction.ID, EnableAllBreakpointsAction.LABEL), 'Debug: Enable All Breakpoints', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(DisableAllBreakpointsAction, DisableAllBreakpointsAction.ID, DisableAllBreakpointsAction.LABEL), 'Debug: Disable All Breakpoints', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(ClearReplAction, ClearReplAction.ID, ClearReplAction.LABEL), 'Debug: Clear Debug Console', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(FocusReplAction, FocusReplAction.ID, FocusReplAction.LABEL), 'Debug: Focus Debug Console', debugCategory);
registry.registerWorkbenchAction(new SyncActionDescriptor(SelectAndStartAction, SelectAndStartAction.ID, SelectAndStartAction.LABEL), 'Debug: Launch Configuration', debugCategory);

// Register Quick Open
(<IQuickOpenRegistry>Registry.as(QuickOpenExtensions.Quickopen)).registerQuickOpenHandler(
	new QuickOpenHandlerDescriptor(
		'vs/workbench/parts/debug/browser/debugQuickOpen',
		'DebugQuickOpenHandler',
		'debug ',
		nls.localize('debugCommands', "Debug Configuration")
	)
);

// register service
registerSingleton(IDebugService, service.DebugService);

// Register configuration
const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
configurationRegistry.registerConfiguration({
	id: 'debug',
	order: 20,
	title: nls.localize('debugConfigurationTitle', "Debug"),
	type: 'object',
	properties: {
		'debug.allowBreakpointsEverywhere': {
			type: 'boolean',
			description: nls.localize({ comment: ['This is the description for a setting'], key: 'allowBreakpointsEverywhere' }, "Allows setting breakpoint in any file"),
			default: false
		},
		'debug.openExplorerOnEnd': {
			type: 'boolean',
			description: nls.localize({ comment: ['This is the description for a setting'], key: 'openExplorerOnEnd' }, "Automatically open explorer view on the end of a debug session"),
			default: false
		},
		'debug.inlineValues': {
			type: 'boolean',
			description: nls.localize({ comment: ['This is the description for a setting'], key: 'inlineValues' }, "Show variable values inline in editor while debugging"),
			default: false
		},
		'debug.hideActionBar': {
			type: 'boolean',
			description: nls.localize({ comment: ['This is the description for a setting'], key: 'hideActionBar' }, "Controls if the floating debug action bar should be hidden"),
			default: false
		},
		'launch': {
			type: 'object',
			description: nls.localize({ comment: ['This is the description for a setting'], key: 'launch' }, "Global debug launch configuration. Should be used as an alternative to 'launch.json' that is shared across workspaces"),
			default: {}
		}
	}
});

debugCommands.registerCommands();


text:
  <% if session['oauth'][:access_token] %>
    facebook login
  <% end %>

<% @facebook_user = User.find_by(profile_picture: @json["id"])%>

<%
require "json"
require 'open-uri'
require "net/http"
require "net/https"
require "cgi"

before do
  @client_id = ENV['FB_CLIENT_ID']
  @client_secret = ENV['FB_CLIENT_SECRET']
  @host = ENV['HOST']

  session[:oauth] ||= {}
end

get "/facebook" do
  if session[:oauth][:access_token].nil?
    redirect '/request'
  else
    # Get user's info from facebook with their access_token
    http = Net::HTTP.new "graph.facebook.com", 443
    request = Net::HTTP::Get.new "/me?access_token=#{session[:oauth][:access_token]}"
    http.use_ssl = true
    response = http.request request
    url = "https://graph.facebook.com/me?fields=id,name,email&access_token=#{session[:oauth][:access_token]}"
    fb_user_info = JSON.parse(open(url).read)
    puts "="*100+"\n fb_user_info:\n"
    puts fb_user_info
    # Find or create user in app's DB
    User.find_or_create_by(username: fb_user_info['name'], fb_id: fb_user_info['id'])
    user_login(User.find_by(fb_id: fb_user_info['id']))
    redirect '/'
  end
end

get "/request" do
  redirect "https://graph.facebook.com/oauth/authorize?client_id=#{@client_id}&redirect_uri=http://#{@host}/fbcallback"
end

get "/fbcallback" do
  session[:oauth][:code] = params[:code]

  http = Net::HTTP.new "graph.facebook.com", 443
  request = Net::HTTP::Get.new "/oauth/access_token?client_id=#{@client_id}&redirect_uri=http://#{@host}/fbcallback&client_secret=#{@client_secret}&code=#{session[:oauth][:code]}"
  http.use_ssl = true
  response = http.request request

  session[:oauth][:access_token] = CGI.parse(response.body)["access_token"][0]

  redirect "/facebook"
end

get "/logout" do
  session.clear
  redirect "/"
end


text:
@interface DBSession (iOS)

+ (NSDictionary*)parseURLParams:(NSString *)query;

- (NSString *)appScheme;

- (void)linkFromController:(UIViewController *)rootController;
- (void)linkUserId:(NSString *)userId fromController:(UIViewController *)rootController;

- (void)linkFromController:(UIViewController *)rootController presentationHandler:(void(^)(UIViewController *viewController))presentationHandler dismissalHandler:(void(^)(BOOL success, UIViewController *viewController))dismissalHandler;
- (void)linkUserId:(NSString *)userId fromController:(UIViewController *)rootController presentationHandler:(void(^)(UIViewController *viewController))presentationHandler dismissalHandler:(void(^)(BOOL success, UIViewController *viewController))dismissalHandler;

- (BOOL)handleOpenURL:(NSURL *)url linkedUserId:(NSString **)userId;
- (BOOL)handleOpenURL:(NSURL *)url;

@end


text:
{% load staticfiles %}

{% if 'reportSummary' in cards %}

            {{ reports.first.name }}

            <button type="button" class="btn btn-sm btn-primary report-view-btn">

                Report View
            </button>

            <button type="button" class="btn btn-sm btn-primary gene-view-btn">

                Gene View
            </button>

            <button type="button" class="btn btn-sm btn-primary variant-view-btn">

                Variant View
            </button>

{% endif %}

{% if 'reportMultiple' in cards %}

            Summary for Multiple Reports

Reports in this summary: {{ reports_list_string }}

            <button type="button" class="btn btn-sm btn-primary report-view-btn">

                Report View
            </button>

            <button type="button" class="btn btn-sm btn-primary gene-view-btn">

                Gene View
            </button>

            <button type="button" class="btn btn-sm btn-primary variant-view-btn">

                Variant View
            </button>

{% endif %}

{% if 'geneList' in cards %}

            Gene List

            {% for one in gene_list %}
            {% if forloop.counter|divisibleby:4 %}

            {% endif %}

                    {{ one }}

            {% if forloop.counter|divisibleby:4 %}

            {% endif %}
            {% endfor %}

{% endif %}

{% if 'geneProfile' in cards %}

            Gene Profile for {{ gene.name }}

            Number of {{ gene.name }} in this report: {{ gene.count }}

            {{ gene.mutations }}

{% endif %}

{% if 'studyDescription' in cards %}

            Study Description

{{ reports.first.study.name }}

{{ reports.first.study.description }}

{% endif %}

{% if 'scrap' in cards %}

Content

{% endif %}

{% if 'samplesDescription' in cards %}

            Samples in {{ reports.first.name }}

            {% for sample in reports.first.study.sample_set.all %}

{{ sample }}
{{ sample.description }}

            {% endfor %}

{% endif %}

{% if 'sample1' in cards %}

            These mountains are nice to look at

            It makes for a nice visual effect

Centered Content

{% endif %}

{% if 'sample2' in cards %}

            This is the Genome

{{ reports.0.genome.name }}

{% endif %}

{% if 'sample3' in cards %}

            This is just content, no header

{% endif %}

{% if 'refalleleratio' in cards %}

Reference Allele Ratio

{% endif %}

{% if 'effectratio' in cards %}

Effect Ratio

{% endif %}

{% if 'topfivegenes' in cards %}

Top 5 Genes

{#        #}

{% endif %}

{% if 'qualitative_map' in cards %}

Variants in Genes of Interest

{% endif %}

{% if 'miguelisafunnyguy' in cards %}

Miguel is fun!

            But is he really?

{% endif %}


text:
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

#include "pch.h"
#include "Scenario1_ReadData.xaml.h"

using namespace SDKTemplate;

using namespace concurrency;
using namespace Platform;
using namespace Windows::Devices::I2c;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

Scenario1_ReadData::Scenario1_ReadData()
{
    InitializeComponent();
}

void Scenario1_ReadData::OnNavigatedFrom(NavigationEventArgs^ e)
{
    StopScenario();
}

task<void> Scenario1_ReadData::StartScenarioAsync()
{
    String^ i2cDeviceSelector = I2cDevice::GetDeviceSelector();

    return create_task(DeviceInformation::FindAllAsync(i2cDeviceSelector))
        .then([this](DeviceInformationCollection^ devices) {

        // 0x40 was determined by looking at the datasheet for the HTU21D sensor
        auto HTU21D_settings = ref new I2cConnectionSettings(0x40);

        // If this next line crashes with an OutOfBoundsException,
        // then the problem is that no I2C devices were found.
        //
        // If the next line crashes with Access Denied, then the problem is
        // that access to the I2C device (HTU21D) is denied.
        //
        // The call to FromIdAsync will also crash if the settings are invalid.
        return I2cDevice::FromIdAsync(devices->GetAt(0)->Id, HTU21D_settings);
    }).then([this](I2cDevice^ i2cDevice) {

        // i2cDevice will be nullptr if there is a sharing violation on the device.
        // This will result in an access violation in Timer_Tick below.
        htu21dSensor = i2cDevice;

        // Start the polling timer.
        timer = ref new DispatcherTimer();
        timer->Interval = TimeSpan{ 500 * 1000 * 10 }; // 500ms
        timerToken = timer->Tick += ref new EventHandler<Object^>(this, &SDKTemplate::Scenario1_ReadData::Timer_Tick);
        timer->Start();
    });
}

void Scenario1_ReadData::StopScenario()
{
    if (timer != nullptr)
    {
        timer->Tick -= timerToken;
        timer->Stop();
        timer = nullptr;
    }

    // Release the I2C sensor.
    delete htu21dSensor;
    htu21dSensor = nullptr;
}

void Scenario1_ReadData::StartStopScenario()
{
    if (timer != nullptr)
    {
        StopScenario();
        StartStopButton->Content = "Start";
        ScenarioControls->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
    else
    {
        StartStopButton->IsEnabled = false;
        StartScenarioAsync().then([this]()
        {
            StartStopButton->IsEnabled = true;
            StartStopButton->Content = "Stop";
            ScenarioControls->Visibility = Windows::UI::Xaml::Visibility::Visible;
        });
    }
}

void Scenario1_ReadData::Timer_Tick(Object^ sender, Object^ e)
{
    // Read data from I2C.
    auto command = ref new Array<byte>(1);
    auto humidityData = ref new Array<byte>(2);
    auto temperatureData = ref new Array<byte>(2);

    // Read humidity.
    command[0] = 0xE5;

    // If this next line crashes with an access violation reading location 0x00000000,
    // then there was a sharing violation on the device. (See StartScenarioAsync above.)
    //
    // If this next line crashes for some other reason, then there was
    // an error accessing the sensor.
    htu21dSensor->WriteRead(command, humidityData);

    // Read temperature.
    command[0] = 0xE3;
    // If this next line crashes, then there was an error accessing the sensor.
    htu21dSensor->WriteRead(command, temperatureData);

    // Calculate and report humidity.
    auto rawReadingH = humidityData[0] << 8 | humidityData[1];
    auto ratioH = rawReadingH / (float)65536;
    double humidity = -6 + (125 * ratioH);
    CurrentHumidity->Text = humidity.ToString();

    //Calculate and report temperature.
    auto rawReadingT = temperatureData[0] << 8 | temperatureData[1];
    auto ratioT = rawReadingT / (float)65536;
    double temperature = (-46.85 + (175.72 * ratioT)) * 9 / 5 + 32;
    CurrentTemp->Text = temperature.ToString();
}


text:
using System.Collections.Generic;
using Azure.Core;

namespace Azure.ResourceManager.Compute.Models
{
    /// <summary> Describes Windows Remote Management configuration of the VM. </summary>
    public partial class WinRMConfiguration
    {
        /// <summary> Initializes a new instance of WinRMConfiguration. </summary>
        public WinRMConfiguration()
        {
            Listeners = new ChangeTrackingList<WinRMListener>();
        }

        /// <summary> Initializes a new instance of WinRMConfiguration. </summary>
        /// <param name="listeners"> The list of Windows Remote Management listeners. </param>
        internal WinRMConfiguration(IList<WinRMListener> listeners)
        {
            Listeners = listeners;
        }

        /// <summary> The list of Windows Remote Management listeners. </summary>
        public IList<WinRMListener> Listeners { get; }
    }
}


text:
Component for lazy add and/or remove pages depending on the container size. Supports pop left of front page that hides when it blurs.

## [Online demo](https://mol.hyoo.ru/#!section=demos/readme/demo=mol_book_demo)

## Usage example

```
$my_app $mol_book
	pages /
		<= Nav_bar $side_menu
			minimal_width 100
		<= Main $main_page
			minimal_width 400
```

## Properties

`pages() : $mol_view[]`

The array of "pages"

`event_front_up( next? : Event ) : Event`

Pop left front page.

`event_front_down( next? : Event ) : Event`

Hide front page is its poped.


text:
Profile - General

            <form class="form-horizontal" name="profileForm" novalidate>

                    First Name
                    <input class="form-control" name="firstName" ng-model="profile.firstName" ng-disabled="!editMode" required />

                    Middle Name
                    <input class="form-control" name="middleName" ng-model="profile.middleName" ng-disabled="!editMode" />

                    Last Name
                    <input class="form-control" name="lastName" ng-model="profile.lastName" ng-disabled="!editMode" required />

                    Email
                    <input class="form-control" name="email" ng-model="profile.email" ng-disabled="!editMode" required />

                    <button type="button" class="btn btn-warning btn-lg glyphicon glyphicon-pencil" ng-hide="editMode" ng-disabled="profileForm.$invalid" ng-click="editProfile()"> Edit</button>
                    <button type="button" class="btn btn-success btn-lg glyphicon glyphicon-saved" ng-show="editMode" ng-disabled="profileForm.$invalid" ng-click="saveProfile()"> Save</button>

            </form>

Profile - Account Details

            <form class="form-horizontal" name="profileFormAccount" novalidate>

                    Old Password
                    <input name="password1" ng-model="accountProfile.oldPassword" type="password" class="form-control" placeholder="Password" ng-minlength="6" required ng-disabled="!editModeAccount" />

                        Password must not be less than 6 characters.

                    New Password
                    <input name="password2" ng-model="accountProfile.newPassword" type="password" class="form-control" placeholder="Password" ng-minlength="6" required ng-disabled="!editModeAccount" />

                        Password must not be less than 6 characters.

                    Confirm Password
                    <input name="password3" type="password" ng-model="accountProfile.newPassword" class="form-control" placeholder="Confirm Password" ng-minlength="6" required ng-disabled="!editModeAccount" />

                        Password must not be less than 6 characters.

                    <button type="button" class="btn btn-warning btn-lg glyphicon glyphicon-pencil" ng-hide="editModeAccount" ng-disabled="profileFormAccount.$invalid" ng-click="editAccountProfile()"> Edit</button>
                    <button type="button" class="btn btn-success btn-lg glyphicon glyphicon-saved" ng-show="editModeAccount" ng-disabled="profileFormAccount.$invalid" ng-click="saveAccountProfile()"> Save</button>

            </form>


text:
import os
import json
import shutil
import fnmatch
import tempfile
import subprocess

import sibis

def export_spiral_files(xnat, resource_location, to_directory, stroop=(None, None, None), verbose=None):
    if verbose:
        print("Exporting spiral files...")
    result = False # Nothing updated or created yet
    # resource location contains results dict with path building elements
    # NCANDA_E01696/27630/spiral.tar.gz
    spiral_nifti_out = os.path.join(to_directory, 'native', 'bold4D.nii.gz')
    if not os.path.exists(spiral_nifti_out):
        tmpdir = tempfile.mkdtemp()
        result = do_export_spiral_files(xnat, resource_location, to_directory, spiral_nifti_out, tmpdir)
        shutil.rmtree(tmpdir)

    # Do we have information on Stroop files?
    if stroop[0]:
        stroop_file_out = os.path.join(to_directory, 'native', 'stroop.txt')
        # Stroop file does not exist yet, so create it
        if not os.path.exists( stroop_file_out ):
            tmpdir = tempfile.mkdtemp()
            stroop_file_tmp = xnat.select.experiment(stroop[0]).resource(stroop[1]).file(stroop[2]).get_copy(os.path.join( tmpdir, stroop[2]))

            from sanitize_eprime import copy_sanitize
            copy_sanitize(stroop_file_tmp, stroop_file_out)
            shutil.rmtree(tmpdir)

            result = True

    if verbose:
        if result:
            print("...done!")
        else:
            print("...nothing exported!")

    return result

def do_export_spiral_files(xnat, resource_location, to_directory, spiral_nifti_out, tmpdir, verbose=None):
    # Do the actual export using a temporary directory that is managed by the caller
    # (simplifies its removal regardless of exit taken)
    [xnat_eid, resource_id, resource_file_bname] = resource_location.split('/')
    tmp_file_path = xnat.select.experiment(xnat_eid).resource(resource_id).file(resource_file_bname).get_copy(os.path.join(tmpdir, "pfiles.tar.gz"))

    errcode, stdout, stderr = untar_to_dir(tmp_file_path, tmpdir)
    if errcode != 0:
        error="ERROR: Unable to un-tar resource file. File is likely corrupt."
        sibis.logging(xnat_eid,error,
                     tempfile_path=tmp_file_path,
                     resource_location=resource_location)
        if verbose:
            print "StdErr:\n{}".format(stderr)
            print "StdOut:\n{}".format(stdout)
        return False

    spiral_E_files = glob_for_files_recursive(tmpdir, pattern="E*P*.7")
    if len(spiral_E_files) > 1:
        error = "ERROR: more than one E file found"
        sibis.logging(xnat_eid,error,
                      spiral_e_files = ', '.join(spiral_E_files))
        return False

    physio_files = glob_for_files_recursive(tmpdir, pattern="P*.physio")
    if len(physio_files) > 1:
        error = 'More than one physio file found.'
        sibis.logging(xnat_eid,error,
                     tmp_file_path=tmp_file_path,
                     physio_files=physio_files)
        return False

    if len(spiral_E_files) == 1:
        # Make directory first
        spiral_dir_out = os.path.join(to_directory, 'native')
        if not os.path.exists(spiral_dir_out):
            os.makedirs(spiral_dir_out)
        # Now try to make the NIfTI
        errcode, stdout, stderr = make_nifti_from_spiral(spiral_E_files[0], spiral_nifti_out)
        if not os.path.exists(spiral_nifti_out):
            error="Unable to make NIfTI from resource file, please try running makenifti manually"
            sibis.logging(xnat_eid, error,
                         spiral_file=spiral_E_files[0])
            if verbose:
                print "StdErr:\n{}".format(stderr)
                print "StdOut:\n{}".format(stdout)
            return False
    else:
        error = "ERROR: no spiral data file found"
        sibis.logging(xnat_eid,error,
                      resource_location=resource_location)
        return False

    if len(physio_files) == 1:
        spiral_physio_out = os.path.join(to_directory, 'native', 'physio')
        shutil.copyfile(physio_files[0], spiral_physio_out)
        gzip(spiral_physio_out)
    return True

def make_nifti_from_spiral(spiral_file, outfile):
    cmd = "makenifti -s 0 %s %s" % (spiral_file, outfile[:-7])

    errcode, stdout, stderr = call_shell_program(cmd)
    if os.path.exists(outfile[:-3]):
        gzip(outfile[:-3])
    return errcode, stdout, stderr

def call_shell_program(cmd):
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = process.communicate()
    return process.returncode, out, err

def untar_to_dir(tarfile, out_dir):
    cmd = "tar -xzf %(tarfile)s --directory=%(out_dir)s"

    cmd = cmd % {'tarfile':tarfile,
                 'out_dir':out_dir
                 }

    errcode, stdout, stderr = call_shell_program(cmd)
    return errcode, stdout, stderr

def glob_for_files_recursive(root_dir, pattern):
    """Globs for files with the pattern rules used in bash. """
    match_files = []
    for root, dirs, files in os.walk(root_dir, topdown=False):
        match_files += [os.path.join(root, fname) for fname in files if fnmatch.fnmatch(fname, pattern)]
    return match_files

def gzip(infile):
    cmd = 'gzip -9 %s' % infile
    call_shell_program(cmd)


text:
References
=====
Rust
-----------

**Documentation**

- _These are highly suggested bookmarks_
- [The Rust Programming Language - Rust Documentation](https://doc.rust-lang.org/book/)

**Courses**
- Video:
  - http://rustbyexample.com/
  - https://www.youtube.com/playlist?list=PLTOeCUgrkpMNEHx6j0vCH0cuyAIVZadnc
  - https://stepic.org/lesson/Rust-The-Basics-9268/step/1
  - https://www.codementor.io/rust/tutorial
  - https://jaredonline.svbtle.com/roguelike-tutorial-in-rust
  - http://exercism.io/languages/rust

**Blogs - Single Author**

- http://blog.burntsushi.net/
- http://hoverbear.org/tag/rust/
- https://brson.github.io/blog/index.html
- http://words.steveklabnik.com/

Books
================================

**Paid**

- [Rust Programming Concepts Book by Matt Carkci — Kickstarter](https://www.kickstarter.com/projects/1712125778/rust-programming-concepts-book)
- [Programming Rust: Jim Blandy, Jason Orendorff](http://www.amazon.com/Programming-Rust-Jim-Blandy/dp/1491927283)
- [Rust Essentials | PACKT Books](https://www.packtpub.com/books/content/support/21311)
- [Professional JavaScript for Web Developers by Nicholas C. Zakas](http://www.amazon.com/gp/product/B006PW2URI/)

**Free**

- [Rust 101: Interactive Rust Tutorial](https://www.ralfj.de/projects/rust-101/main.html)
- [Getting Started with Rust](http://aml3.github.io/RustTutorial/html/01.html)
- [Rust For C++](https://ronald-liu.gitbooks.io/rust-for-c-/content/)
- [Why Rust? | Rust Basics] (https://dumindu.gitbooks.io/rust-basics/content/why_rust.html)
- [Why Rust? - O'Reilly Media](http://www.oreilly.com/programming/free/why-rust.csp)


text:
using System;

namespace Jasily.Reflection.Attributes
{
    [AttributeUsage(AttributeTargets.Parameter)]
    public class DefaultValueAttribute : Attribute
    {
        public DefaultValueAttribute(object value)
        {
            this.Value = value;
        }

        public object Value { get; }
    }
}


text:
/*
   TOANDFRO - To and Fro

   split the given string into x arrays/strings of lenght "col". then reverse each alternate array/string stating from 2nd string.
   then print all characters from each array/string based on their index.

   useful info :
       string has str.begin() and str.end() funtions.
       algorithm header has reverse funtion whicj can do inplace reversing.
 */

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void sent(string s, int col){
        int sen_len = s.length();
        int rows = sen_len/col;
        // cout << s << col << sen_len << rows << endl;
        string arr[rows];
        int i=0;
        int row=0;
        while(i<sen_len ) {
                if (i%2 == 0) {
                        arr[row] = s.substr(i,col);
                        // cout << i << "\t" << arr[row] << endl;
                } else {
                        string temp = s.substr(i,col);
                        // Reverse str[beign..end]
                        reverse(temp.begin(),temp.end());
                        arr[row] = temp;
                        // cout << i << "\t" << arr[row] << endl;
                }
                i+=col;
                row++;
        }

        for (int i = 0; i < col; i++) {
                for(int j=0; j<rows; j++) {
                        cout << arr[j][i];
                }
        }
        cout << "\n";
}

int main(){
        int col;
        string cipher;
        while(true) {
                cin >> col;
                if (col) {
                        cin >> cipher;
                        sent(cipher,col);
                } else {
                        return 0;
                }
        }
}


text:
module Queuel
  module Null
    class Message < Base::Message
    end
  end
end