void helpFunction(int leftbracket, int rightbracket, string s, vector<string>& result) { if (leftbracket == 0 && rightbracket == 0) { result.push_back(s); return; } if (leftbracket > 0) { helpFunction(leftbracket - 1, rightbracket, s + "(", result); } if (rightbracket > 0 && leftbracket < rightbracket) { helpFunction(leftbracket, rightbracket - 1, s + ")", result); } return; }
vector<string> generateParenthesis(int n) { vector<string> result; helpFunction(n, n, "", result); return result; }
vector<vector<int> > helpFunction(int lhs, int rhs, int k) { vector<vector<int> > result; if (k == 0 || lhs > rhs) { goto out; } if (k == 1) { for (int i = lhs; i <= rhs; ++i) { result.push_back(vector<int>(1, i)); } goto out; } for (int i = lhs; i <= rhs; ++i) { auto combinations = helpFunction(i + 1, rhs, k - 1); for (int j = 0; j < combinations.size(); ++j) { combinations[j].insert(combinations[j].begin(), i); } result.insert(result.begin(), combinations.begin(), combinations.end()); } out: return result; }
vector<vector<int> > combine(int n, int k) { return helpFunction(1, n, k); }
Ct_32_int main(Ct_32_int argc, char *argv[]) { int argumentCounter; int fileAttributesFlag = 1; int createDispositionFlag = 1; int createOptionsFlag = 1; int shareAccessFlag = 1; int desiredAccessFlag = 1; int deleteFlag = 1; int multipleFlag = 0; CtInitializeMemory(); if(argc == 1) { helpFunction(); } else if(!strcmpi(argv[1], "-help")) { helpFunction(); } else if(argc > 2) { for(argumentCounter = 2; argumentCounter < argc; argumentCounter++) { if(!strcmpi(argv[argumentCounter], "-intense")) { fileAttributesFlag = 1; createDispositionFlag = 1; createOptionsFlag = 1; shareAccessFlag = 1; desiredAccessFlag = 1; break; } else if(!strcmpi(argv[argumentCounter], "-light")) { fileAttributesFlag = 1; createDispositionFlag = 1; createOptionsFlag = 0; shareAccessFlag = 0; desiredAccessFlag = 1; } else if(!strcmpi(argv[argumentCounter], "-constant")) { printf("Hello"); argumentCounter++; if(!strcmpi(argv[argumentCounter], "FileAttributes")) { fileAttributesFlag = 0; } else if(!strcmpi(argv[argumentCounter], "CreateDisposition")) { createDispositionFlag = 0; } else if(!strcmpi(argv[argumentCounter], "CreateOptions")) { createOptionsFlag = 0; } else if(!strcmpi(argv[argumentCounter], "ShareAccess")) { shareAccessFlag = 0; } else if(!strcmpi(argv[argumentCounter], "DesiredAccess")) { desiredAccessFlag = 0; } } else if(!strcmpi(argv[argumentCounter], "-keep")) { deleteFlag = 0; } else if(!strcmpi(argv[argumentCounter], "-multiple")) { multipleFlag = 1; } else { printf("Illegal paramater found! What is %s? This program definitely doesn't know. Continuing anyway though because the developer didn't have time for accurate error handling.\n", argv[argumentCounter]); break; } } selectiveFileCreator(argv[1], fileAttributesFlag, createDispositionFlag, createOptionsFlag, shareAccessFlag, desiredAccessFlag, deleteFlag, multipleFlag); } CtShutdownMemory(); return 0; };
int main( int argc , char *argv[] , char *envp[]) { // File Descriptors int sockfd; int fd_c; struct sockaddr_in servaddr; //char sendline[MAXLINE]; char recvline[MAXLINE]; char writeBuff[MAXLINE]; char putCommand[MAXLINE]; // char readBuff[MAXLINE]; ssize_t ln; FILE *file; char * token; int option = 0; char *ipAddr; char *serverPort; int p_flag = 0; int i_flag = 0; umask(0); while ((option = getopt(argc, argv,"p:i:")) != -1) { switch (option) { case 'p' : p_flag = 1; serverPort = optarg; fputs("p flag triggered\n",stderr); break; case 'i': i_flag = 1; ipAddr = optarg; fputs("i flag triggered\n",stderr); break; default: printf("Usuage: ./socket_client -p <PORT_NUMBER> -i <IP_ADDR>\n"); exit(EXIT_FAILURE); } } if ((i_flag == 1) && (p_flag == 1)){ sockfd = socket(AF_INET, SOCK_STREAM, 0); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(atol(serverPort)); inet_pton(AF_INET, ipAddr, &servaddr.sin_addr.s_addr); if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) != 0) { perror("could not connect"); exit(EXIT_FAILURE); } do { printf(PROMPT); fflush(stdout); memset(writeBuff, 0,MAXLINE); fgets(writeBuff,MAXLINE, stdin); ln = strlen(writeBuff) - 1; if (writeBuff[ln] == '\n'){ writeBuff[ln] = 0; } if(strcmp(writeBuff,CMD_HELP) == 0){ helpFunction(); } else if(strcmp(writeBuff,CMD_LOCAL_DIR) == 0){ file = popen(CMD_LS_POPEN,"r"); while(fgets(writeBuff,sizeof(writeBuff),file) != NULL){ printf("%s",writeBuff); } pclose(file); } else if(strncmp(writeBuff,"clear",(ssize_t)4) == 0){ file = popen("clear","w"); perror("Local Popen (Extra Clear Command)"); pclose(file); //perror("Local Popen (Extra Clear Command)"); } else if (strcmp(writeBuff,CMD_LOCAL_PWD) == 0){ file = popen("pwd","r"); //perror("Local Popen for PWD"); while(fgets(writeBuff,sizeof(writeBuff),file) != NULL){ printf("%s",writeBuff); } pclose(file); } else if(strcmp(writeBuff,CMD_LOCAL_HOME) == 0){ memset(writeBuff,' ',sizeof(writeBuff)); chdir(getenv("HOME")); perror("chdir: "); getcwd(writeBuff,sizeof(writeBuff)); perror("getcwd: "); fprintf(stdout,"HOME: <%s> CWD: <%s> \n",getenv("HOME"),writeBuff); }else if(strncmp(writeBuff,CMD_LOCAL_CHDIR, (ssize_t)3) == 0){ int k; fprintf(stderr,"Changing Local Client Directory\n"); printf("lcd command: <%s>\n", writeBuff); token = strtok(writeBuff," "); token = strtok(NULL, " "); printf("Changing into <<<%s>>>\n", token); if(token[0] != 0){ //Change the directory k = chdir(token); perror("chdir:"); // Get new CWD getcwd(token,sizeof(token)); if(k == 0){ fprintf(stdout,"Local Client PWD is now <%s>\n",token); }else{ fprintf(stdout,"Local Client PWD was not **changed**\n"); } memset(token, 0, sizeof(token)); }else{ printf("Format is lcd <dir>\n"); } }else if (strcmp(writeBuff,CMD_REMOTE_PWD) == 0){ // send the message write(sockfd, writeBuff, strlen(writeBuff)); perror("write (pwd): "); // receive the message read(sockfd, recvline, MAXLINE); perror("read (pwd): "); // print fprintf(stderr,"Remote Server PWD:\n \t%s\n",recvline); }else if(strncmp(writeBuff,CMD_REMOTE_DIR,(ssize_t)3) == 0){ //printf("CM_REMOTE_DIR : <%s>\n", writeBuff); //Send Dir request write(sockfd, writeBuff, strlen(writeBuff)); while(1){ // receive the message read(sockfd, recvline, MAXLINE); //perror("read (pwd): "); if(strncmp(recvline, STR(EOT_CHAR), (ssize_t) MAXLINE) == 0 ){ break; }else{ fprintf(stdout,"%s",recvline); } } }else if(strncmp(writeBuff, CMD_REMOTE_CHDIR,(ssize_t)2) == 0){ fprintf(stderr, "%s\n", writeBuff); // Write Request to Server for CD <dir> write(sockfd, writeBuff, strlen(writeBuff)); perror("write (cd dir): "); // Read the response read(sockfd, recvline, MAXLINE); perror("read (CD dir) "); fprintf(stderr,"Remote Server PWD Changed:\n \t%s\n",recvline); }else if(strncmp(writeBuff, CMD_REMOTE_HOME,(ssize_t)4) == 0){ fprintf(stderr, "%s\n", writeBuff); // Write Request to Server for CD <dir> write(sockfd, writeBuff, strlen(writeBuff)); perror("write (cd dir): "); // Read the response read(sockfd, recvline, MAXLINE); perror("read (CD dir) "); fprintf(stdout,"\nHOME: <%s> CWD: <%s> \n",getenv("HOME"),recvline); }else if(strstr(writeBuff,CMD_GET_FROM_SERVER) != NULL){ fprintf(stderr, "EOT_CHAR is \n\t%s\n",STR(EOT_CHAR)); // Send GET Request to Server write(sockfd, writeBuff, MAXLINE); perror("Write (initial) GET"); token = strtok(writeBuff," "); token = strtok(NULL," "); // open file to be written to fd_c = open(token, SEND_FILE_FLAGS, SEND_FILE_PERMISSIONS); perror("fd_c open"); // receive the message while(1){ int numBytes; // receive the message numBytes = read(sockfd, recvline, MAXLINE); perror("read (GET) "); fprintf(stderr, "%d\n", numBytes); //fprintf(stderr, "<%s>",recvline); if(strcmp(STR(EOT_CHAR), recvline) == 0){ close(fd_c); perror("close fd_c"); break; }else{ write(fd_c, recvline, numBytes); //memset(recvline, 0, sizeof(recvline)); perror("write (GET) "); } memset(recvline, 0, sizeof(recvline)); } }else if(strncmp(CMD_PUT_TO_SERVER,writeBuff,(ssize_t)3) == 0){ printf("PUT command: <%s>\n", writeBuff); strcpy(putCommand,writeBuff); token= strtok(writeBuff," "); token = strtok(NULL," "); if(access(token, F_OK ) != -1 ) { int k; /**** file exists ****/ //Set Message command to put fprintf(stderr,"Sending <<<%s>>>\n",putCommand); write(sockfd, putCommand, sizeof(putCommand)); // Send the intial synch message write(sockfd, putCommand, sizeof(putCommand)); perror("Write 1"); printf("PUT File: <%s>\n", token); //Memset message to 0 memset(putCommand, 0 ,sizeof(putCommand)); //OPEN filename for reading fd_c = open(token,O_RDONLY); perror("Open fd_c"); while((k = read(fd_c, writeBuff,MAXLINE)) != 0){ fprintf(stderr,"<<<%d>>>\n",k); write(sockfd, writeBuff, k); if (k == -1){ fprintf(stderr,"*** Read Error BREAK ***\n"); break; } } fprintf(stderr,"Done sending file <%s>\n",token); //Send Final Message EOT MESSAGE strcpy(writeBuff,STR(EOT_CHAR)); /* // Send final message */ write(sockfd, writeBuff, MAXLINE); perror("write (final)"); } else { char *currentDir = getcwd(currentDir,PATH_MAX); fprintf(stderr,"File: <%s> does not exist in client's cwd (%s) \n",token,currentDir); } }else{ // send the message write(sockfd, writeBuff, strlen(writeBuff)); perror("write (other) "); // receive the message read(sockfd, recvline, MAXLINE); perror("read (other) "); fprintf(stderr,"<%s>\n",recvline); } }while(strcmp(writeBuff,CMD_EXIT) != 0); close(sockfd); return(EXIT_SUCCESS); }else{ printf("Usuage2: ./socket_client -p <PORT_NUMBER> -i <IP_ADDR> \n"); return(EXIT_FAILURE); } }
int main(int argc, char *argv[]) { int i; int retVal; char *imagePath = "null"; unsigned char **grayPixelMap; imageData_s imageData; userInput_s userArgs; /* Init */ initUserInput( &userArgs ); /*************************************************************************/ /* Get user input */ /*************************************************************************/ if( argc < 2 ) { printf("Use -h or --help flag for help!\n"); return 0; } /* Loop input arguments */ for( i=1 ; i<argc ; i++ ) { /* Get image name - first argument */ if( i == 1 ) { imagePath = argv[1]; } /* --info flag */ if( strcmp( argv[i] , "--info") == 0) { userArgs.infoFlag = 1; continue; } /* -i, --invert flags */ if( (strcmp( argv[i] , "-i" ) == 0 ) || (strcmp( argv[i] , "--invert")== 0) ) { userArgs.invertFlag = 1; } /* -b, --bitGraphic flags */ if( (strcmp( argv[i] , "-b" ) == 0 ) || (strcmp( argv[i] , "--bitGraphic")== 0) ) { if( argv[i+1] != NULL ) { userArgs.bitGraphic = atoi(argv[i+1]); if( (userArgs.bitGraphic < 1) || (userArgs.bitGraphic > 4) ) { printf(" Warrning: -b option must be set to 1, 2, 3 or 4!\n"); userArgs.bitGraphic = 4; /* Using default value */ } } else { printf(" Warrning: -b option must be set to 1, 2, 3 or 4!\n"); } continue; } /* -s, --size flags */ if( (strcmp( argv[i] , "-s" ) == 0 ) || (strcmp( argv[i] , "--size")== 0) ) { if( argv[i+1] != NULL ) { userArgs.sizeMode = atoi(argv[i+1]); if( (userArgs.sizeMode < 1) || (userArgs.sizeMode > 10) ) { printf(" Warrning: -s option must be set [ 1 - 10 ]!\n"); userArgs.sizeMode = 6; /* Using default value */ } } else { printf(" Warrning: -s option must be set [ 1 - 10 ]!\n"); } continue; } /* --html flag */ if( strcmp( argv[i] , "--html" ) == 0 ) { userArgs.htmlMode = 1; } /* -h, --help flags */ if( (strcmp( argv[i] , "-h" ) == 0 ) || (strcmp( argv[i] , "--help")== 0) ) { helpFunction(); return 0; } } /* END Loop input arguments */ /*************************************************************************/ /* Get image info */ /*************************************************************************/ retVal = storeBmpImageData( imagePath , &imageData ); if( retVal < 0 ) { return 0; } /* Print image data */ if( userArgs.infoFlag == 1 ) { printImageInfo( &imageData ); return 0; } /*************************************************************************/ /* Make gray scale pixel map */ /*************************************************************************/ /* Allocate memory for gray pixel map */ grayPixelMap = createPixelMap( imageData.imgHeight , imageData.imgWidth ); if( grayPixelMap == NULL ) { return 0; } /* Read image and store gray pixels in gray pixel map */ retVal = makeGrayPixelMap( grayPixelMap , &imageData ); if( retVal < 0 ) { printf("Cannot create gray-scale pixel map!\n"); return 0; } /*************************************************************************/ /* Main */ /*************************************************************************/ /* Print image to output */ printAsciiImage ( grayPixelMap , &userArgs , &imageData ); /* Free memory of gray pixel map */ destroyPixelMap( grayPixelMap , imageData.imgHeight ); return 0; }