示例#1
0
/* Changes fileLines to point to an array of strings, one per each
 * line in the named file.  Returns the number of strings, or 0 if
 * there was a problem reading the file.
 */
int fgetlines(char* fileName, char ***fileLines) {
    /* This implementation uses dynamic memory allocation to ask the
     * operating system for just enough memory to hold the data.  The
     * code has extensive error checking of return values.  Don't worry
     * if you don't understand this code yet; we'll cover dynamic
     * memory allocation more in future sessions.  But it is good
     * practice to see if you can work out what we're doing here.
     */
    int MAX_LINES = 255;
    int lineCount = 0;
    char **result = calloc(MAX_LINES, sizeof(char *));
    if (result == NULL)
        return 0;

    FILE *filePtr;
    filePtr = fopen(fileName, "r");
    if (filePtr == NULL) {
        free(result);
        return 0;
    }

    char *nextLine = calloc(MAX_LINE_LENGTH, sizeof(char));
    if (nextLine == NULL) {
        free(result);
        return 0;
    }
    int len = fgetline(filePtr, nextLine, MAX_LINE_LENGTH);
    while (len && lineCount < MAX_LINES) {
        result[lineCount] = nextLine;
        lineCount++;
        nextLine = calloc(MAX_LINE_LENGTH, sizeof(char));
        if (nextLine == NULL) {
            freelines(result, lineCount);
            return 0;
        }
        len = fgetline(filePtr, nextLine, MAX_LINE_LENGTH);
    }

    /* Frees the last line, which wasn't used. */
    free(nextLine);

    /* Closes the file. */
    int e = fclose(filePtr);
    if (e == EOF) {
        /* If there's an error closing the file, then release all the memory
         * we asked for and return 0. */
        freelines(result, lineCount);
        return 0;
    }

    /* Shrinks the line array to just the size needed. */
    char **reallocResult = realloc(result, lineCount * sizeof(char *));
    if (reallocResult == NULL)
        *fileLines = result;
    else
        *fileLines = reallocResult;
    return lineCount;
}
示例#2
0
/* Reads the file with the given name in its entirety using dynamic
 * memory allocation.  Writes the content to the console. */
void printFileToConsole3(char *name) {
    char **fileLines;
    int lineCount = fgetlines(name, &fileLines);
    int i;
    for (i=0; i<lineCount; i++)
        printf("%s", fileLines[i]);
    freelines(fileLines, lineCount);
}
示例#3
0
文件: undo.c 项目: jwillia3/wse2
int clearundo(Buf *b, Undo **stk) {
	
	Undo	*u;
	

	while (u=pop(stk)) {
		freelines(u->dat, u->lo, u->hi);
		clearundo(b, &u->grp);
	}
	return 1;
}
示例#4
0
文件: main.c 项目: jmahler/shootout
int main()
{
	char **lines = NULL;
	size_t num_lines = 0;
	int i;

	if (readlines(&lines, &num_lines, stdin) < 0) {
		exit(EXIT_FAILURE);
	}

	sort(lines, num_lines);

	for (i = 0; i < num_lines; i++) {
		printf("%s", lines[i]);
	}

	freelines(lines, num_lines);

	return 0;
}
示例#5
0
int main(int argc, const char * const *argv)
{
  int help = 0, version = 0, hang = 0, prefix = -1, repeat = 0, suffix = -1,
      Tab = 1, width = 72, body = 0, cap = 0, div = 0, Err = 0, expel = 0,
      fit = 0, guess = 0, invis = 0, just = 0, last = 0, quote = 0, Report = 0,
      touch = -1;
  int prefixbak, suffixbak, c, sawnonblank, oweblank, n, i, afp, fs;
  charset *bodychars = NULL, *protectchars = NULL, *quotechars = NULL;
  char *parinit = NULL, *arg, **inlines = NULL, **endline, **firstline, *end,
       **nextline, **outlines = NULL, **line;
  const char *env, * const whitechars = " \f\n\r\t\v";
  errmsg_t errmsg = { '\0' };
  lineprop *props = NULL, *firstprop, *nextprop;
  FILE *errout;

/* Process environment variables: */

  env = getenv("PARBODY");
  if (!env) env = "";
  bodychars = parsecharset(env,errmsg);
  if (*errmsg) {
    help = 1;
    goto parcleanup;
  }

  env = getenv("PARPROTECT");
  if (!env) env = "";
  protectchars = parsecharset(env,errmsg);
  if (*errmsg) {
    help = 1;
    goto parcleanup;
  }

  env = getenv("PARQUOTE");
  if (!env) env = "> ";
  quotechars = parsecharset(env,errmsg);
  if (*errmsg) {
    help = 1;
    goto parcleanup;
  }

  env = getenv("PARINIT");
  if (env) {
    parinit = malloc((strlen(env) + 1) * sizeof (char));
    if (!parinit) {
      strcpy(errmsg,outofmem);
      goto parcleanup;
    }
    strcpy(parinit,env);
    arg = strtok(parinit,whitechars);
    while (arg) {
      parsearg(arg, &help, &version, bodychars, protectchars,
               quotechars, &hang, &prefix, &repeat, &suffix, &Tab,
               &width, &body, &cap, &div, &Err, &expel, &fit, &guess,
               &invis, &just, &last, &quote, &Report, &touch, errmsg );
      if (*errmsg || help || version) goto parcleanup;
      arg = strtok(NULL,whitechars);
    }
    free(parinit);
    parinit = NULL;
  }

/* Process command line arguments: */

  while (*++argv) {
    parsearg(*argv, &help, &version, bodychars, protectchars,
             quotechars, &hang, &prefix, &repeat, &suffix, &Tab,
             &width, &body, &cap, &div, &Err, &expel, &fit, &guess,
             &invis, &just, &last, &quote, &Report, &touch, errmsg );
    if (*errmsg || help || version) goto parcleanup;
  }

  if (Tab == 0) {
    strcpy(errmsg, "<Tab> must not be 0.\n");
    goto parcleanup;
  }

  if (touch < 0) touch = fit || last;
  prefixbak = prefix;
  suffixbak = suffix;

/* Main loop: */

  for (sawnonblank = oweblank = 0;  ;  ) {
    for (;;) {
      c = getchar();
      if (expel && c == '\n') {
        oweblank = sawnonblank;
        continue;
      }
      if (csmember((char) c, protectchars)) {
        sawnonblank = 1;
        if (oweblank) {
          putchar('\n');
          oweblank = 0;
        }
        while (c != '\n' && c != EOF) {
          putchar(c);
          c = getchar();
        }
      }
      if (c != '\n') break;
      putchar(c);
    }
    if (c == EOF) break;
    ungetc(c,stdin);

    inlines =
      readlines(&props, protectchars, quotechars, Tab, invis, quote, errmsg);
    if (*errmsg) goto parcleanup;

    for (endline = inlines;  *endline;  ++endline);
    if (endline == inlines) {
      free(inlines);
      inlines = NULL;
      continue;
    }

    sawnonblank = 1;
    if (oweblank) {
      putchar('\n');
      oweblank = 0;
    }

    delimit((const char * const *) inlines,
            (const char * const *) endline,
            bodychars, repeat, body, div, 0, 0, props);

    if (expel)
      marksuperf((const char * const *) inlines,
                 (const char * const *) endline, props);

    firstline = inlines, firstprop = props;
    do {
      if (isbodiless(firstprop)) {
        if (!isinvis(firstprop) && !(expel && issuperf(firstprop))) {
          for (end = *firstline;  *end;  ++end);
          if (!repeat  ||  firstprop->rc == ' ' && !firstprop->s) {
            while (end > *firstline && end[-1] == ' ') --end;
            *end = '\0';
            puts(*firstline);
          }
          else {
            n = width - firstprop->p - firstprop->s;
            if (n < 0) {
              sprintf(errmsg,impossibility,5);
              goto parcleanup;
            }
            printf("%.*s", firstprop->p, *firstline);
            for (i = n;  i;  --i)
              putchar(firstprop->rc);
            puts(end - firstprop->s);
          }
        }
        ++firstline, ++firstprop;
        continue;
      }

      for (nextline = firstline + 1, nextprop = firstprop + 1;
           nextline < endline && !isbodiless(nextprop) && !isfirst(nextprop);
           ++nextline, ++nextprop);

      prefix = prefixbak, suffix = suffixbak;
      setaffixes((const char * const *) firstline,
                 (const char * const *) nextline, firstprop, bodychars,
                 quotechars, hang, body, quote, &afp, &fs, &prefix, &suffix);
      if (width <= prefix + suffix) {
        sprintf(errmsg,
                "<width> (%d) <= <prefix> (%d) + <suffix> (%d)\n",
                width, prefix, suffix);
        goto parcleanup;
      }

      outlines =
        reformat((const char * const *) firstline,
                 (const char * const *) nextline,
                 afp, fs, hang, prefix, suffix, width, cap,
                 fit, guess, just, last, Report, touch, errmsg);
      if (*errmsg) goto parcleanup;

      for (line = outlines;  *line;  ++line)
        puts(*line);

      freelines(outlines);
      outlines = NULL;

      firstline = nextline, firstprop = nextprop;
    } while (firstline < endline);

    freelines(inlines);
    inlines = NULL;

    free(props);
    props = NULL;
  }

parcleanup:

  if (bodychars) freecharset(bodychars);
  if (protectchars) freecharset(protectchars);
  if (quotechars) freecharset(quotechars);
  if (parinit) free(parinit);
  if (inlines) freelines(inlines);
  if (props) free(props);
  if (outlines) freelines(outlines);

  errout = Err ? stderr : stdout;
  if (*errmsg) fprintf(errout, "par error:\n%.*s", errmsg_size, errmsg);
  if (version) fputs("par 1.50\n",errout);
  if (help)    fputs(usagemsg,errout);

  return *errmsg ? EXIT_FAILURE : EXIT_SUCCESS;
}