All pastes #1525997 Raw Edit

dead/playlistmemalloc

public text v1 · immutable
#1525997 ·published 2009-08-12 03:38 UTC
rendered paste body
PLIST * plist_new(const char * fname)
{
  PLIST * pl = NULL;
  FILE * plfile = NULL;
  double line_count = 0; /* number of lines in file */
  char buf[LBUF]; /* line buffer to read file */
  if (pl = malloc(sizeof *pl)) {               /* single = intentional */
    pl->songs = NULL;
    pl->nsongs = pl->maxsongs = 0;
    if (fname && (pl->fname = malloc(strlen(fname) * sizeof *pl->fname)))
      strcpy(pl->fname,fname);  /* if got memory, save filename */
    else
      pl->fname = "(ENO_MEM)";  /* couldn't get memory, can't save fname */
    if (plfile = fopen(fname,"r")) {           /* single = intentional */
      line_count = f_olc(plfile); /* count lines on open file handle */
      if (line_count > 0 && (pl->songs = malloc(line_count * sizeof **pl->songs))) {
        pl->maxsongs = line_count;  /* update maximum song capacity */
        while (pl->nsongs < pl->maxsongs && fgets(buf,LBUF,plfile)) {
          if (strspn(buf," \r\n\t") < strlen(buf)) { /* ignore empty lines */
            f_chomp(buf);
            pl->songs[(int)pl->nsongs++] = song_new(buf);
          }
        }
      }
      fclose(plfile); /* done with file */
    }
  }
  return pl;
}