Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define UTF_SIZE 2
- #define UNIX "-unix"
- #define MAC "-mac"
- #define WIN "-win"
- #define BSLASH_N 0x004f
- #define BSLASH_R 0x006f
- #define SWAP_END "-swap"
- #define KEEP_END "-keep"
- int plainCopy(const char* srcName, const char* destName);
- int opSystemChangeCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
- const char* destOpSysFlg);
- int endSwapCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
- const char* destOpSysFlg, const char* endSwapFlg);
- void swap(FILE* dest, char buffer[], const char* endSwapFlg);
- char getNewLine(const char* opSysFlg);
- int main(int argc, char *argv[]) {
- // the output file would be the first arg.
- // option 1: srcName + destName
- // option 2: srcName + destName + srcOpSystemFlag + destOpSystemFlag
- // option 3: same as option 2 but with endianChangeFlag
- // reading and writing files as binary files ----
- // every char is in 2 bytes. UTF-16
- // override same name file
- // try to open the source file
- printf("got here \n");
- switch (argc) {
- case 3:
- // option 1: srcName + destName
- printf("case 1 \n");
- return plainCopy(argv[1], argv[2]);
- /*case 5:
- // option 2
- fourArgs(argv[1], argv[2], argv[3], argv[4]);
- break; */
- case 6:
- // option 2
- return endSwapCopy(argv[1], argv[2], argv[3], argv[4], argv[5]);
- default:
- return 1;
- }
- return 0;
- }
- int plainCopy(const char* srcName, const char* destName) {
- return opSystemChangeCopy(srcName, destName, UNIX, UNIX);
- }
- int opSystemChangeCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
- const char* destOpSysFlg) {
- return endSwapCopy(srcName, destName, srcOpSysFlg,
- destOpSysFlg, KEEP_END);
- }
- int endSwapCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
- const char* destOpSysFlg, const char* endSwapFlg) {
- // if the two files share the same name, we can't convert
- if (!strcmp(srcName, destName)) {
- return 1;
- }
- FILE* src = fopen(srcName, "rb");
- FILE* dest = fopen(destName, "wb");
- // if any File failed to open, return failure
- if (src == NULL || dest == NULL) {
- printf("files not open");
- return 1;
- }
- // if both opened successfully, continue
- char* srcNewLineChar, destNewLineChar;
- char buffer[2];
- // get the system's newLine character
- char srcNewLine = getNewLine(srcOpSysFlg);
- char destNewLine = getNewLine(destOpSysFlg);
- char newLineBuff[2] = {destNewLine, 0};
- printf("%c \n", srcNewLine);
- // go over src, taking 2 bytes at a time (a single character in UTF-16)
- while (fread(buffer, 1, 2, src) == 2) {
- // if the character is a newLine characer and the two systems are different
- // change the char to the destination file's operating system's format
- if ((buffer[0] == srcNewLine && buffer[1] == 0) && strcmp(srcOpSysFlg, destOpSysFlg)) {
- printf("found a tiny o\n");
- // if our source is Windows
- if (!strcmp(srcOpSysFlg, WIN)) {
- char temp[2];
- // keep reading characters to search for \r\n
- while (fread(temp, 1, 2, src) == 2) {
- if (temp[0] == BSLASH_R && temp[1] == 0) {
- // it's just a hanging \r, but we found another \r. keep looking.
- swap(dest, temp, endSwapFlg);
- } else {
- // we found a '\r\n' !! we can stop the loop that was looking for it.
- if (temp[0] == BSLASH_N && temp[1] == 0) {
- swap(dest, newLineBuff, endSwapFlg);
- } else {
- // the sequence is done
- swap(dest, buffer, endSwapFlg);
- swap(dest, temp, endSwapFlg);
- }
- continue;
- }
- } // looking for \n loop
- } else {
- // if we're not on windows, first put the new line char
- swap(dest, newLineBuff, endSwapFlg);
- // if we are transferring to windows, just add another \n
- if (!strcmp(destOpSysFlg, WIN)) {
- char bSlashNBuff[2] = {BSLASH_N, 0};
- swap(dest, bSlashNBuff, endSwapFlg);
- }
- }
- } else {
- // nothing special is needes for a random character
- swap(dest, buffer, endSwapFlg);
- }
- }
- fclose(src);
- fclose(dest);
- printf("end func");
- return 0;
- }
- void swap(FILE* dest, char buffer[], const char* endSwapFlg) {
- // if we have to swap endian-ness, swap the two bytes of every character
- if (!strcmp(endSwapFlg, SWAP_END)) {
- // takes only a single byte since we're in C
- char firstByte = buffer[0];
- char secondByte = buffer[1];
- // write the two bytes in reverse order
- fwrite(&secondByte, 1, 1, dest);
- fwrite(&firstByte, 1, 1, dest);
- } else {
- // write the buffer to the file without swapping endians
- fwrite(buffer, 1, 2, dest);
- }
- }
- char getNewLine(const char* opSysFlg) {
- if (!strcmp(opSysFlg, UNIX)) {
- return BSLASH_R;
- }
- return BSLASH_N;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement