I have written a program in C, which does the following operations:
- creates or splits Camel Case variable, method, class names.
Input format
Each line of the input file will begin with an operation (S or C) followed by a semi-colon followed by M, C, or V followed by a semi-colon followed by the words you'll need to operate on.
The operation will either be S (split) or C (combine)
M indicates method, C indicates class, and V indicates variable
In the case of a split operation, the words will be a camel case method, class or variable name that you need to split into a space-delimited list of words starting with a lowercase letter.
In the case of a combine operation, the words will be a space-delimited list of words starting with lowercase letters that you need to combine into the appropriate camel case String. Methods should end with an empty set of parentheses to differentiate them from variable names.
Output Format:
- For each input line, your program should print either the space-delimited list of words (in the case of a split operation) or the appropriate camel case string (in the case of a combine operation).
Sample Input
S;M;plasticCup()
C;V;mobile phone
C;C;coffee machine
S;C;LargeSoftwareBook
C;M;white sheet of paper
S;V;pictureFrame
Sample Output
plastic cup
mobilePhone
CoffeeMachine
large software book
whiteSheetOfPaper()
picture frame
here is what I've written so far:
#include <asm-generic/errno-base.h>
#include <asm-generic/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#define SPACE ' '
#define SPLIT 'S'
#define COMBINE 'C'
#define METHOD 'M'
#define CLASS COMBINE
#define VARIABLE 'V'
static char *caseProblem(char *restrict);
static void *substr(const void *restrict, size_t);
static char *split(char *restrict, int);
static char *combine(char *restrict, int);
static void *__memmove(void *restrict, int, size_t);
int
main(void)
{
char *p = NULL;
ssize_t len;
size_t n = 0;
while ((len = getline(&p, &n, stdin)) != -1)
(void)fprintf(stdout, "%s", caseProblem(p));
free(p);
exit(EXIT_SUCCESS);
}
static void *
__memmove(void *restrict src, int delim, size_t n)
{
char *dest = NULL;
if ((dest = (char *)malloc(65535 * sizeof(char))) == NULL)
errx(EXIT_FAILURE, "%s", strerror(ENOMEM));
memmove(dest, src, strlen(src) - n);
dest[strlen(dest)] = delim;
return (dest);
}
static char *
caseProblem(char *restrict p)
{
char opt, type;
unsigned int i;
size_t size = strlen(p);
/* extract option (S or C) */
opt = p[0];
/* extract type (M, V or C) */
type = p[2];
p = substr(p, 4);
if (opt == SPLIT && type == METHOD)
p = __memmove(p, '\n', 3);
if (opt == COMBINE && type == METHOD) {
p = __memmove(p, 0, 1); // reject: '\n'?
strcat(p, "()\n");
}
i = 0;;
while (i < size) {
switch (opt) {
case SPLIT:
p = split(p, type);
break;
case COMBINE:
p = combine(p, type);
break;
default:
errx(EXIT_FAILURE, "Invalid Option...!\n");
}
++i;
}
p[i] = '\0';
return (char *)(p);
}
static void *
substr(const void *restrict src, size_t n)
{
char *dest = NULL;
if ((dest = (char *)malloc(65535 * sizeof(char))) == NULL)
errx(EXIT_FAILURE, "%s", strerror(ENOMEM));
return (memcpy(dest, (src + n), strlen(src)));
}
static char *
split(char *restrict in, int type)
{
char *out = NULL;
unsigned int i, o;
size_t size = strlen(in);
if ((out = (char *)malloc(65535 * sizeof(char))) == NULL)
errx(EXIT_FAILURE, "%s", strerror(ENOMEM));
i = o = 0;
while (i < size) {
if (type == METHOD || type == VARIABLE || type == CLASS) {
if (isupper(in[i])) {
in[i] = tolower(in[i]);
if (i != 0) {
out[o++] = SPACE;
out[o] = in[i];
}
}
} else {
errx(EXIT_FAILURE, "Invalid Type...!\n");
}
out[o++] = in[i];
++i;
}
out[o] = '\0';
return (char *)(out);
}
static char *
combine(char *restrict p, int type)
{
unsigned int i, j, space;
size_t size = strlen(p);
i = j = 0;
space = 0;
while (i < size) {
switch (type) {
case VARIABLE:
case METHOD:
if (p[i] == SPACE) {
++space;
if (islower(p[space]))
p[i + 1] = toupper(p[i + 1]);
}
break;
case CLASS:
if (i == 0)
p[i] = toupper(p[i]);
if (p[i] == SPACE) {
++space;
if (islower(p[space]))
p[i + 1] = toupper(p[i + 1]);
}
break;
default:
errx(EXIT_FAILURE, "Invalid Type...!\n");
break;
}
if (p[i] != SPACE)
p[j++] = p[i];
++i;
}
p[j] = '\0';
return (p);
}
I'd like to know if there is any way to improve it.