コード例 #1
0
ファイル: 2.03.c プロジェクト: Paul-Andrew/C-KnR
main()
{
  char line[MAXLINE], c;
  int len = 0, i, dec = 0, power;
  char *abcdef = "abcdefABCDEF";
  for (len = getaline(line,MAXLINE); len > 1; len = getaline(line,MAXLINE)){
    if (line[1]=='x'||line[1]=='X'){
      for(i=2;i<len;i++) {
	line[i-2]=line[i];
      }
      len -=2;
    }
       ;//ignores 0x / 0X prefix
    for (i=0; i<len-1; i++){
      dec *=16;
      c = line[i];
      if(isdigit(c)){
	dec +=(c-'0');
      }//if
      else if (index(abcdef,c)){
	dec += ((c-'A')%('a'-'A'))+10;//hex value of a given letter
      }//else if
      else {
	printf("%c is not valid in hexadecimal!\n",c);
	break;
      }
    }//for
    printf("%i\n",dec);
    dec = 0;
  }//for
}//main
コード例 #2
0
ファイル: 1.17.c プロジェクト: Paul-Andrew/C-KnR
main()
{
  char line[MAXLINE];
  int len = 0, i;
  for (len = getaline(line,MAXLINE); len > 1; len = getaline(line,MAXLINE)){
    (len >= 80) ? printf("%s",line) : printf("%i\n",len);
  }//for len
  return 0;
}
コード例 #3
0
ファイル: charset.c プロジェクト: yinzhiqiang422/libutils-c
void *utf8fan2jian_init(const char *unihan)
{
  char *data,*hzbuf,*hz,*hzfan,*hzjian;
  TCMAP *map;
  int hznum,hzlen;

  data = loadfile(unihan,NULL);
  if(!data) return NULL;

  hznum = getlinenum(data,"\n");
  map = tcmapnew2(hznum);

  hzbuf = data;
  while((hz = getaline(hzbuf,"\n", &hzlen)) != NULL) {
    hzjian = strchr(hz,'=');
    if(!hzjian) return NULL;
    *hzjian++ = 0;
    hzfan = hz;
    trim(hzfan);
    trim(hzjian);
    tcmapput2(map,hzfan,hzjian);
    hzbuf = hz + hzlen + 1;
  }

  xfree(data);
  return map;
}
コード例 #4
0
ファイル: extended.c プロジェクト: researcherben/course-notes
int main()
{
    char line[MAX];
    int numbers[LIMIT], i = 0;
    double answer;

    while(1)
    {
        printf("Enter a NUMBER (or 'quit' to exit): ");
        getaline(line);
        
        if(strcmp(line, "quit") == 0)
            break;
            
        if(check(line) == DIGITS)
        {
            numbers[i++] = atoi(line);
            if(i == LIMIT) {
                 printf("ARRAY FULL!\n");
                 break;
            }
        }        
        else            
            printf("%s is not all digits\n", line);
    }
    
    answer = average(numbers, i);
    printf("%.2f\n", answer);
}
コード例 #5
0
ファイル: colander.cpp プロジェクト: bvds/andes
char* solveMoreOfTheProblem() {
    try {
        if (! inFile.eof()) {
            string t = getaline(inFile);
            if (t.size() == 0) {
                inFile.clear();
                inFile.close();
                return error[4];
            } else {
                sprintf(lzbfr, "%s", t.c_str());
            }
            return lzbfr;
        } else {
            inFile.clear();
            inFile.close();
            return error[4];
        }
    } catch (string message) {
        inFile.clear();
        inFile.close();
        throw message;
    } catch (...) {
        inFile.clear();
        inFile.close();
        throw string("More went bad!!");
    }
    return error[3];
}
コード例 #6
0
ファイル: ex_vops2.c プロジェクト: AlfredArouna/illumos-gate
static void
omultlinerep(int savecnt, line *startsrcline, int endsrccol)
{
	int tmpcnt = 2;	/* 1st insert counts as 1 repeat */
	line *srcline, *endsrcline;

	endsrcline = dot;
	/* Save linebuf into temp file before moving off the line. */
	vsave();

	/*
	 * Loop from the first line of the first iteration
	 * through endsrcline, appending after dot.
	 */
	while (tmpcnt <= savecnt) {
		for (srcline = startsrcline; srcline <= endsrcline; ++srcline) {
			getaline(*srcline);
			/* copy linebuf to temp file */
			vdoappend(linebuf);
			vcline++;
		}
		++tmpcnt;
	}
	cursor = linebuf + endsrccol;
}
コード例 #7
0
ファイル: rnews.c プロジェクト: BackupTheBerlios/leafnode
/*
 * process an uncompressed newsbatch
 * return 1 if successful, 0 otherwise
 */
static int
fprocessbatch(FILE * f, char *firstline)
{
    char *l;
    long bytes;
    int ret;

    while ((l = (firstline ? firstline : getaline(f)))) {
	static const char tomatch[] = "#! rnews ";
	firstline = NULL;
	ln_log(LNLOG_SDEBUG, LNLOG_CARTICLE,
		"fprocessbatch: read \"%s\"", l);

	if (!str_isprefix(l, tomatch)) {
	    ln_log(LNLOG_SERR, LNLOG_CARTICLE,
		   "expected `#! rnews', got %-.40s%s", l,
		   strlen(l) > 40 ? "[...]" : "");
	    goto pb_bail;
	}
	if (0 == get_long(l + strlen(tomatch), &bytes)) {
	    ln_log(LNLOG_SERR, LNLOG_CARTICLE,
		   "cannot extract article length from `%-.40s%s'\n", l,
		   strlen(l) > 40 ? "[...]" : "");
	    goto pb_bail;
	}
	ret = store_stream(f, 0, filter, bytes, 0);
	ln_log(LNLOG_SDEBUG, LNLOG_CARTICLE,
			"store_stream returns %d", ret);
    }
    return 1;
  pb_bail:
    return 0;
}
コード例 #8
0
ファイル: utilities.c プロジェクト: AlainODea/illumos-gate
int32_t
reply(char *question)
{
	char line[80];

	if (preen)
		pfatal(gettext("INTERNAL ERROR: GOT TO reply()"));
	(void) printf("\n%s? ", question);
	if (nflag || fswritefd < 0) {
		(void) printf(gettext(" no\n\n"));
		iscorrupt = 1;		/* known to be corrupt */
		return (0);
	}
	if (yflag) {
		(void) printf(gettext(" yes\n\n"));
		return (1);
	}
	if (getaline(stdin, line, sizeof (line)) == EOF)
		errexit("\n");
	(void) printf("\n");
	if (line[0] == 'y' || line[0] == 'Y')
		return (1);
	else {
		iscorrupt = 1;		/* known to be corrupt */
		return (0);
	}
}
コード例 #9
0
ファイル: config_file.c プロジェクト: mmodahl/RetroArch
static config_file_t *config_file_new_internal(const char *path, unsigned depth)
{
   struct config_file *conf = (struct config_file*)calloc(1, sizeof(*conf));
   if (!conf)
      return NULL;

   if (!path)
      return conf;

   conf->path = strdup(path);
   if (!conf->path)
   {
      free(conf);
      return NULL;
   }

   conf->include_depth = depth;
   FILE *file = fopen(path, "r");

   if (!file)
   {
      free(conf->path);
      free(conf);
      return NULL;
   }

   while (!feof(file))
   {
      struct entry_list *list = (struct entry_list*)calloc(1, sizeof(*list));
      char *line = getaline(file);

      if (line)
      {
         if (parse_line(conf, list, line))
         {
            if (conf->entries)
            {
               conf->tail->next = list;
               conf->tail = list;
            }
            else
            {
               conf->entries = list;
               conf->tail = list;
            }
         }

         free(line);
      }

      if (list != conf->tail)
         free(list);
   }
   fclose(file);

   return conf;
}
コード例 #10
0
/*@null@*/ /*@only@*/ char *
mygetfoldedline(const char *fi, unsigned long ln, FILE * f)
{
    /* what characters are considered whitespace that marks the beginning of
       continuation lines.
       WARNING: NEVER EVER list \n here! */
    static const char white[] = " \t";
    char *l1, *l2;
    int c, len, oldlen;

    l1 = getaline(f);
    if (!l1)
	return NULL;
    l2 = (char *)mycritmalloc(fi, ln, (len = strlen(l1)) + 1, "getfoldedline");
    strcpy(l2, l1);

    /* only try to read continuation if the line is not empty 
     * and not a lone dot */
    if (*l2 && strcmp(l2, ".")) {
	for (;;) {
	    c = fgetc(f);
	    if (c != EOF) {
		ungetc(c, f);
		if (strchr(white, c)) {
		    /* join */
		    l1 = getaline(f);
		    if (l1) {
			oldlen = len;
			len += strlen(l1);
			l2 = (char *)mycritrealloc(fi, ln, l2, len + 1,
						   "getfoldedline");
			strcpy(l2 + oldlen, l1);
		    }
		} else {
		    break;
		}
	    } else {
		break;
	    }
	}
    }
    return l2;
}
コード例 #11
0
ファイル: wmkeys.c プロジェクト: bbidulock/dockapps
void read_config()
{
  char* rcfilename;
  char* home_var;
  FILE* pfile;
  char key[256], value[256];

  home_var = getenv("HOME");

  rcfilename = malloc(sizeof(char) * strlen(home_var) + 11 /* / .wmkeysrc + NULL*/);
  strcpy(rcfilename, home_var);
  strcat(rcfilename, "/.wmkeysrc");

  pfile = fopen(rcfilename, "r");
  if(pfile == NULL) {
    /* try to open system-wide configuration */
    strcpy(rcfilename, "/etc/wmkeysrc");
    pfile = fopen(rcfilename, "r");

    if(!pfile) {
      fprintf(stderr, "Error: cannot open ${HOME}/.wmkeysrc or /etc/wmkeysrc\n");
      exit(1);
    }
  }

  while(!feof(pfile)) {
    getaline(pfile, key, 256);

    if(!feof(pfile)) {
      getaline(pfile, value, 256);

      configs[num_configs].name = malloc(sizeof(char)*strlen(key)+1);
      strcpy(configs[num_configs].name, key);
      configs[num_configs].filename = malloc(sizeof(char)*strlen(value)+1);
      strcpy(configs[num_configs].filename, value);
      num_configs++;
    }
  }
  if(num_configs == 0) {
    fprintf(stderr, "Error: no configurations, exiting.\n");
    exit(1);
  }
}
コード例 #12
0
ファイル: database.c プロジェクト: jens-na/abook-call
int
parse_database(FILE *in)
{
        char *line = NULL;
	char *tmp;
	int sec=0, field;
	list_item item;

	item = item_create();

	for(;;) {
		line = getaline(in);
		if(feof(in)) {
			if(item[field_id(NAME)] && sec) {
				add_item2database(item);
			} else {
				item_empty(item);
			}
			break;
		}

		if(!*line || *line == '\n' || *line == '#') {
			goto next;
		} else if(*line == '[') {
			if(item[field_id(NAME)] && sec ) {
				add_item2database(item);
			} else {
				item_empty(item);
			}
			sec = 1;
			memset(item, 0, ITEM_SIZE);
			if(!(tmp = strchr(line, ']')))
				sec = 0; /*incorrect section lines are skipped*/
		} else if((tmp = strchr(line, '=') ) && sec) {
			*tmp++ = '\0';
			find_field_number(line, &field);
			if(field != -1) {
				item[field] = xstrdup(tmp);
				goto next;
			} else if(!strcasecmp(opt_get_str(STR_PRESERVE_FIELDS),
						"all")){
				declare_unknown_field(line);
				item = xrealloc(item, ITEM_SIZE);
				item[fields_count - 1] = xstrdup(tmp);
				goto next;
			}
		}
next:
		xfree(line);
	}

	xfree(line);
	item_free(&item);
	return 0;
}
コード例 #13
0
int main() {
    int len;
    char line[MAXLINE];

    while((len = getaline(line, MAXLINE)) > 0) {
        printf("Length of line: %d\n", len);
        printf("Line: %s\n", line);
    }

    return 0;
}
コード例 #14
0
ファイル: strindex.c プロジェクト: hayesey/c_book
main() {
  char line[MAXLINE];
  int found = 0;

  while (getaline(line, MAXLINE) > 0)
    if (strindex(line, pattern) >= 0) {
      printf("%s", line);
      found++;
    }
  return found;
}
コード例 #15
0
int main() {
    int len;
    char line[MAXLINE];

    while((len = getaline(line, MAXLINE)) > 0) {
        if (len >= 80) {
            printf("Line: %s\n", line);
        }
    }

    return 0;
}
コード例 #16
0
ファイル: reverse.c プロジェクト: sukotto/C-TCPL
int main(void)
{
  int len;
  char line[MAXLINE];
  while((len = getaline(line,MAXLINE)) > 0)
    {
      char* reversed = reverse(line,len);
      printf("%s\n",reversed);
      free(reversed);
    }
  return 0;
}
コード例 #17
0
ファイル: ex1-19.c プロジェクト: adamesque/k-r
/* Remove trailing blanks and tabs. Omit empty lines. */
main()
{
	int len;				/* current line length */
	char line[MAXLINE];		/* current input line */

	while ((len = getaline(line, MAXLINE)) > 0) {
		line[len-1] = '\0'; // chop off the newline
		reverse(line);
		printf("%s\n", line);
	}
	return 0;
}
コード例 #18
0
ファイル: coldriver.cpp プロジェクト: LraCrisis/andes
/************************************************************************
 * solveTheProblem							*
 *	Assumes the equations and variables have already been entered	*
 *	solves the problem, writing the solution in TEMP_FILE, 		*
 *	including the tags and problem statements if the problem was	*
 *	not completely solved.						*
 *   Then it opens the file for reading, and returns the first line	*
 *	[does not close file]						*
 ************************************************************************/
const char* solveTheProblem() {
  int k;

  if (isFirst) throw string("solveTheProblem called before initialization");
  try {
    // reset the buffer to be empty
    resultBuffer.str(string());
    if(resultBuffer.good()) {
      numsols->assign(canonvars->size(),HUGE_VAL);
      if (solveeqs(resultBuffer)) {
	// should we do checking of solution here?
	bool discrep = false;
	for (k = 0; k < canoneqf->size(); k++) {
	  if (checksol((*canoneqf)[k],numsols,RELERR) > 1) {
	    if (!discrep) {
	      resultBuffer << "<DISCREPANCIES>" << endl;
	      discrep = true;
	    }
	    resultBuffer << (*canoneqf)[k]->getInfix() << endl;
	  }
	} // loop over equations to check
	dimchkeqf(resultBuffer);
	// end of "should we do checking of solution here?"
      }
    } 
    else throw string("unable to create solution buffer");
  } 
  catch (string message) { throw message; } 
  catch (...) { throw string("solveTheProblem went boom!!"); }

  // this would be a good place to insert checksol ? or after returning
  // solution, in solveMoreOfTheProblem ?
  try {
    resultBuffer.clear();
    resultBuffer.seekg(0);
    int tk;
    try { tk = resultBuffer.eof(); } 
    catch (...) { throw string("eof?!?"); }
    if (! tk) {
      string t;
      try { t = getaline(resultBuffer); }
      catch (...) { throw string("getaline fails???"); }
      try { sprintf(lzbfr, "%s", t.c_str()); } 
      catch(...) { throw string("copy is wrong??"); }
      return lzbfr;
    } 
    else return error[4];
  } 
  catch (string message) { throw message; } 
  catch (...) { throw string("Read went bad!!"); }
  return error[3];
}
コード例 #19
0
ファイル: 1.18_1.c プロジェクト: jinto-thomas/knr
main()
{
	char line[MAXLEN];
	int len;
	char box[MAXLEN];
	//	while ((len = getaline(line, MAXLEN)) > 0)
		len = getaline(line, MAXLEN);
//		printf("%s", line);
		filter(line, box);
		printf("%s",box);
		printf("%s",line);					
	return 0;
}
コード例 #20
0
ファイル: ex17.c プロジェクト: vyshakn/CKnR
int main()
{
	int len;	/* current line length */
	int max;	/* maximum length */
	char line[MAXLINE];	/* current input line */

	max = 80;	/* set filter*/
	while ((len = getaline(line, MAXLINE)) > 0)
		if (len > max) {
			printf("%s", line);
		}
	return 0;
}
コード例 #21
0
ファイル: e01-21_entab.c プロジェクト: chettrick/tcpl
/*
 * Replace blanks with tabs and blanks as to keep the same spacing.
 */
int
main(void)
{
	int len;
	char line[BUFSIZ];	/* Current input line. */
	char tabbed[BUFSIZ];	/* Cooked string with tabs. */

	while ((len = getaline(line, BUFSIZ)) != -1) {
		entab(tabbed, line);
		printf("%s", tabbed);
	}

	return (0);
}
コード例 #22
0
ファイル: csplit.c プロジェクト: AlainODea/illumos-gate
static offset_t
findline(char *expr, offset_t oset)
{
	static int benhere = 0;
	offset_t lncnt = 0, saveloc;

	saveloc = ftello(infile);
	if (curline != (offset_t)1 || benhere)	/* If first line, first time, */
		(void) getaline(FALSE);		/* then don't skip */
	else
		lncnt--;
	benhere = 1;
	while (getaline(FALSE) != NULL) {
		lncnt++;
		if ((sptr = strrchr(linbuf, '\n')) != NULL)
			*sptr = '\0';
		if (step(linbuf, expr)) {
			(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
			return (curline+lncnt+oset);
		}
	}
	(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
	return (curline+lncnt+oset+2);
}
コード例 #23
0
ファイル: csplit.c プロジェクト: AlainODea/illumos-gate
static void
to_line(offset_t ln)
{
	outfile = getfile();
	if (ln != LAST) {
		if (curline > ln)
			fatal("%s - out of range\n", targ);
		while (curline < ln) {
			if (getaline(TRUE) == NULL)
				fatal("%s - out of range\n", targ);
			flush();
		}
	} else		/* last file */
		if (getaline(TRUE) != NULL) {
			flush();
			for (;;) {
				if (getaline(TRUE) == NULL)
					break;
				flush();
			}
		} else
			fatal("%s - out of range\n", targ);
	closefile();
}
コード例 #24
0
ファイル: lsort.c プロジェクト: BackupTheBerlios/leafnode
int
main(void)
{
    char *l;
    char **act;
    size_t acount = 0;
    size_t i;
    FILE *f;
    int err = 0;
    mastr *path = mastr_new(LN_PATH_MAX);

    (void)mastr_vcat(path, def_spooldir, "/leaf.node/groupinfo.old", NULL);

    f = fopen(mastr_str(path), "r");
    if (f == NULL) {
	fprintf(stderr, "Error: Can't open \"%s\": %s!\n", mastr_str(path), strerror(errno));
	exit(EXIT_FAILURE);
    }

    act = (char **)critmalloc(sizeof(char *),
			      "Allocating space for active file");

    while ((l = getaline(f))) {
	act = (char **)realloc(act, (acount + 1) * sizeof(char *));
	if (act == NULL) {
	    fprintf(stderr, "out of memory: realloc returned NULL\n");
	    exit(EXIT_FAILURE);
	}
	act[acount] = critstrdup(l, "lsort");
	acount++;
    }
    (void)fclose(f);

    qsort(act, acount, sizeof(char *), &lcomp);

    clearerr(stdout);
    for (i = 0; i < acount; i++) {
	int ok;
	ok = printf("%s\n", act[i]);
	if (ok < 0) { err = 1; break; }
    }

    free(act);
    mastr_delete(path);
    fflush(stdout);
    if (ferror(stdout)) err = 1;
    return err ? EXIT_FAILURE : EXIT_SUCCESS;
}
コード例 #25
0
ファイル: config_file.c プロジェクト: CatalystG/snes9x-next
config_file_t *config_file_new(const char *path)
{

	struct config_file *conf = calloc(1, sizeof(*conf));
	if (conf == NULL)
		return NULL;

	if (path == NULL)
		return conf;

	FILE *file = fopen(path, "r");
	if (!file)
	{
		free(conf);
		return NULL;
	}

	struct entry_list *tail = conf->entries;

	while (!feof(file))
	{
		struct entry_list *list = calloc(1, sizeof(*list));
		char *line = getaline(file);

		if (line)
		{
			if (parse_line(list, line))
			{
				if (conf->entries == NULL)
				{
					conf->entries = list;
					tail = list;
				}
				else
				{
					tail->next = list;
					tail = list;
				}
			}
			free(line);
		}
	}
	fclose(file);

	return conf;
}
コード例 #26
0
ファイル: 1.9.c プロジェクト: adamesque/k-r
/* print longest input line */
main()
{
	int len;				/* current line length */
	int max;				/* maximum length seen so far */
	char line[MAXLINE];		/* current input line */
	char longest[MAXLINE];	/* longest line saved here */

	max = 0;
	while ((len = getaline(line, MAXLINE)) > 0)
		if (len > max) {
			max = len;
			copy(longest, line);
		}
	if (max > 0)	/* there was a line */
		printf("%s", longest);
	return 0;
}
コード例 #27
0
ファイル: ex_io.c プロジェクト: drscream/illumos-joyent
void
rop2(void)
{
	line *first, *last, *a;

	deletenone();
	clrstats();
	first = addr2 + 1;
	(void)append(getfile, addr2);
	last = dot;
	if (value(vi_MODELINES))
		for (a=first; a<=last; a++) {
			if (a==first+5 && last-first > 10)
				a = last - 4;
			getaline(*a);
				chkmdln(linebuf);
		}
}
コード例 #28
0
/*
 * call getaline with timeout
 */
char *
timeout_getaline(FILE * f, unsigned int timeout)
{
    char *l;
    struct sigaction sa;

    if (sigsetjmp(to, 1)) {
	ln_log(LNLOG_SERR, LNLOG_CTOP, "timeout reading.");
	return NULL;
    }
    sa.sa_handler = timer;
    sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
    sigemptyset(&sa.sa_mask);
    (void)sigaction(SIGALRM, &sa, NULL);
    (void)alarm(timeout);
    l = getaline(f);
    (void)alarm(0U);
    return l;
}
コード例 #29
0
ファイル: rnews.c プロジェクト: BackupTheBerlios/leafnode
/*
 * process any file
 * return 0 if failed, 1 otherwise
 */
static int
fprocessfile(FILE * f)
{
    char *l;
    int i;

    const struct compressors {
	const char *command;	/* read from batch */
	const char *compressor[4];/* command to run */
    } compressors[] = {
	{ "cunbatch", { GZIP, "-c", "-d", NULL} },
	{ "gunbatch", { GZIP, "-c", "-d", NULL} },
	{ "zunbatch", { GZIP, "-c", "-d", NULL} },
	{ "bunbatch", { BZIP2, "-c", "-d", NULL} },
    };

    l = getaline(f);
    if (!l || strlen(l) < 2) {
	return 0;
    }
    if (str_isprefix(l, "#! rnews ")) {
	    return fprocessbatch(f, l);
    } else if (str_isprefix(l, "#!")) {
	char *c;

	c = l + 2;
	SKIPLWS(c);
	for (i = 0;
	     i < (long)(sizeof(compressors) / sizeof(struct compressors));
	     i++) {
	    if (str_isprefix(c, compressors[i].command)) {
		FILE *uc;

		uc = decompresspipe(f, compressors[i].compressor);
		if (uc == NULL)
			return 0;
		return fprocessbatch(uc, NULL);
	    }
	}
    }
    return 1;
}
コード例 #30
0
ファイル: entab.c プロジェクト: carlosjrxd/C4Dummies
int main()
{
  int i,t;
  int spacecount,len;

  while (( len = getaline()) > 0 )
    {
      spacecount = 0;
      for( i=0; i < len; i++)
	{
	  if(line[i] == ' ')
	    spacecount++; /* increment counter for each space */
	  if(line[i] != ' ')
	    spacecount = 0; /* reset counter */
	  if(spacecount == TAB2SPACE) /* Now we have enough spaces
				      ** to replace them with a tab
				      */
	    {
	      /* Because we are removing 4 spaces and
	      ** replacing them with 1 tab we move back 
	      ** three chars and replace the ' ' with a \t
	      */
	      i -= 3; /* same as "i = i - 3" */
	      len -= 3;
	      line[i] = '\t';
	      /* Now move all the char's to the right into the
	      ** places we have removed.
	      */
	      for(t=i+1;t<len;t++)
		line[t]=line[t+3];
	      /* Now set the counter back to zero and move the 
	      ** end of line back 3 spaces
	      */
	      spacecount = 0;
	      line[len] = '\0'; 
	    }
	}
      printf("%s", line);
    }
  return 0;
}