/* * Project name: Java Get-Set Generator * Author: Isaac Good * Summary: If you ever wrote 'proper' Java, you know that the data members of a class are * supposed to be private and read/written using public get/set methods. * Unfortunately, it is a pain to write all those get/sets. Hence this program! * This program takes the class name and the variables seperated with white space. * It them writes all the get and set methods. * Variable types currently supported: - int - String - StringItem (J2ME) - TextField (J2ME) * * Example input: myClass int counter int number String name StringItem outputLocation TextField userInput * * Project start/end: * August 07, '06 * * This is under the MIT license like the rest of my software. See http://www.ecf.utoronto.ca/~goodi/html/software.html for the license. */ #include #include #include // Input Data struct. Class name, array of variable names + types and nunber of variables typedef struct { int size; char **varName; char **varType; char * className; } InputData; // This is the 'on error' function InputData doErr (void) { InputData i; printf("Error. Usage: "); i.size = 0; return i; } // Check validity of input types int invalidType(char * s) { int i; i = 1; i &= strcmp(s, "int"); i &= strcmp(s, "String"); i &= strcmp(s, "TextField"); i &= strcmp(s, "StringItem"); if (i) printf("Invalid type: %s\n", s); return i; } // Reads in input from stdio and checks all types are valid. InputData getInputData (void) { InputData in; char buffer[128]; in.size = 0; if(scanf("%s", buffer) != 1) return doErr(); in.className = malloc(strlen(buffer)); strcpy(in.className, buffer); in.varName = NULL; in.varType = NULL; while(scanf("%s", buffer) == 1) { // Pull var type if (invalidType(buffer)) return doErr(); in.varType = realloc(in.varType, sizeof(char*) * (in.size + 1)); in.varType[in.size] = malloc(strlen(buffer) + 1); strcpy(in.varType[in.size], buffer); // Pull var name if(scanf("%s", buffer) != 1) return doErr(); in.varName = realloc(in.varName, sizeof(char*) * (in.size + 1)); in.varName[in.size] = malloc(strlen(buffer) + 1); strcpy(in.varName[in.size], buffer); in.size++; } return in; } // Takes "myVar" and returns "MyVar" which can be prefixed with "set" or "get" char * toGetSetName (char * name) { char * t; t = malloc(strlen(name) + 1); strcpy(t, name); if (t[0] >= 'a' && t[0] <= 'z') t[0] -= 32; return t; } // Returns the type used to set or returned from the get. e.g. StringItem is set with String and get returns a String char * getSetVariableType (char * s) { if (strcmp(s, "int") == 0) return "int"; else if (strcmp(s, "String") == 0) return "String"; else if (strcmp(s, "TextField") == 0) return "String"; else if (strcmp(s, "StringItem") == 0) return "String"; else return ""; } // Creates the set statement. e.g. a StringItem is set with StringItem.setText(String) char * makeSet (int i, InputData in) { char * s; s = malloc(1024); strcpy(s, "{ this."); strcat(s, in.varName[i]); if (strcmp(in.varType[i], "int") == 0) { strcat(s, " = "); s[strlen(s)] = in.varType[i][0]; s[strlen(s) + 1] = NULL; } else if (strcmp(in.varType[i], "String") == 0) { strcat(s, " = "); s[strlen(s)] = in.varType[i][0]; s[strlen(s) + 1] = NULL; } else if (strcmp(in.varType[i], "TextField") == 0) { strcat(s, ".setString("); s[strlen(s)] = in.varType[i][0]; s[strlen(s) + 1] = NULL; strcat(s, ")"); } else if (strcmp(in.varType[i], "StringItem") == 0) { strcat(s, ".setText("); s[strlen(s)] = in.varType[i][0]; s[strlen(s) + 1] = NULL; strcat(s, ")"); } strcat(s, "; }\n"); return s; } // Creates the get statement. e.g. a StringItem is read with StringItem.getText(); char * makeGet (int i, InputData in) { char * s; if (strcmp(in.varType[i], "int") == 0) return in.varName[i]; else if (strcmp(in.varType[i], "String") == 0) return in.varName[i]; else if (strcmp(in.varType[i], "TextField") == 0) { s = malloc(strlen(in.varName[i]) + 1 + 12); strcpy(s, in.varName[i]); strcat(s, ".getString()"); return s; } else if (strcmp(in.varType[i], "StringItem") == 0) { s = malloc(strlen(in.varName[i]) + 1 + 10); strcpy(s, in.varName[i]); strcat(s, ".getText()"); return s; } else return ""; } void makeOutput(InputData in) { int i; char * t; // Output class name printf("public class %s {\n", in.className); // Output variables for (i = 0; i < in.size; i++) printf("\tprivate %s %s;\n", in.varType[i], in.varName[i]); printf("\n"); // Output set functions for (i = 0; i < in.size; i++) { printf("\tpublic void set%s (%s %c) ", toGetSetName(in.varName[i]), getSetVariableType(in.varType[i]), in.varType[i][0]); t = makeSet(i, in); printf("%s", t); free(t); } printf("\n"); // Output get functions for (i = 0; i < in.size; i++) { printf("\tpublic %s get%s () ", getSetVariableType(in.varType[i]), toGetSetName(in.varName[i])); printf("{ return this.%s; }", makeGet(i, in)); } printf("\n}"); return; } int main (void) { InputData in; in = getInputData(); if (in.size == 0) return 1; makeOutput(in); return 0; }